Advertisement
a_pramanik

articulation points

Jan 24th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. ///:-)
  2. #include <bits/stdc++.h>
  3. #define pb push_back
  4. #define ll long long int
  5. #define inf 2000000000
  6. #define infLL 9000000000000000000
  7. #define infULL 18446744073709551615
  8. #define Aktaruzzaman using
  9. #define pi (2.0*acos(0.0))
  10. #define Pramanik namespace std;
  11. #define vsort(v)   sort(v.begin(),v.end())
  12. #define ull unsigned long long int
  13. #define mem(a, b) memset(a, b, sizeof a)
  14. #define cf 100009
  15. #define MOD 1000000007
  16. #define pii pair<int, int>
  17.  
  18. //int dx[]={0,1,0,-1};
  19. //int dy[]={1,0,-1,0};
  20. //int dx[]={1,1,0,-1,-1,-1,0,1};
  21. //int dy[]={0,1,1,1,0,-1,-1,-1};
  22.  
  23. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  24. template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  25. template <class T> inline T is_prime(T a){if(a<2|a==4)return false;if(a==2||a==3||a==5)return true; T g=(T)sqrt(a)+1; for(T i=2; i<=g; i++){if(a%i==0)return false;}return false;}
  26. template <class T> inline T bigmod(T n,T p,T m){if(p==0)return 1; if(p%2)return ((n%m)*bigmod(n,p-1,m))%m; else {T c=bigmod(n,p/2,m);return ((c%m)*(c%m))%m;}}
  27.  
  28. Aktaruzzaman Pramanik
  29.  
  30. vector<int>a[1000], ans;
  31. int disc[1001], low[1001], vis[1001], par[1001];
  32. int discTime;
  33.  
  34. void dfs(int x){
  35.  
  36.     ++discTime;
  37.     disc[x]=discTime;
  38.     low[x]=discTime;
  39.     int children=0;
  40.     vis[x]++;
  41.  
  42.     for(int i=0; i<a[x].size(); i++){
  43.         int y = a[x][i];
  44.         if(vis[y]==-1){
  45.  
  46.             children++;
  47.             par[y]=x;
  48.             dfs(y);
  49.             low[x]=min(low[x], low[y]);
  50.  
  51.             if(children>1 && par[x]==-1){
  52.                 ans.pb(x);
  53.             }
  54.             if(par[x]!=-1 && low[y]>=disc[x])ans.pb(x);
  55.  
  56.         }
  57.  
  58.         else if(y!=par[x]){
  59.             low[x]=min(low[x], disc[y]);
  60.         }
  61.     }
  62.  
  63. }
  64.  
  65. int main()
  66. {
  67.     //freopen("input.txt","r",stdin)
  68.     //freopen("output.txt","w",stdout)
  69.  
  70.     int n, m, i, j, k, x, y;
  71.  
  72.     scanf("%d%d", &n, &m);
  73.  
  74.     for(i=1; i<=m; i++){
  75.         scanf("%d%d", &x, &y);
  76.         a[x].pb(y);
  77.         a[y].pb(x);
  78.     }
  79.  
  80.     for(i=1; i<=n; i++){
  81.         par[i]=-1;
  82.         vis[i]=-1;
  83.         disc[i]=0;
  84.         low[i]=0;
  85.     }
  86.     discTime=0;
  87.     for(i=1; i<=n; i++){
  88.         if(vis[i]==-1)dfs(i);
  89.     }
  90.  
  91.     printf("Articulation points are:\n");
  92.     for(i=0; i<ans.size(); i++)printf("%d ",ans[i]);
  93.     printf("\n");
  94.     ans.clear();
  95.     for(i=1; i<=n; i++)a[i].clear();
  96.     cout<<"OK"<<endl;
  97.     return main();
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement