#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 (u == v)return;
if(dep[u]>dep[v])
swap(u,v);
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){
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 dfs2(int src,int p){
if(p!=-1)
dep[src]=dep[p]+1;
for(int nbr:inv[src])
dfs2(nbr,src);
}
void add_edge(int u,int v){
ans.insert({min(u,v),max(u,v)});
if(min(u,v)==11 && max(u,v)==24){
int brkpoint=1;
}
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);
dfs2(v,-1);
up[v]=u;
inv[u].insert(v);
}
void solve(){
int n,m;
cin >> n >> m;
up.resize(n+1,-1);
dep.resize(n+1);
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(v!=-1 && dep[u]<dep[v]){
ans.erase({min(up[v],v),max(v,up[v])});
v=up[get2(v)];
}
while(u!=-1 && v!=-1 && get2(u)!=get2(v)) {
ans.erase({min(up[v], v), max(v, up[v])});
ans.erase({min(up[u], u), max(u, up[u])});
v = up[v];
u = up[u];
}
}
for(pi p:ans)
cout << p.first << " " << p.second << "\n";
}
Compilation message
pipes.cpp: In function 'void add_edge(int, int)':
pipes.cpp:63:13: warning: unused variable 'brkpoint' [-Wunused-variable]
63 | int brkpoint=1;
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
1132 KB |
Output is correct |
2 |
Correct |
20 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
725 ms |
4556 KB |
Output is correct |
2 |
Correct |
347 ms |
6084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1856 ms |
5572 KB |
Output is correct |
2 |
Correct |
1034 ms |
13020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3857 ms |
7304 KB |
Output is correct |
2 |
Execution timed out |
5026 ms |
11192 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5052 ms |
15872 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5042 ms |
16524 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5053 ms |
18888 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5070 ms |
18880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5038 ms |
18428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |