# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
994675 |
2024-06-08T03:31:56 Z |
vjudge1 |
Pipes (CEOI15_pipes) |
C++17 |
|
1915 ms |
62276 KB |
#include<bits/stdc++.h>
using namespace std;
#define N 100100
#define C cyc.abp
struct unionf{
int par[N];
unionf(){
for(int i=0;i<N;i++)
par[i]=i;
}
int abp(int a){
return (par[a]-a?par[a]=abp(par[a]):a);
}
} cyc,comp;
int par[N],sz[N],dep[N];
set<pair<int,int>>st;
vector<pair<int,int>>adj[N];
int depth=0,times=0;
void repos(int x,int p,int d){
par[x]=p,dep[x]=d;
for(auto[i,j]:adj[x])
if(C(i)-p&&C(i)!=x)
repos(C(i),x,d+1);
depth--;
}
int mergepar(int a){
times++;
if(times>N){
cout<<"fail (infinite loop)";
exit(0);
}
int b=par[a];
dep[a]=dep[b];
par[a]=par[b];
if(adj[a].size()<adj[b].size())
swap(a,b);
cyc.par[b]=a;
for(auto[i,j]:adj[b])
if(C(i)-a&&C(i)-b){
adj[a].push_back({i,j});
if(C(i)-par[a])
par[C(i)]=a;
}
adj[b].clear();
return a;
}
int main(){
int n,m,ee=0;
cin>>n>>m;
for(int i=1;i<=n;i++)
sz[i]=1;
while(m--){
if(ee>=n){
cout<<"fail (merged too many times)";
exit(0);
}
int a,b;
cin>>a>>b;
if(C(a)==C(b))
continue;
if(a==7&&b==5){
cerr<<"stuff";
}
if(comp.abp(a)==comp.abp(b)){
int acyc=C(a),bcyc=C(b);
while(acyc-bcyc){
if(dep[acyc]<dep[bcyc])
swap(acyc,bcyc);
acyc=mergepar(acyc),
bcyc=C(bcyc);
}
} else {
int acyc=C(a),bcyc=C(b);
int acomp=comp.abp(a),bcomp=comp.abp(b);
if(sz[acomp]<sz[bcomp]) swap(a,b),
swap(acyc,bcyc),swap(acomp,bcomp);
sz[acomp]+=sz[bcomp];
comp.par[bcomp]=acomp;
repos(bcyc,acyc,dep[acyc]+1);
adj[acyc].push_back({b,a});
adj[bcyc].push_back({a,b});
ee++;
}
}
for(int i=1;i<=n;i++)if(C(i)==i)
for(auto[j,k]:adj[i]) if(C(j)-i)
st.insert({min(j,k),max(j,k)});
for(auto[i,j]:st)
cout<<i<<' '<<j<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4956 KB |
Output is correct |
2 |
Correct |
5 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
4700 KB |
Output is correct |
2 |
Correct |
156 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
4956 KB |
Output is correct |
2 |
Correct |
321 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
453 ms |
5720 KB |
Output is correct |
2 |
Runtime error |
415 ms |
19776 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
585 ms |
7628 KB |
Output is correct |
2 |
Runtime error |
533 ms |
25808 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
929 ms |
8136 KB |
Output is correct |
2 |
Runtime error |
934 ms |
41352 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1201 ms |
8908 KB |
Output is correct |
2 |
Runtime error |
1208 ms |
50516 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1522 ms |
8908 KB |
Output is correct |
2 |
Runtime error |
1468 ms |
62276 KB |
Memory limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1915 ms |
20172 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |