#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e5 + 10 ;
int P[MAX] , P1[MAX] , sz1[MAX] , P2[MAX] , mark[MAX] , dep[MAX] ;
int n , m ;
vector< vector<int> >adj(MAX) ;
void init()
{
for(int i = 1 ; i <= n ; ++i)
P1[i] = i , sz1[i] = 1 ;
}
int root(int node)
{
return P1[node] ;
}
int r = -1 ;
void dfs(int node , int par , bool flag)
{
P1[node] = r ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dep[child] = dep[node] + 1 ;
if(P[node] == child)
dfs(child , node , mark[node]) ;
else
dfs(child , node , mark[child]) ;
}
P[node] = P2[node] = par , mark[node] = flag ;
}
void Union(int x , int y)
{
int a = root(x) ;
int b = root(y) ;
if(sz1[a] < sz1[b])
swap(a , b) , swap(x , y) ;
adj[x].push_back(y) , adj[y].push_back(x) ;
dep[y] = dep[x] + 1 , r = a ;
dfs(y , x , 0) ;
sz1[a] += sz1[b] ;
}
vector<int>v1 , v2 ;
void solve(int x , int y)
{
while(x != y)
{
while(dep[x] >= dep[y] && x != y)
v1.push_back(x) , mark[x] = 1 , x = P2[x] ;
while(dep[y] >= dep[x] && x != y)
v2.push_back(y) , mark[y] = 1 , y = P2[y] ;
}
for(auto &node : v1)
P2[node] = x ;
for(auto &node : v2)
P2[node] = y ;
v1.clear() , v2.clear() ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
init() ;
for(int i = 0 ; i < m ; ++i)
{
int x , y ;
cin>>x>>y ;
if(root(x) != root(y))
Union(x , y) ;
else
solve(x , y) ;
}
for(int i = 1 ; i <= n ; ++i)
{
if(!mark[i] && P[i])
cout<<i<<" "<<P[i]<<"\n" ;
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2892 KB |
Output is correct |
2 |
Correct |
5 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
2892 KB |
Output is correct |
2 |
Correct |
126 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
3188 KB |
Output is correct |
2 |
Correct |
253 ms |
3148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
3916 KB |
Output is correct |
2 |
Correct |
296 ms |
4336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
497 ms |
6536 KB |
Output is correct |
2 |
Correct |
408 ms |
6468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
783 ms |
7048 KB |
Output is correct |
2 |
Correct |
702 ms |
7096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
968 ms |
8180 KB |
Output is correct |
2 |
Correct |
943 ms |
8204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1171 ms |
8228 KB |
Output is correct |
2 |
Correct |
1109 ms |
8348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1446 ms |
7852 KB |
Output is correct |
2 |
Correct |
1402 ms |
10264 KB |
Output is correct |