-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patharticulation point and bridges.cpp
More file actions
76 lines (56 loc) · 1.13 KB
/
articulation point and bridges.cpp
File metadata and controls
76 lines (56 loc) · 1.13 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
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> gr[N];
int vis[N], disc[N], low[N], tme = 1;
vector<pair<int, int>> bridges;
set<int> arti_points;
void dfs(int cur, int par) {
vis[cur] = 1;
disc[cur] = low[cur] = tme++;
int child = 0;
for (auto x : gr[cur]) {
if (!vis[x]) {
dfs(x, cur);
child++;
// we know low and disc of x
low[cur] = min(low[cur], low[x]);
// bridges
if (low[x] > disc[cur]) {
bridges.push_back({cur, x});
}
// articulation points
if (par != 0 && low[x] >= disc[cur]) {
arti_points.insert(cur);
}
}
else if (x != par) {
// backedge
low[cur] = min(low[cur], disc[x]);
}
}
// root is an arti or not
if (par == 0 && child > 1) {
arti_points.insert(cur);
}
return;
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
gr[x].push_back(y);
gr[y].push_back(x);
}
dfs(1, 0);
for (auto x : arti_points) cout << x << '\n';
for (auto x : bridges) {
cout << x.first << " " << x.second << '\n';
}
return 0;
}