Repository navigation
Expand file tree
/
Copy pathBridgeTree.cpp
More file actions
109 lines (102 loc) · 2.61 KB
/
Copy pathBridgeTree.cpp
File metadata and controls
109 lines (102 loc) · 2.61 KB
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
106
107
108
109
struct BridgeTree {
struct edge {
int u, v;
edge(int _u, int _v) : u(_u), v(_v) {}
int other(int x) {
return x == u ? v : u;
}
};
int n, m;
vector<vector<int>> graph;
vector<edge> E;
vector<bool> visited, is_bridge;
vector<int> tin, low;
int timer, component;
vector<vector<int>> newgraph;
vector<int> group;
BridgeTree(int _n = N) : n(_n) {
graph.resize(n + 1);
group.resize(n + 1);
newgraph.resize(n + 1);
component = 0;
m = 0;
}
void add_edge(int u, int v) {
E.push_back(edge(u, v));
graph[u].push_back(m);
graph[v].push_back(m);
m++;
}
void IS_BRIDGE(int x) {
is_bridge[x] = true;
}
void dfs(int cur, int par = -1) {
visited[cur] = true;
tin[cur] = low[cur] = timer++;
for (int to : graph[cur]) {
int x = to;
to = E[to].other(cur);
if (to == par) {
continue;
}
if (visited[to]) {
low[cur] = min(low[cur], tin[to]);
}
else {
dfs(to, cur);
low[cur] = min(low[cur], low[to]);
if (low[to] > tin[cur])
IS_BRIDGE(x);
}
}
}
void find_bridges() {
timer = 0;
visited.assign(n + 1, false);
is_bridge.assign(m, false);
tin.assign(n + 1, -1);
low.assign(n + 1, -1);
for(int i = 1; i <= n; i++) {
if(!visited[i]) {
dfs(i);
}
}
}
void run(int cur) {
queue<int> q;
visited[cur] = true;
q.push(cur);
group[cur] = component;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int x : graph[u]) {
int v = E[x].other(u);
if(visited[v]) {
continue;
}
if(is_bridge[x]) {
component++;
newgraph[component].push_back(group[cur]);
newgraph[group[cur]].push_back(component);
run(v);
}
else {
group[v] = group[cur];
q.push(v);
visited[v] = true;
}
}
}
}
void solve() {
find_bridges();
visited.assign(n + 1, false);
for(int i = 1; i <= n; i++) {
if(!visited[i]) {
component++;
run(i);
}
}
}
};