Advertisement
Saleh127

Light OJ 1026 / Articulation point-Bridge

Feb 16th, 2022
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. /***
  2.  created: 2022-02-16-22.31.00
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll int
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. vector< pair<ll,ll> >ans;
  13. ll low[200005],in[200005];
  14. vector<ll>g[200005];
  15. bool v[200005];
  16. ll timer;
  17. void dfs(ll vertex,ll parent)
  18. {
  19.     v[vertex]=1;
  20.  
  21.     low[vertex]=in[vertex]=++timer;
  22.  
  23.     for(auto child: g[vertex])
  24.     {
  25.         if(child==parent) continue;
  26.  
  27.         if(v[child])
  28.         {
  29.             low[vertex]=min(low[vertex],in[child]);
  30.         }
  31.         else
  32.         {
  33.             dfs(child,vertex);
  34.  
  35.             if(low[child]>in[vertex])
  36.             {
  37.                 ans.push_back({min(vertex,child),max(vertex,child)});
  38.             }
  39.             low[vertex]=min(low[vertex],low[child]);
  40.         }
  41.     }
  42.  
  43.     return;
  44.  
  45. }
  46.  
  47. void clr(ll n)
  48. {
  49.     for(ll i=0; i<n+2; i++)
  50.     {
  51.         g[i].clear();
  52.         v[i]=0;
  53.         low[i]=0;
  54.         in[i]=0;
  55.     }
  56.     ans.clear();
  57.     timer=0;
  58. }
  59.  
  60. int main()
  61. {
  62.  
  63.     ios_base::sync_with_stdio(0);
  64.     cin.tie(0);
  65.     cout.tie(0);
  66.  
  67.     ll n,m,i,j,k,l;
  68.  
  69.     test
  70.     {
  71.         cin>>n;
  72.  
  73.         clr(n);
  74.  
  75.         for(ll yy=0; yy<n; yy++)
  76.         {
  77.             char cc;
  78.  
  79.             cin>>m>>cc>>k>>cc;
  80.  
  81.             for(i=0; i<k; i++)
  82.             {
  83.                 cin>>j;
  84.                 g[m].push_back(j);
  85.             }
  86.         }
  87.  
  88.  
  89.         for(i=0; i<n; i++)
  90.         {
  91.             if(v[i]==0)
  92.             {
  93.                 dfs(i,-1);
  94.             }
  95.         }
  96.  
  97.         sort(ans.begin(),ans.end());
  98.  
  99.         cout<<"Case "<<cs<<":"<<nl;
  100.  
  101.         cout<<ans.size()<<" critical links"<<nl;
  102.  
  103.         for(i=0; i<ans.size(); i++)
  104.         {
  105.             cout<<ans[i].first<<" - "<<ans[i].second<<nl;
  106.         }
  107.     }
  108.  
  109.     get_lost_idiot;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement