-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathBCC.cpp
167 lines (150 loc) · 4.03 KB
/
BCC.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
struct MagicComponents {
struct edge {
ll u, v, id;
};
ll num, n, edges;
vector<ll> dfs_num, low, vis;
vector<ll> cuts; // art-vertices
vector<edge> bridges; // bridge-edges
vector<vector<edge>> adj; // graph
vector<vector<edge>> bccs; // all the bccs where bcc[i] has all the edges inside it
deque<edge> e_stack;
// Nodes are numberd from 0
MagicComponents(const ll& _n) : n(_n) {
adj.assign(n, vector<edge>());
edges = 0;
}
void add_edge(const ll& u, const ll& v) {
adj[u].push_back({u,v,edges});
adj[v].push_back({v,u,edges++});
}
void run(void) {
vis.assign(n, 0);
dfs_num.assign(n, 0);
low.assign(n, 0);
bridges.clear();
cuts.clear();
bccs.clear();
e_stack = deque<edge>();
num = 0;
for (ll i = 0; i < n; ++i) {
if (vis[i]) continue;
dfs(i, -1);
}
}
void dfs(const ll& node, const ll& par) {
dfs_num[node] = low[node] = num++;
vis[node] = 1;
ll n_child = 0;
for (edge& e : adj[node]) {
if (e.v == par) continue;
if (vis[e.v] == 0) {
++n_child;
e_stack.push_back(e);
dfs(e.v, node);
low[node] = min(low[node], low[e.v]);
if (low[e.v] >= dfs_num[node]) {
if (dfs_num[node] > 0 || n_child > 1)
cuts.push_back(node);
if (low[e.v] > dfs_num[node]) {
bridges.push_back(e);
pop(node);
} else pop(node);
}
} else if (vis[e.v] == 1) {
low[node] = min(low[node], dfs_num[e.v]);
e_stack.push_back(e);
}
}
vis[node] = 2;
}
void pop(const ll& u) {
vector<edge> list;
for (;;) {
edge e = e_stack.back();
e_stack.pop_back();
list.push_back(e);
if (e.u == u) break;
}
bccs.push_back(list);
}
//# Make sure to call run before calling this function.
// Function returns a new graph such that all two connected
// components are compressed into one node and all bridges
// in the previous graph are the only edges connecting the
// components in the new tree.
// map is an integer array that will store the mapping
// for each node in the old graph into the new graph. //$
MagicComponents component_tree(vector<ll>& map) {
vector<char> vis(edges);
for (const edge& e : bridges)
vis[e.id] = true;
ll num_comp = 0;
map.assign(map.size(), -1);
for (ll i = 0; i < n; ++i) {
if (map[i] == -1) {
deque<ll> q;
q.push_back(i);
map[i] = num_comp;
while (!q.empty()) {
ll node = q.front();
q.pop_front();
for (const edge& e : adj[node]) {
if (!vis[e.id] && map[e.v] == -1) {
vis[e.id] = true;
map[e.v] = num_comp;
q.push_back(e.v);
}
}
}
}
++num_comp;
}
MagicComponents g(num_comp);
vis.assign(vis.size(), false);
for (ll i = 0; i < n; ++i) {
for (const edge& e : adj[i]) {
if (!vis[e.id] && map[e.v] < map[e.u]) {
vis[e.id] = true;
// This is an edge in the bridge tree
// we can add this edge to a new graph[] and this will
// be our new tree. We can now do operations on this tree
g.add_edge(map[e.v], map[e.u]);
}
}
}
return g;
}
//# Make sure to call run before calling this function.
// Function returns a new graph such that all biconnected
// components are compressed into one node. Cut nodes will
// be in multiple components, so these nodes will also have
// their own component by themselves. Edges in the graph
// represent components to articulation points
// map is an integer array that will store the mapping
// for each node in the old graph into the new graph.
// Cut points to their special component, and every other node
// to their specific component. //$
MagicComponents bcc_tree(vector<ll>& map) {
vector<ll> cut(n, -1);
ll size = bccs.size();
for (const auto& i : cuts)
map[i] = cut[i] = size++;
MagicComponents g(size);
vector<ll> used(n);
for (ll i = 0; i < bccs.size(); ++i) {
for (const edge& e : bccs[i]) {
vector<ll> tmp = {e.u,e.v};
for (const ll& node : tmp) {
if (used[node] != i+1) {
used[node] = i+1;
if (cut[node] != -1)
g.add_edge(i, cut[node]);
else map[node] = i;
}
}
}
}
return g;
}
};