-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbridges_articulation_points.cpp
72 lines (63 loc) · 1.9 KB
/
bridges_articulation_points.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
// https://codeforces.com/blog/entry/71146
// https://leetcode.com/problems/critical-connections-in-a-network/submissions/
// https://pastebin.com/7MWuxdLQ
// https://pastebin.com/ATwq4mJa
// https://codeforces.com/blog/entry/68138 (best)
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> articulationPoints;
vector<vector<int>> bridges;
vector<vector<int>> g;
// Earliest ancestor I can reach by using a back edge either directly
// or by going down the tree and finding a back edge
vector<int> lowestAncestorTime;
vector<int> discoveryTime;
int currentTime = 0;
void dfs(int u, int parent = -1) {
if (discoveryTime[u])
return; // Already processed
discoveryTime[u] = lowestAncestorTime[u] = ++currentTime;
bool isArticulationVertex = false;
int children = 0;
for (int v : g[u]) {
if (v == parent)
continue;
if (!discoveryTime[v]) { // Is Child
children++;
dfs(v, u);
lowestAncestorTime[u] = min(lowestAncestorTime[u], lowestAncestorTime[v]);
if (lowestAncestorTime[v] > discoveryTime[u]) {
// Cannot reach me or above in alternate ways, so this is a bridge
vector<int> t = {u, v};
sort(t.begin(), t.end());
bridges.push_back(t);
}
if (lowestAncestorTime[v] >= discoveryTime[u]) {
// Cannot reach above me in alternate ways, so this is an articulation vertex
// Except if I am a leaf node
isArticulationVertex = true;
}
} else {
lowestAncestorTime[u] = min(lowestAncestorTime[u], discoveryTime[v]);
}
}
if (isArticulationVertex) {
if (parent != -1 || children > 1)
articulationPoints.push_back(u);
}
}
int main() {
int n, m;
cin >> n >> m;
g.resize(n);
lowestAncestorTime.resize(n);
discoveryTime.resize(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
}