#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;
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);
}
}
int x;
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]){
for(int i=0;i<=16;i++)
if((dub[b]-dub[a])&(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{
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];
}
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6748 KB |
Output is correct |
2 |
Correct |
8 ms |
6772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
385 ms |
6492 KB |
Output is correct |
2 |
Correct |
376 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
6744 KB |
Output is correct |
2 |
Correct |
794 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1200 ms |
9476 KB |
Output is correct |
2 |
Correct |
957 ms |
9552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1437 ms |
13140 KB |
Output is correct |
2 |
Correct |
1250 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2291 ms |
13520 KB |
Output is correct |
2 |
Correct |
2286 ms |
13512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3044 ms |
14396 KB |
Output is correct |
2 |
Correct |
2898 ms |
14336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3948 ms |
14316 KB |
Output is correct |
2 |
Correct |
3765 ms |
14340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5067 ms |
14120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |