-
Notifications
You must be signed in to change notification settings - Fork 0
/
Articulation Point - I.cpp
40 lines (40 loc) · 1.16 KB
/
Articulation Point - I.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
void dfs(int node,int parent,vector<int>&vis,vector<int>&tin,vector<int>&low,int &timer,vector<int>adj[],vector<int>&arti){
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,arti);
low[node]=min(low[node],low[it]);
child++;
if(low[it]>=tin[node]&&parent!=-1){
arti[node]=1;
}
}
else{
low[node]=min(low[node],tin[it]);
}
}
if(parent==-1 &&child>1){
arti[node]=1;
}
}
vector<int> articulationPoints(int V, vector<int>adj[]) {
vector<int>ans;
vector<int>vis(V,0);
vector<int>tin(V,-1);
vector<int>low(V,-1);
vector<int>arti(V,0);
int timer=0;
for(int i=0;i<V;i++){
if(!vis[i]){
dfs(i,-1,vis,tin,low,timer,adj,arti);
}
}
for(int i=0;i<V;i++){
if(arti[i]==1) ans.push_back(i);
}
if(ans.size()==0) return {-1};
return ans;
}