Advertisement
Josif_tepe

Untitled

Dec 12th, 2022
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <stack>
  5. using namespace std;
  6. const int maxn = 1e5 + 10;
  7. int n, m;
  8. vector<int> graph[maxn];
  9. int discoverable_time[maxn];
  10. int low_link[maxn];
  11. bool visited[maxn];
  12. int current_time = 0;
  13.  
  14. void dfs(int node, int parent = -1) {
  15.     visited[node] = true;
  16.     discoverable_time[node] = current_time;
  17.     low_link[node] = current_time;
  18.     current_time++;
  19.     int children = 0;
  20.     for(int i = 0; i < (int) graph[node].size(); i++) {
  21.         int neighbour = graph[node][i];
  22.         if(neighbour != parent) {
  23.             if(visited[neighbour]) {
  24.                 low_link[node] = min(low_link[node], discoverable_time[neighbour]);
  25.             }
  26.             else {
  27.                 dfs(neighbour, node);
  28.                 low_link[node] = min(low_link[node], low_link[neighbour]);
  29.                 if(parent != -1 and low_link[neighbour] >= discoverable_time[node]) {
  30.                     cout << "Articulation point: " << node + 1 << endl;
  31.                 }
  32.                 children++;
  33.             }
  34.         }
  35.     }
  36.     if(children > 1 and parent == -1) {
  37.         cout << "Articulation point: " << node + 1 << endl;
  38.     }
  39. }
  40. void articulation_points() {
  41.     for(int i = 0; i < maxn; i++) {
  42.         visited[i] = false;
  43.         discoverable_time[i] = -1;
  44.         low_link[i] = -1;
  45.     }
  46.     for(int i = 0; i < n; i++) {
  47.         if(!visited[i]) {
  48.             dfs(i);
  49.         }
  50.     }
  51. }
  52. int main()
  53. {
  54.     cin >> n >> m;
  55.     for(int i = 0; i < m; i++) {
  56.         int a, b;
  57.         cin >> a >> b;
  58.         a--; b--;
  59.         graph[a].push_back(b);
  60.         graph[b].push_back(a);
  61.     }
  62.     articulation_points();
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement