Advertisement
Abrar_Al_Samit

2-vertex-connected-component (BCC)

Feb 23rd, 2023
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. const int nax = 1e5 + 2;
  2. vector<int>tree[nax * 2];
  3. int n, m;
  4. int sub[nax * 2];
  5. long long ans = 0;
  6.  
  7. void ad(int u, int v) {
  8.   tree[u].push_back(v);
  9.   tree[v].push_back(u);
  10. }
  11. template<int SZ> struct BCC {
  12.   vector<int>adj[SZ];
  13.   int N;
  14.   int pre_order[SZ], low[SZ], par[SZ];
  15.   int compsz[SZ];
  16.   int ti = 0;
  17.  
  18.   vector<pair<int,int>>st;
  19.   vector<vector<pair<int,int>>>fin;
  20.  
  21.   void addEdge(int u, int v) {
  22.     adj[u].push_back(v);
  23.     adj[v].push_back(u);
  24.   }
  25.  
  26.   void BCCutil(int u, bool root = 0) {
  27.     pre_order[u] = low[u] = ti++;
  28.     int child = 0;
  29.  
  30.     for(int i : adj[u]) if(i!=par[u]) {
  31.       if(pre_order[i]==-1) {
  32.         ++child, par[i] = u;
  33.         st.emplace_back(u, i);
  34.         BCCutil(i);
  35.         low[u] = min(low[u], low[i]);
  36.  
  37.         if((root && child>1) || (!root && low[i]>=pre_order[u])) {// cut vertex
  38.           int l = fin.size();
  39.           fin.emplace_back();
  40.           while(1) {
  41.             fin[l].push_back(st.back());
  42.             st.pop_back();
  43.             if(fin[l].back()==make_pair(u, i)) break;
  44.           }
  45.         }
  46.  
  47.       } else if(pre_order[i] < pre_order[u]) {
  48.         low[u] = min(low[u], pre_order[i]);
  49.         st.emplace_back(u, i);
  50.       }
  51.     }
  52.   }
  53.  
  54.   void bcc() {
  55.     for(int i=1; i<=N; ++i) {
  56.       pre_order[i] = par[i] = low[i] = -1;
  57.     }
  58.     for(int i=1; i<=N; ++i) {
  59.       if(pre_order[i]==-1) {
  60.         BCCutil(i, 1);
  61.         if(!st.empty()) fin.push_back(st);
  62.         st.clear();
  63.       }
  64.     }
  65.  
  66.  
  67.     int co = 0;
  68.     for(auto a : fin) {
  69.       set<int>s;
  70.       for(auto b : a) {
  71.         s.insert(b.first), s.insert(b.second);
  72.       }
  73.       ++co;
  74.       compsz[co] = s.size();
  75.       for(int i : s) ad(i, co+N);
  76.     }
  77.   }
  78. };
  79. BCC<nax>a;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement