-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_ArticulationPointII.cpp
61 lines (55 loc) · 1.59 KB
/
GFG_ArticulationPointII.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
/*
Articulation Point - II
https://practice.geeksforgeeks.org/problems/articulation-point2616/1
*/
class Solution {
public:
int timer;
vector<int> dtime;
vector<int> low;
vector<bool> visited;
vector<bool> isArti;
vector<int> articulationPoints(int V, vector<int>adj[]) {
dtime.resize(V, 0);
low.resize(V, 0);
visited = vector<bool>(V, false);
isArti.resize(V, false);
timer = 0;
for(int v=0; v<V; v++)
{
if(visited[v] == 0) // given not necessarily connected
dfs(v, -1, adj);
}
vector<int> ans;
for(int i=0; i<V; i++)
if(isArti[i]) ans.push_back(i);
if(ans.size()==0) return {-1};
return ans;
}
void dfs(int s, int p, vector<int> adj[])
{
dtime[s] = low[s] = timer++;
visited[s] = true;
int children = 0;
for(auto w: adj[s])
{
if(visited[w]== false) // not visited, tree edge,
{
dfs(w, s, adj);
children++;
// cout<<s<<" Tree "<<w<<endl;
low[s] = min(low[s], low[w]);
if(low[w] >= dtime[s] and p != -1) // not root
isArti[s] = true;
}
else if(w != p) // w!=p and visited, that is backedge
{
// cout<<s<<" w!=p "<<w<<endl;
low[s] = min(low[s], dtime[w]);
}
}
// root node
if(p == -1 and children > 1)
isArti[s] = true;
}
};