#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();
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 2.097 ms | 11 MB + 504 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 2.248 ms | 13 MB + 408 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 2.098 ms | 11 MB + 512 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 2.125 ms | 11 MB + 512 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 3.518 ms | 11 MB + 832 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 3.515 ms | 11 MB + 816 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 35.526 ms | 17 MB + 760 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 35.818 ms | 16 MB + 944 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 340.506 ms | 43 MB + 236 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 344.444 ms | 45 MB + 352 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 339.716 ms | 39 MB + 272 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 344.226 ms | 41 MB + 524 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 3.84 ms | 13 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 3.847 ms | 13 MB + 880 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 42.535 ms | 21 MB + 620 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 43.448 ms | 22 MB + 264 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 450.524 ms | 68 MB + 120 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 450.35 ms | 68 MB + 120 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 401.089 ms | 52 MB + 712 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 429.337 ms | 61 MB + 264 KB | Accepted | Score: 5 | 显示更多 |