forked from striver79/StriversGraphSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
articulationCpp
53 lines (48 loc) · 1.29 KB
/
articulationCpp
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
#include <bits/stdc++.h>
using namespace std;
void dfs(int node, int parent, vector<int> &vis, vector<int> &tin, vector<int> &low, int &timer, vector<int> adj[], vector<int> &isArticulation) {
vis[node] = 1;
tin[node] = low[node] = timer++;
int child = 0;
for(auto it: adj[node]) {
if(it == parent) continue;
if(!vis[it]) {
dfs(it, node, vis, tin, low, timer, adj, isArticulation);
low[node] = min(low[node], low[it]);
child++;
if(low[it] >= tin[node] && parent != -1) {
isArticulation[node] = 1;
}
} else {
low[node] = min(low[node], tin[it]);
}
}
if(parent == -1 && child > 1) {
isArticulation[node] = 1;
}
}
int main() {
int n, m;
cin >> n >> m;
vector<int> adj[n];
for(int i = 0;i<m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> tin(n, -1);
vector<int> low(n, -1);
vector<int> vis(n, 0);
vector<int> isArticulation(n, 0);
int timer = 0;
for(int i = 0;i<n;i++) {
if(!vis[i]) {
dfs(i, -1, vis, tin, low, timer, adj, isArticulation);
}
}
for(int i = 0;i<n;i++) {
if(isArticulation[i] == 1) cout << i << endl;
}
return 0;
}