-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiconnectedComponent.cpp
More file actions
43 lines (43 loc) · 1.77 KB
/
BiconnectedComponent.cpp
File metadata and controls
43 lines (43 loc) · 1.77 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
struct BiconnectedComponent {
vector<int> ord, low;
vector<bool> used;
vector<vector<int>> g;
vector<vector<pair<int, int>>> bc;
vector<pair<int, int>> tmp;
int n, k = 0;
BiconnectedComponent(const vector<vector<int>> &g) : g(g) {
n = g.size();
ord.resize(n, -1);
low.resize(n, -1);
used.resize(n, false);
}
void dfs(int u, int prev) {
used[u] = true;
ord[u] = k ++;
low[u] = ord[u];
int cnt = 0;
for (auto v : g[u]) if (v != prev) {
if (ord[v] < ord[u]) {
tmp.emplace_back(min(u, v), max(u, v));
}
if (!used[v]) {
cnt ++;
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= ord[u]) {
bc.push_back({});
while (true) {
pair<int, int> e = tmp.back();
bc.back().emplace_back(e);
tmp.pop_back();
if (min(u, v) == e.first && max(u, v) == e.second) {
break;
}
}
}
} else {
low[u] = min(low[u], ord[v]);
}
}
}
};