nrk009

Bridges

Nov 4th, 2020
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 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. vector  < pair < int , int > > ans;
  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.            
  58.             //if low[it] < tin[s] back edge is present!!
  59.             //condn for no back edge
  60.             if(low[it] > tin[s]){
  61.                
  62.                 ans.push_back({s,it});
  63.             }
  64.         }
  65.         //FOR BACK EDGE
  66.         else if(it != par[s]){
  67.                 low[s] = min(tin[it] , low[s]);
  68.         }
  69.     }
  70.     //vis[s] = 2;
  71.  
  72. }
  73.  
  74. int main()
  75. {
  76.     fast;
  77.     ll t;
  78.     cin >> t ;
  79.     ll n , m;
  80.        
  81.     while(t--){
  82.         cin >> n >> m ;
  83.         for(int i = 0 ; i <= n ;i++ ){
  84.             v[i].clear();
  85.             vis[i] = tin[i] = low[i] = par[i] = 0;
  86.         }
  87.         ans.clear();
  88.         timer =  0;
  89.         for(int i = 0 ; i < m ;i++ ){
  90.             ll a , b;
  91.             cin >> a >> b ;
  92.             //cout << a << " " << b << "\n";
  93.             v[a].pb(b);
  94.             v[b].pb(a);
  95.         }    
  96.         // for(int i = 1 ; i <= n;  i++ ){
  97.         //  if(!vis[i]){
  98.         //      dfs(i,0);
  99.         //  }
  100.         // }
  101.         dfs(1,0);
  102.         for(auto it : ans){
  103.             cout << it.first << " " << it.second << " ";
  104.         }
  105.     }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.    
  127.     return 0;
  128. //It's not end it yet
  129. }
Advertisement
Add Comment
Please, Sign In to add comment