#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[5050], cir[5050], tot, fa[5050], dep[5050];
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;
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 | 441.6 us | 1 MB + 1008 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 516.92 us | 1 MB + 1020 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 472.47 us | 1 MB + 1008 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 464.56 us | 1 MB + 1008 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 1.584 ms | 2 MB + 296 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 1.574 ms | 2 MB + 276 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 26.771 ms | 7 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 26.665 ms | 6 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 146.955 ms | 31 MB + 796 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 147.98 ms | 34 MB + 608 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 147.472 ms | 26 MB + 424 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 148.322 ms | 29 MB + 468 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 184.897 ms | 2 MB + 376 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 245.92 ms | 2 MB + 436 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 243.56 ms | 575 MB + 72 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #16 | 243.45 ms | 575 MB + 72 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #17 | 282.258 ms | 575 MB + 64 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #18 | 282.279 ms | 575 MB + 64 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #19 | 285.722 ms | 575 MB + 72 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #20 | 284.321 ms | 575 MB + 60 KB | Runtime Error | Score: 0 | 显示更多 |