-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathArticulationPoint.cpp
54 lines (43 loc) · 1.53 KB
/
ArticulationPoint.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
// https://practice.geeksforgeeks.org/problems/articulation-point-1/1
class Solution {
public:
int timer= 1;
void dfs(int node, int parent, vector<int> adj[], vector<bool> &visited, int timeOfInsertion[],
int low[], set<int> &articulationPoint){
visited[node]= true;
timeOfInsertion[node]= low[node]= timer++;
int child= 0;
for(auto i: adj[node]){
if(i == parent)
continue;
else if(!visited[i]){
dfs(i, node, adj, visited, timeOfInsertion, low, articulationPoint);
low[node]= min(low[node], low[i]);
// denotes that bridge is present
if(low[i]>=timeOfInsertion[node] && parent!=-1)
articulationPoint.insert(node);
child++;
}else{
low[node]= min(low[node], low[i]);
}
}
if(parent==-1 && child>1)
articulationPoint.insert(node);
}
vector<int> articulationPoints(int V, vector<int>adj[]) {
// Code here
vector<bool> visited(V, false);
int timeOfInsertion[V];
int low[V];
set<int> articulationPoint;
for(int i=0; i<V; i++){
if(!visited[i]){
dfs(i, -1, adj, visited, timeOfInsertion, low, articulationPoint);
}
}
vector<int> ans(articulationPoint.begin(), articulationPoint.end());
if(ans.size()==0)
ans.push_back(-1);
return ans;
}
};