# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
472858 |
2021-09-14T12:10:23 Z |
Carmel_Ab1 |
Pipes (CEOI15_pipes) |
C++17 |
|
5000 ms |
14388 KB |
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define all(x) x.begin(),x.end()
#define out(x) {cout << x << "\n"; return;}
#define GLHF ios_base::sync_with_stdio(0); cin.tie(0)
#define pb push_back
void solve();
int main(){
GLHF;
solve();
}
vi up;
vi par,rnk,dep,par2;
int get(int x) { return par[x] = (x == par[x] ? x : get(par[x])); }
int get2(int x){return par2[x]=(x==par2[x]?x:get2(par2[x]));}
void unite2(int u,int v){
u = get2(u), v = get2(v);
if(dep[u]>dep[v])
swap(u,v);
if (u == v)return;
par2[v] = u;
}
void unite(int u, int v) {//u in new leader
u = get(u), v = get(v);
if (u == v)return;
if (rnk[u] > rnk[v])
swap(u, v);
par[u] = v;
rnk[v] += rnk[u];
}
vector<set<int>> inv;//inv[i]= {j s.t par[j]=i} = {kids of i}
set<pi>ans;
void dfs(int src,int p=0){
//dep is maintain-able
if(src==-1 )
return;
dfs(up[src],src);
up[src]=p;
if(p)
dep[src]=dep[p]+1;
inv[src].erase(p);
inv[p].insert(src);
}
void add_edge(int u,int v){
ans.insert({min(u,v),max(u,v)});
if(rnk[get(u)]<rnk[get(v)])
swap(u,v);
//deg[u]>deg[v], re-root at v
dep[v]=dep[u]+1;
dfs(v);
up[v]=u;
inv[u].insert(v);
}
void solve(){
int n,m;
cin >> n >> m;
up.resize(n+1,-1);
dep.resize(n+1,1e9);
par.resize(n+1);
rnk.resize(n+1,1);
par2.resize(n+1);
for(int i=0; i<=n; i++)
par[i]=i,par2[i]=i;
inv.resize(n+1);
for(int i=0; i<m; i++){
int u,v;
cin >> u >> v;
if(u==v)
continue;
if(get(u)!=get(v)){
add_edge(u,v);
unite(u,v);
continue;
}
if(dep[u]>dep[v])
swap(u,v);
while(up[v]!=-1 && dep[u]<dep[v]){
ans.erase({min(up[v],v),max(v,up[v])});
unite2(up[v],v);
v=get2(v);
}
while(up[u]!=-1 && up[v]!=-1 && get2(u)!=get2(v)){
ans.erase({min(up[v],v),max(v,up[v])});
unite2(up[v],v);
v=get2(v);
ans.erase({min(up[u],u),max(u,up[u])});
unite2(up[u],u);
u=get2(u);
}
}
for(pi p:ans)
cout << p.first << " " << p.second << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5048 ms |
204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5077 ms |
972 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5081 ms |
844 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5053 ms |
1868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5061 ms |
3660 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5056 ms |
10356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5059 ms |
11204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5047 ms |
14340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5063 ms |
14388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5087 ms |
13624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |