提交记录 13844


用户 题目 状态 得分 用时 内存 语言 代码长度
widsnoy 2002. 【NOIP2018】旅行(加强版) Accepted 100 450.524 ms 69752 KB C++ 2.28 KB
提交时间 评测时间
2020-08-12 20:00:31 2020-08-12 20:00:39
#include<bits/stdc++.h>
using namespace std;

const int N = 5e5 + 5;
int n, m, ans[N], stk[N], top;
vector<int> e[N];

int read() {
    int w = 0; char ch = getchar();
    while(ch > '9' || ch < '0') ch = getchar();
    while(ch >= '0' && ch <= '9') {
        w = w * 10 + ch - 48;
        ch = getchar();
    }
    return w;
}

namespace subtask1 {
    int dep = 0;
    void dfs(int u, int fa) {
        ans[++dep] = u;
        if (dep == n) return;
        for (int v : e[u]) {
            if (v == fa) continue;
            dfs(v, u);
        }
    }
    void main() {
        dfs(1, -1);
        for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
        putchar('\n');
        exit(0);
    }
}
namespace subtask2 {
    int dep = 0, g[N] = {}, vis[N] = {}, cnt, pd = 0, yel = N;
    void find(int u, int fa) {
        if (pd) return;
        stk[++top] = u; vis[u] = 1;
        for (int v : e[u]) {
            if (v == fa) continue;
            if (vis[v]) {
                while (stk[top] != v) g[stk[top--]] = 1;
                g[v] = 1;
                pd = 1;
                return;
            }
            find(v, u);
            if (pd) return;
        }
        top--;
    }
    void dfs(int u, int fa) {
        ans[++dep] = u; vis[u] = 1;
        for (int i = 0; i < e[u].size(); i++) {
            int v = e[u][i];
            if (v == fa || vis[v]) continue;
            if (!g[v]) dfs(v, u);
            else {
                if (!pd) {
                    int to = i == e[u].size() - 1 ? yel : e[u][i + 1];
                    if (to == fa) yel = i == e[u].size() - 2 ? yel : e[u][i + 2];
                    else yel = to;
                }
                if (yel < v && !pd) {
                    pd = 1; continue;
                }
                else dfs(v, u);
            }
        }
    }
    void main() {
        find(1, -1);
        pd = 0;
        memset(vis, 0, sizeof vis);
        dfs(1, -1);
        for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
        putchar('\n');
        exit(0);
    }
}

int main() {
    n = read(); m = read();
    for (int i = 1; i <= m; i++) {
        int u = read(), v = read();
        e[u].push_back(v); e[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) sort(e[i].begin(), e[i].end());
    if (n == m + 1) subtask1::main();
    else subtask2::main();
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #12.097 ms11 MB + 504 KBAcceptedScore: 5

Testcase #22.248 ms13 MB + 408 KBAcceptedScore: 5

Testcase #32.098 ms11 MB + 512 KBAcceptedScore: 5

Testcase #42.125 ms11 MB + 512 KBAcceptedScore: 5

Testcase #53.518 ms11 MB + 832 KBAcceptedScore: 5

Testcase #63.515 ms11 MB + 816 KBAcceptedScore: 5

Testcase #735.526 ms17 MB + 760 KBAcceptedScore: 5

Testcase #835.818 ms16 MB + 944 KBAcceptedScore: 5

Testcase #9340.506 ms43 MB + 236 KBAcceptedScore: 5

Testcase #10344.444 ms45 MB + 352 KBAcceptedScore: 5

Testcase #11339.716 ms39 MB + 272 KBAcceptedScore: 5

Testcase #12344.226 ms41 MB + 524 KBAcceptedScore: 5

Testcase #133.84 ms13 MB + 820 KBAcceptedScore: 5

Testcase #143.847 ms13 MB + 880 KBAcceptedScore: 5

Testcase #1542.535 ms21 MB + 620 KBAcceptedScore: 5

Testcase #1643.448 ms22 MB + 264 KBAcceptedScore: 5

Testcase #17450.524 ms68 MB + 120 KBAcceptedScore: 5

Testcase #18450.35 ms68 MB + 120 KBAcceptedScore: 5

Testcase #19401.089 ms52 MB + 712 KBAcceptedScore: 5

Testcase #20429.337 ms61 MB + 264 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-22 18:18:16 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠