#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;
void dfs(int gde,int pret){
//cout<<gde<<" "<<pret<<endl;
dub[gde]=dub[pret]+1;
lift[gde][0]=pret;
for(int i=1;i<=16;i++)
lift[gde][i]=lift[lift[gde][i-1]][i-1];
for(int x:stablo[gde])
if(x!=pret)
dfs(x,gde);
}
inline int getp(int x){
if(rod[x]==x)
return x;
rod[x]=getp(rod[x]);
return rod[x];
}
inline void spoj(int a,int b,bool ost=false){
int oa=a,ob=b;
a=getp(a);
b=getp(b);
//cout<<a<<" ab "<<b<<endl;
if(vel[a]<vel[b]){
swap(a,b);
swap(oa,ob);
}
rod[b]=a;
vel[a]+=vel[b];
if(ost){
stablo[oa].push_back(ob);
stablo[ob].push_back(oa);
}
//dfs(ob,oa);
}
inline int digni(int ko,int x){
for(int i=0;i<=16;i++)
if(x&(1<<i))
ko=lift[ko][i];
return ko;
}
inline int LCA(int a,int b){
if(dub[a]>dub[b])
swap(a,b);
while(dub[b]>dub[a])
b=lift[b][0];
while(a!=b){
a=lift[a][0];
b=lift[b][0];
}
return a;
/*if(dub[b]>dub[a])
b=digni(b,dub[b]-dub[a]);
if(a==b)
return a;
for(int i=16;i>=0;i--)
if(lift[a][i]!=lift[b][i]){
a=lift[a][i];
b=lift[b][i];
}*/
return lift[a][0];
}
void dfs2(int gde,int pret){
for(int x:stablo[gde])
if(x!=pret){
dfs2(x,gde);
kol[gde]+=kol[x];
}
if(pret!=0 and kol[gde]==0){
R.push_back({min(gde,pret),max(gde,pret)});
}
}
int main(){
/*
ifstream inf;
ofstream ouf;
inf.open("Input.txt");
ouf.open("Output1.txt");
#define cin inf*/
// #define cout ouf
//cout<<sizeof(vel)+sizeof(rod)+sizeof(dub)+sizeof(lift)+sizeof(kol)<<endl;
int a,b;
// N=100000;
// M=6000000;
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))
spoj(a,b,true);
}
dub[0]=-1;
for(int i=1;i<=N;i++)
if(dub[i]==0)
dfs(i,0);
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]++;
kol[LCA(a,b)]-=2;
}
else
spoj(a,b,false);
}
// cout<<LCA(10,2)<<endl;
/*cout<<"STABLO:"<<endl;
for(int i=1;i<=N;i++){
cout<<i<<": "<<lift[i][0]<<endl;
}*/
//cout<<sizeof(stablo)<<endl;
//cin>>a;
for(int i=1;i<=N;i++)
if(dub[i]==0)
dfs2(i,0);
sort(R.begin(),R.end());
for(auto x:R)
cout<<x.first<<" "<<x.second<<"\n";
// inf.close();
// ouf.close();
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 |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7000 KB |
Output is correct |
2 |
Correct |
8 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
12112 KB |
Output is correct |
2 |
Correct |
360 ms |
11960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
797 ms |
16512 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1460 ms |
25680 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2473 ms |
34012 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3897 ms |
46684 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5054 ms |
57940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5007 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5037 ms |
65536 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |