-
Notifications
You must be signed in to change notification settings - Fork 1
/
12 December Articulation Point - I
69 lines (43 loc) · 1.61 KB
/
12 December Articulation Point - I
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
class Solution {
public:
void dfs(int node, int parent, int& timer, vector<int> adj[], vector<int>& vis, vector<int>& tin, vector<int>& low, vector<int>& isarti){
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, timer, adj, vis, tin, low, isarti);
low[node] = min(low[node], low[it]);
if(low[it] >= tin[node] && parent != -1){
isarti[node] = 1;
}
child++;
}
else{
low[node] = min(low[node], tin[it]);
}
}
if(parent == -1 && child > 1)
isarti[node] = 1;
}
vector<int> articulationPoints(int V, vector<int>adj[]) {
// Code here
vector<int> res;
vector<int> vis(V, 0);
vector<int> tin(V, -1);
vector<int> low(V, -1);
vector<int> isarti(V, 0);
int timer = 0;
for(int i = 0; i < V; i++){
if(!vis[i]){
dfs(i, -1, timer, adj, vis, tin, low, isarti);
}
}
for(int i = 0; i < V; i++){
if(isarti[i]) res.push_back(i);
}
if(res.size() == 0) res.push_back(-1);
return res;
}
};