|
| 1 | +//this code can be used to find Articulation points(APs) and bridges in a Graph |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +int timee;// a variable to keep track of the time at which a particular vertex is encounterd |
| 6 | +vector<int> graph[10001];//adjacency list for the graph (10000 is the max number of vertices.. you can change as per your needs) |
| 7 | +bool visited[10001] , AP[10001]; |
| 8 | +//visited - bool array used in DFS to know whether the node is visited or not |
| 9 | +//AP - bool array to tell whether ith vertex is a AP or NOT |
| 10 | + |
| 11 | +int disc[10001] , low[10001] , parent[10001]; |
| 12 | +//disc - discovery time of a vertex |
| 13 | +//low - exit time of a source |
| 14 | +//parent - array to tell the parent of ith vertex |
| 15 | + |
| 16 | +//function to find APs and Bridges |
| 17 | +void printAP2(int source , vector<int> &Ap , vector<pair<int , int> > &bridges){ |
| 18 | + visited[source] = true; |
| 19 | + disc[source] = timee++; |
| 20 | + low[source] = disc[source]; |
| 21 | + int child = 0; |
| 22 | + for(auto i : graph[source]){ |
| 23 | + if(!visited[i]){ |
| 24 | + child++; |
| 25 | + parent[i] = source; |
| 26 | + printAP2(i , Ap , bridges); |
| 27 | + low[source] = min(low[i] , low[source]); |
| 28 | + |
| 29 | + if(parent[source] == source){ |
| 30 | + if(child > 1) Ap.push_back(source); |
| 31 | + } |
| 32 | + else{ |
| 33 | + if(low[i] >= disc[source]) Ap.push_back(source); // IMP |
| 34 | + } |
| 35 | + |
| 36 | + if(low[i] > disc[source]){ |
| 37 | + pair<int , int> temp; |
| 38 | + temp.first = source; |
| 39 | + temp.second = i; |
| 40 | + bridges.push_back(temp); |
| 41 | + } |
| 42 | + } |
| 43 | + else if(i != parent[source]) low[source] = min(low[source] , disc[i]); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void printAP(int n , int source){ |
| 48 | + vector<int> Ap; |
| 49 | + vector<pair<int , int> > bridges; //contains edges which are bridges |
| 50 | + parent[source] = source; |
| 51 | + printAP2(source , Ap , bridges); |
| 52 | + for(int i = 0 ; i < n ; i++){ |
| 53 | + if(!visited[i]){ |
| 54 | + parent[i] = i; |
| 55 | + printAP2(i , Ap , bridges); |
| 56 | + } |
| 57 | + } |
| 58 | + //print number of APs and Bridges |
| 59 | + //printing the APS and Bridges |
| 60 | + sort(Ap.begin() , Ap.end()); |
| 61 | + sort(bridges.begin() , bridges.end()); |
| 62 | + cout << Ap.size() << endl; |
| 63 | + for(int i = 0 ; i < Ap.size() ; i++){ |
| 64 | + cout << Ap[i] << " "; |
| 65 | + } |
| 66 | + cout << endl; |
| 67 | + |
| 68 | + cout << bridges.size() << endl; |
| 69 | + for(int i = 0 ; i < bridges.size() ; i++){ |
| 70 | + cout << bridges[i].first << " " << bridges[i].second << endl; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +int main(){ |
| 75 | + //n - no. of vertices , e - no. of edges |
| 76 | + int n , e , u , v; |
| 77 | + cin >> n >> e; |
| 78 | + //inputting e edges |
| 79 | + for(int i = 0 ; i < e ; i++){ |
| 80 | + cin >> u >> v; |
| 81 | + graph[u].push_back(v); |
| 82 | + graph[v].push_back(u); |
| 83 | + } |
| 84 | + printAP(n , 0); |
| 85 | +} |
0 commit comments