#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=5e5+10;
const int inf=1e9;
vector <int> vec[M];
void add(int a,int b){
vec[a].push_back(b);
vec[b].push_back(a);
}
bool vis[M],cir[M],finded;
int stk[M],up;
void dfs_cir(int a,int b){
vis[a]=1;stk[++up]=a;
for(int i=0,sz=vec[a].size(),v;i<sz;i++){
v=vec[a][i];
if(finded) return;
if(v==b) continue;
if(vis[v]){
finded=1;
for(;up&&stk[up]!=v;--up){
cir[stk[up]]=1;
} cir[stk[up--]]=1;
return;
}else{
dfs_cir(v,a);
}
}
vis[stk[up--]]=0;
}
int ans[M],tot,ls[M],top,delet;
void dfs_ans(int a,int nex){
vis[a]=1;ans[++tot]=a;
int p=top+1,q;
for(int i=0,sz=vec[a].size(),v;i<sz;i++){
v=vec[a][i];
if(!vis[v])ls[++top]=v;
}
q=top;
for(int i=p,j=p+1;i<=q;i=j,j++){
if(vis[ls[i]]) continue;
while(j<=q&&vis[ls[j]])++j;
if(j<=q)dfs_ans(ls[i],ls[j]);
else if(!delet&&cir[a]&&cir[ls[i]]&&ls[i]>nex)delet=1;
else dfs_ans(ls[i],nex);
}
}
int a,b,n,m;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i=1;i<=n;i++){sort(vec[i].begin(),vec[i].end());}
if(m==n-1){
dfs_ans(1,inf);
}else{
dfs_cir(1,1);
memset(vis,0,sizeof(vis));
dfs_ans(1,inf);
}
for(int i=1;i<n;i++) printf("%d ",ans[i]);printf("%d",ans[n]);
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 2.018 ms | 11 MB + 492 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 2.052 ms | 11 MB + 984 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 2.036 ms | 11 MB + 500 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 2.011 ms | 11 MB + 500 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 3.611 ms | 11 MB + 928 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 3.573 ms | 11 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 38.885 ms | 19 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 39.374 ms | 18 MB + 344 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 358.68 ms | 53 MB + 156 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 361.57 ms | 56 MB + 692 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 356.843 ms | 46 MB + 524 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 360.98 ms | 50 MB + 284 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 3.78 ms | 12 MB + 424 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 3.757 ms | 12 MB + 496 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 43.177 ms | 21 MB + 160 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 43.893 ms | 21 MB + 984 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 455.646 ms | 74 MB + 800 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 456.304 ms | 74 MB + 800 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 407.786 ms | 55 MB + 724 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 435.373 ms | 66 MB + 308 KB | Accepted | Score: 5 | 显示更多 |