forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiconnectedgraph.cpp
105 lines (96 loc) · 2.13 KB
/
biconnectedgraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct BiconectedGraph{
typedef pair<int,int> P;
int n;
vector<vector<int> > G,C,T;
vector<int> ord,low,belong;
vector<P> B;
BiconectedGraph(){}
BiconectedGraph(int sz):n(sz),G(sz),C(sz),T(sz){}
void add_edge(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
void input(int m,int offset=0){
int a,b;
for(int i=0;i<m;i++){
cin>>a>>b;
add_edge(a+offset,b+offset);
}
}
bool is_bridge(int u,int v){
if(ord[u]>ord[v]) swap(u,v);
return ord[u]<low[v];
}
void dfs(int v,int p,int &k){
ord[v]=low[v]=k;
++k;
for(int u:G[v]){
if(u==p) continue;
if(ord[u]>=0){
low[v]=min(low[v],ord[u]);
}else{
dfs(u,v,k);
low[v]=min(low[v],low[u]);
}
if(is_bridge(u,v)) B.push_back(P(u,v));
}
}
void fill_component(int c,int v){
C[c].push_back(v);
belong[v]=c;
for(int u:G[v]){
if(belong[u]>=0||is_bridge(u,v)) continue;
fill_component(c,u);
}
}
void add_component(int v,int &k){
if(belong[v]>=0) return;
fill_component(k++,v);
}
int build(){
int k=0;
ord.resize(n);
low.resize(n);
belong.resize(n);
fill(ord.begin(),ord.end(),-1);
fill(belong.begin(),belong.end(),-1);
for(int v=0;v<n;v++){
if(ord[v]>=0) continue;
dfs(v,-1,k);
}
k=0;
for(int i=0;i<(int)B.size();i++){
add_component(B[i].first,k);
add_component(B[i].second,k);
}
for(int v=0;v<n;v++) add_component(v,k);
for(int i=0;i<(int)B.size();i++){
int u=belong[B[i].first],v=belong[B[i].second];
T[u].push_back(v);
T[v].push_back(u);
}
return k;
}
};
//END CUT HERE
signed main(){
int v,e;
cin>>v>>e;
BiconectedGraph bcc(v);
bcc.input(e,0);
bcc.build();
typedef pair<int,int> P;
vector<P> B=bcc.B;
for(P &p:B) if(p.first>p.second) swap(p.first,p.second);
sort(B.begin(),B.end());
for(P p:B) cout<<p.first<<" "<<p.second<<endl;
return 0;
}
/*
verified on 2017/12/20
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_B&lang=jp
*/