#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
int N,M,vel[maxn],rod[maxn],dub[maxn],lift[maxn][17],kol[maxn];
vector<int> stablo[maxn];
vector<pair<int,int>> R;
int S[maxn],cnt=0;
int getp(int x){
if(rod[x]==x)
return x;
rod[x]=getp(rod[x]);
return rod[x];
}
int main(){
int a,b,oa,ob;
int x;
cin>>N>>M;
for(int i=1;i<=N;i++){
vel[i]=1;
rod[i]=i;
}
while(M--){
cin>>a>>b;
if(getp(a)!=getp(b)){
oa=a;
ob=b;
a=getp(a);
b=getp(b);
if(vel[a]<vel[b]){
swap(a,b);
swap(oa,ob);
}
rod[b]=a;
vel[a]+=vel[b];
stablo[oa].push_back(ob);
stablo[ob].push_back(oa);
}
}
for(int i=1;i<=N;i++)
if(lift[i][0]==0){
S[++cnt]=i;
for(int j=cnt;j<=cnt;j++){
for(int x:stablo[S[j]]){
if(x==lift[S[j]][0])
continue;
lift[x][0]=S[j];
S[++cnt]=x;
}
}
}
dub[0]=-1;
for(int i=1;i<=N;i++)
dub[S[i]]=dub[lift[S[i]][0]]+1;
for(int j=1;j<=16;j++)
for(int i=1;i<=N;i++)
lift[i][j]=lift[lift[i][j-1]][j-1];
cin.clear(); // Clear the EOF flag
cin.seekg(0, std::ios::beg);
cin>>N>>M;
for(int i=1;i<=N;i++){
vel[i]=1;
rod[i]=i;
}
while(M--){
cin>>a>>b;
if(getp(a)==getp(b)){
kol[a]++;
kol[b]++;
if(dub[a]>dub[b])
swap(a,b);
if(dub[b]>dub[a]){
x=dub[b]-dub[a];
for(int i=0;i<=16;i++)
if(x&(1<<i))
b=lift[b][i];
}
if(a!=b){
for(int i=16;i>=0;i--)
if(lift[a][i]!=lift[b][i]){
a=lift[a][i];
b=lift[b][i];
}
a=lift[a][0];
}
kol[a]-=2;
}
else{
a=getp(a);
b=getp(b);
if(vel[a]<vel[b]){
rod[a]=b;
vel[b]+=vel[a];
}
else{
rod[b]=a;
vel[a]+=vel[b];
}
}
}
for(int i=N;i>=1;i--){
x=S[i];
for(int y:stablo[x])
if(y!=lift[x][0])
kol[x]+=kol[y];
if(kol[x]==0 and lift[x][0]!=0)
R.push_back({min(x,lift[x][0]),max(x,lift[x][0])});
}
sort(R.begin(),R.end());
for(auto x:R)
cout<<x.first<<" "<<x.second<<"\n";
return 0;
}
/*
10 13
2 6
7 2
5 10
10 3
3 4
5 8
9 7
4 2
7 9
5 8
1 3
3 7
2 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6748 KB |
Output is correct |
2 |
Correct |
8 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
6492 KB |
Output is correct |
2 |
Correct |
359 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
735 ms |
6748 KB |
Output is correct |
2 |
Correct |
779 ms |
6948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1126 ms |
9552 KB |
Output is correct |
2 |
Correct |
935 ms |
10032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1382 ms |
13140 KB |
Output is correct |
2 |
Correct |
1241 ms |
13136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2294 ms |
13588 KB |
Output is correct |
2 |
Correct |
2264 ms |
13648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2943 ms |
14424 KB |
Output is correct |
2 |
Correct |
2903 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4094 ms |
14320 KB |
Output is correct |
2 |
Correct |
3737 ms |
14336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4732 ms |
14168 KB |
Output is correct |
2 |
Runtime error |
4592 ms |
30608 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |