nrk009

Articulation Point

Nov 4th, 2020
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  5. #define ll long long
  6. #define ld long double
  7. #define pb push_back
  8. #define fe first
  9. #define se second
  10. #define nl "\n"
  11. #define pp pair < ll , ll >
  12. #define sz(x) (ll)x.size()
  13. #define st(x) sort(x.begin(),x.end())
  14. #define rst(x) sort(x.rbegin(), x.rend())
  15. #define all(x) x.begin(),x.end()
  16. long double pi = 3.14159265358979323;
  17.  
  18. const double EPS = 1e-12;
  19. const int N = 1e6 + 5;
  20. //Just A Gentle Reminder about MOD
  21. const int mod = 1e9 + 7;
  22.  
  23. //Articulation point
  24.  
  25. //Two Condn
  26. //1 - if u is root node
  27. //it must have two children components
  28.  
  29. //2 - not a root
  30. //there should not a back edge from subgraph of v to ancestor of u
  31.  
  32.  
  33. vector < ll > v[N];
  34. //vector < ll > ans;
  35. int tin[N];
  36. int low[N]; //earliest node which is back edge
  37. int vis[N];
  38. bool Arti[N];
  39. ll par[N];
  40. ll timer;
  41.  
  42. void dfs(ll s , ll timer){
  43.  
  44.     int cnt = 0 ;
  45.     timer++;
  46.     tin[s] = timer;
  47.     low[s] = timer;
  48.     vis[s] = 1;
  49.     for(auto it : v[s]){
  50.  
  51.        
  52.         if(!vis[it]){
  53.             par[it] = s;
  54.             cnt++;
  55.             dfs(it,timer);
  56.             low[s] = min(low[it] , low[s]);
  57.             //For Root Node
  58.             if(s == 1 && cnt > 1){
  59.                 //cout << s << " ";
  60.                 Arti[s] = 1;
  61.             }
  62.             //For Any Other Node
  63.             //if low[it] < tin[s] back edge is present!!
  64.             //condn for no back edge
  65.             else if(s != 1 && low[it] >= tin[s]){
  66.                
  67.                 Arti[s] = 1;
  68.             }
  69.         }
  70.         //FOR BACK EDGE
  71.         else if(it != par[s]){
  72.                 low[s] = min(tin[it] , low[s]);
  73.         }
  74.     }
  75.     //vis[s] = 2;
  76.  
  77. }
  78.  
  79. int main()
  80. {
  81.     fast;
  82.     // ll t;
  83.     // cin >> t ;
  84.     ll n , m;
  85.        
  86.     while(cin >> n >> m && m != 0 && n != 0 ){
  87.        
  88.         for(int i = 0 ; i <= n ;i++ ){
  89.             v[i].clear();
  90.             vis[i] = tin[i] = low[i] = Arti[i] = par[i] = 0;
  91.         }
  92.         timer =  0;
  93.         for(int i = 0 ; i < m ;i++ ){
  94.             ll a , b;
  95.             cin >> a >> b ;
  96.             //cout << a << " " << b << "\n";
  97.             v[a].pb(b);
  98.             v[b].pb(a);
  99.         }    
  100.         for(int i = 1 ; i <= n;  i++ ){
  101.             if(!vis[i]){
  102.                 dfs(i,0);
  103.             }
  104.         }
  105.        
  106.         ll ans = 0 ;
  107.         for(int i  = 1 ; i <= n; i++ ){
  108.             if(Arti[i])ans++ ;
  109.         }
  110.         cout << ans << "\n";
  111.     }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.    
  133.     return 0;
  134. //It's not end it yet
  135. }
Advertisement
Add Comment
Please, Sign In to add comment