Maruf_Hasan

ArticulationPointandBridge

Jan 7th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define pb push_back
  6. #define ll long long
  7. #define pii pair<int,int>
  8. #define pll pair<ll,ll>
  9. #define M 100007
  10. #define INF 1e9
  11. #define INFL 1e18
  12. #define PI acos(-1)
  13. #define mp make_pair
  14. #define fast_in_out ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  15.  
  16.  
  17.  
  18. vector<int>adj[M];
  19.  
  20.  
  21.  
  22. int parent[M];
  23. int low[M];
  24. int d[M];
  25. int visited[M];
  26. bool isArticulationPoint[M];
  27. int Time=0;
  28. bool mark[M];
  29. vector<pair<int,int> >ans;
  30. void DFS(int u,int root)
  31. {
  32. Time=Time+1;
  33. visited[u]=1;
  34. d[u]=low[u]=Time;
  35. int noOfChildren=0;
  36. for(int i=0;i<adj[u].size();i++)
  37. {
  38. int v=adj[u][i];
  39. if(v==parent[u])
  40. continue;
  41.  
  42. if(visited[v])
  43. {
  44. low[u]=min(low[u],d[v]);
  45. }
  46. else
  47. {
  48. parent[u]=v;
  49.  
  50. DFS(v,root);
  51. low[u]=min(low[u],low[v]);
  52. if(low[v]>=d[u] && u!=root)
  53. {
  54. isArticulationPoint[u]=true;
  55. ans.pb(mp(u,v));
  56. }
  57. noOfChildren=noOfChildren+1;
  58. }
  59. if(u==root && noOfChildren>1)
  60. {
  61. isArticulationPoint[u]=true;
  62. ans.pb(mp(u,adj[u][0]));
  63. }
  64. }
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. int main()
  72. {
  73. int n;
  74. cin>>n;
  75. for(int i=0;i<n;i++)
  76. {
  77. int x,y;
  78. cin>>x>>y;
  79. adj[x].pb(y);
  80. adj[y].pb(x);
  81. }
  82. DFS(1,1);
  83. for(int i=1;i<=n;i++)
  84. {
  85. if(isArticulationPoint[i])
  86. cout<<i<<endl;
  87. //cout<<ans[i].first<<" "<<ans[i].second<<endl;
  88. }
  89.  
  90. return 0;
  91. }
  92. //7
  93. //1 2
  94. //1 3
  95. //3 4
  96. //3 7
  97. //4 5
  98. //4 6
  99. //6 7
  100. //
Advertisement
Add Comment
Please, Sign In to add comment