Submission #1060666

#TimeUsernameProblemLanguageResultExecution timeMemory
1060666NemanjaSo2005Pipes (CEOI15_pipes)C++17
100 / 100
4963 ms14420 KiB
#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,c2=0,S2[100]; int getp(int x){ while(rod[x]!=x){ S2[++c2]=x; x=rod[x]; } while(c2>0){ rod[S2[c2]]=x; c2--; } return 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...