#include<bits/stdc++.h>
using namespace std;
int n, m, hea[505000], to[1001000], ban1=-1, ban2=-1;
int ui[505000], vi[505000], ans[505000], st[505000], tp, win;
void dfs(int x,int fff){
st[++tp]=x;
if (win==0&&st[tp]!=ans[tp]){
win=st[tp]<ans[tp]? 1: -1;
}
if (win==-1) return;
for (int i=hea[x];i<hea[x+1];++i)
if (to[i]!=fff&&i!=ban1&&i!=ban2){
dfs(to[i],x);
}
}
void realmain(){
tp=0; win=0;
dfs(1,0);
if (win==1) memcpy(ans,st,sizeof ans);
}
void BAN(int u,int v){
ban1=lower_bound(to+hea[u],to+hea[u+1],v)-to;
ban2=lower_bound(to+hea[v],to+hea[v+1],u)-to;
}
int usd[505000], cir[505000], tot, fa[505000], dep[505000];
void getcir(int x,int fff){
usd[x]=1;
dep[x]=dep[fff]+1; fa[x]=fff;
for (int i=hea[x];i<hea[x+1];++i){
int y=to[i]; if (y==fff) continue;
if (!usd[y]) getcir(y,x);
else if (dep[x]>dep[y]){
for (int t=x;t!=y;t=fa[t])
cir[tot++]=t;
cir[tot++]=y;
}
}
}
int main(){
cin>>n>>m; int u, v; if (max(n,m)>5e5) return 0;
for (int i=1;i<=m;++i){
scanf("%d%d",&u,&v);
++hea[u]; ++hea[v];
ui[i]=u; vi[i]=v;
}
for (int i=1;i<=n+1;++i) hea[i]+=hea[i-1];
for (int i=1;i<=m;++i){
u=ui[i]; v=vi[i];
to[--hea[u]]=v;
to[--hea[v]]=u;
}
for (int i=1;i<=n;++i)
sort(to+hea[i],to+hea[i+1]);
memset(ans,33,sizeof ans);
if (m==n-1) realmain();
else{
getcir(1,0);
for (int i=0;i<tot;++i){
BAN(cir[i],cir[(i+1)%tot]);
realmain();
}
}
for (int i=1;i<=n;++i) printf("%d ",ans[i]);
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 437.83 us | 1 MB + 1008 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 513.92 us | 1 MB + 1020 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 468.24 us | 1 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 465.49 us | 1 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 1.589 ms | 2 MB + 300 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 1.586 ms | 2 MB + 280 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 26.844 ms | 7 MB + 824 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 26.698 ms | 6 MB + 744 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 146.315 ms | 31 MB + 800 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 147.471 ms | 34 MB + 612 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 146.787 ms | 26 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 147.743 ms | 29 MB + 472 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 184.644 ms | 2 MB + 392 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 244.386 ms | 2 MB + 452 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 1 s | 9 MB + 244 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #16 | 1 s | 9 MB + 920 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #17 | 1 s | 51 MB + 596 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #18 | 1 s | 51 MB + 596 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #19 | 1 s | 36 MB + 44 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #20 | 1 s | 44 MB + 728 KB | Time Limit Exceeded | Score: 0 | 显示更多 |