#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);
if(v==44 && u==81){
int brk=1;
}
while(v!=-1 && dep[u]<dep[v]){
ans.erase({min(up[v],v),max(v,up[v])});
unite2(up[v],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])});
if (dep[get2(v)] > dep[get2(u)]) {
unite2(up[v], v);
v = up[get2(v)];
}
else {
unite2(up[u], u);
u = up[get2(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;
| ^~~~~~~~
pipes.cpp: In function 'void solve()':
pipes.cpp:101:17: warning: unused variable 'brk' [-Wunused-variable]
101 | int brk=1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
1088 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
159 ms |
2372 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
301 ms |
3436 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
648 ms |
5840 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5065 ms |
10552 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5070 ms |
12612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5085 ms |
14716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5049 ms |
14724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5039 ms |
14404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |