#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int M = 20055 ;
using namespace std ;
inline int read() {
char c = getchar() ; int x = 0 , w = 1 ;
while(c>'9'||c<'0') { if(c=='-') w = -1 ; c = getchar() ; }
while(c>='0'&&c<='9') { x = x*10+c-'0' ; c = getchar() ; }
return x*w ;
}
int n , m ;
int hea[M] , num ;
int temp[M] , tot , Ans[M] ;
bool vis[M] , use[M] ;
vector < int > e[M] ;
int dfn[M] , low[M] , cnt ;
int bel[M] , st[M] , top , sz ;
bool exist[M] ;
int id[5005][5005] ;
struct E {
int Nxt , From , to ;
} edge[M] ;
inline void add_edge(int from , int to) {
edge[++num].Nxt = hea[from] ;
edge[num].From = from ;
edge[num].to = to ; hea[from] = num ;
}
void Dfs(int u , int father) {
temp[++tot] = u ;
for(int i = hea[u] ; i ; i = edge[i].Nxt) {
int v = edge[i].to ;
if(v == father || vis[i]) continue ;
Dfs(v , u) ;
}
}
inline void Solve1() {
Dfs(1 , 1) ;
for(int i = 1 ; i <= tot ; i ++) printf("%d ",temp[i]) ;
}
void tarjan(int u , int father) {
dfn[u] = low[u] = ++cnt ; exist[u] = true ; st[++top] = u ;
for(int i = hea[u] ; i ; i = edge[i].Nxt) {
int v = edge[i].to ;
if(v == father) continue ;
if(!dfn[v]) {
tarjan(v , u) ;
low[u] = min(low[u] , low[v]) ;
}
if(exist[v]) low[u] = min(low[u] , dfn[v]) ;
}
if(dfn[u] == low[u]) {
++ sz ;
while(st[top + 1] != u) {
bel[st[top]] = sz ;
exist[st[top --]] = false ;
}
}
}
inline bool chkmin() {
for(int i = 1 ; i <= tot ; i ++){
if(Ans[i] > temp[i]) return true ;
else if(Ans[i] < temp[i]) return false ;
}
return false ;
}
inline void Solve2() {
for(int i = 1 ; i <= n ; i ++)
if(!dfn[i])
tarjan(i , i) ;
memset(Ans , 63 , sizeof(Ans)) ;
for(int i = 1 , u , v ; i <= num ; i ++) {
u = edge[i].From , v = edge[i].to ;
if(bel[u] != bel[v]) continue ;
if(use[id[u][v]] || use[id[v][u]]) continue ;
use[id[u][v]] = use[id[v][u]] = true ;
vis[id[u][v]] = vis[id[v][u]] = true ;
tot = 0 ;
Dfs(1 , 1) ;
vis[id[u][v]] = vis[id[v][u]] = false ;
if(chkmin())
for(int j = 1 ; j <= tot ; j ++)
Ans[j] = temp[j] ;
}
for(int i = 1 ; i <= n ; i ++) printf("%d ",Ans[i]) ;
}
int main() {
n = read() , m = read() ;
for(int i = 1 , u , v ; i <= m ; i ++) {
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()) ;
for(int j = e[i].size() - 1 , v ; j >= 0 ; j --) {
v = e[i][j] ;
add_edge(i , v) ;
id[i][v] = num ;
}
}
if(m == n - 1) {
Solve1() ;
return 0 ;
}
else Solve2() ;
return 0 ;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 113.31 us | 560 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 118.3 us | 560 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 110.76 us | 560 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 162.13 us | 932 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 157.47 us | 940 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 672.66 us | 5 MB + 820 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 673.79 us | 5 MB + 844 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 672.54 us | 5 MB + 856 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 658.92 us | 5 MB + 552 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 661.74 us | 5 MB + 564 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 3.499 ms | 34 MB + 916 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 3.511 ms | 35 MB + 20 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 3.52 ms | 35 MB + 280 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 3.493 ms | 34 MB + 936 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 3.534 ms | 35 MB + 416 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 121.76 us | 664 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 121.09 us | 664 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 205.96 us | 1 MB + 24 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 203.1 us | 1 MB + 16 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 8.546 ms | 5 MB + 924 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 8.518 ms | 5 MB + 924 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 8.859 ms | 6 MB + 12 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 275.426 ms | 35 MB + 492 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 260.911 ms | 35 MB + 100 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 247.084 ms | 35 MB + 60 KB | Accepted | Score: 4 | 显示更多 |