-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy patharticulationpoints.cpp
More file actions
69 lines (62 loc) · 1.73 KB
/
articulationpoints.cpp
File metadata and controls
69 lines (62 loc) · 1.73 KB
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
//this code can be used to find Articulation points in a Graph
#include <bits/stdc++.h>
using namespace std;
int timee;// a variable to keep track of the time at which a particular vertex is encounterd
vector<int> graph[100];//adjacency list for the graph (100 is the max number of vertices.. you can change as per your needs)
bool visited[100] , AP[100];
//visited - bool array used in DFS to know whether the node is visited or not
//AP - bool array to tell whether ith vertex is a AP or NOT
int disc[100] , low[100] , parent[100];
//disc - discovery time of a vertex
//low - exit time of a source
//parent - array to tell the parent of ith vertex
//finding all APs
void printAP2(int source){
visited[source] = true;
disc[source] = timee++;
low[source] = disc[source];
int child = 0;
for(auto i : graph[source]){
if(!visited[i]){
child++;
parent[i] = source;
printAP2(i);
low[source] = min(low[i] , low[source]);
if(parent[source] == source){
if(child > 1) AP[source] = true;
}
else{
if(low[i] >= disc[source]) AP[source] = true; // IMP
}
}
else if(i != parent[source]) low[source] = min(low[i] , low[source]);
}
}
//find and print all APs
void printAP(int n , int source){
for(int i = 0 ; i < n ; i++) low[i] = INT_MAX;
parent[source] = source; //initializing source os source vertex to itself
printAP2(source);
for(int i = 0 ; i < n ; i++){
if(!visited[i]){
parent[i] = i;
printAP2(i);
}
}
for(int i = 0 ; i < n ; i++){
if(AP[i]) cout << i << " ";
}
cout << endl;
}
int main(){
//n - no. of vertices , e - no. of edges
int n , e , u , v;
cin >> n >> e;
//inputting e edges
for(int i = 0 ; i < e ; i++){
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
printAP(n , 0);
}