Advertisement
Masfiq_ds_algo

ALGR: Articulation Node

Apr 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #define MAX_NODE 10000
  5. #define NIL 99999
  6. using namespace std;
  7.  
  8. vector<bool> visited(MAX_NODE,false);
  9. vector<bool> Articulations_pont(MAX_NODE);
  10. vector<int> parent(MAX_NODE,-1);
  11. int time=0;
  12. vector<int> d(MAX_NODE,0);
  13. vector<int> low(MAX_NODE,0);
  14.  
  15.  
  16. void DFS_Visit(vector<vector<int>> gr, int u) // taking reference to see the changes in color
  17. {
  18.     time++;
  19.     d[u]=time;//cout<<"start time of "<<u<<" is "<<d[u]<<endl;
  20.     low[u]=d[u];
  21.     visited[u]=true;
  22.     int num_of_childeren_visited=0;
  23.     for(int i=0; i<gr[u].size(); i++)
  24.     {
  25.  
  26.         int v=gr[u][i];
  27.         bool flag = false;
  28.         if(v==parent[u])
  29.         {
  30.             ///go to line (what??)
  31.             flag = true;
  32.         }
  33.         if(!flag)
  34.         {
  35.             if(visited[v]==true)
  36.             {
  37.                 low[u]=min(low[u],d[v]);
  38.             }
  39.             if(!visited[v])
  40.             {
  41.                 parent[v]=u;
  42.                 DFS_Visit(gr,v);
  43.                 low[u]=min(low[u],low[v]);
  44.  
  45.                 if(d[u]<=low[v] && parent[u]!=-1)// when u is root, parent[u]=-1
  46.                     Articulations_pont[u]=true;
  47.                 num_of_childeren_visited++;
  48.             }
  49.             if(num_of_childeren_visited>1 && parent[u]==-1)
  50.                 Articulations_pont[u]=true;
  51.         }
  52.     }//end for
  53. }
  54. void DFS(vector<vector<int>> gr)
  55. {
  56.     int nv = gr.size() - 1;
  57. //    for(int i=1;i<=nv;i++){
  58. //        visited[i]=false;
  59. //        parent[i]=-1;
  60. //        d[i]=0;
  61. //        low[i]=0;
  62. //    }
  63.     //time=0;
  64.     for(int i=1; i<=nv; i++)
  65.         if(!visited[i])
  66.             DFS_Visit(gr,i);
  67. }
  68.  
  69. int main()
  70. {
  71.     int n, e;
  72.     scanf("%d %d", &n, &e);
  73.     vector<vector<int>> gr;
  74.     gr.clear();
  75.     gr.resize(n+1);
  76.     for(int i=0; i<e; i++)
  77.     {
  78.         int u,v;
  79.         scanf("%d %d", &u, &v);
  80.         gr[u].push_back(v);
  81.         gr[v].push_back(u);
  82.     }
  83.  
  84.     DFS(gr);
  85.     printf("Articulation points are :\n");
  86.     for(int i=1; i<n; i++)
  87.         if(Articulations_pont[i]==true)
  88.             cout<<i<<endl;
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement