Alex_tz307

Biconnected Components + Articulations + Bridges

Nov 4th, 2020 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("componentebiconexe.in");
  6. ofstream fout("componentebiconexe.out");
  7.  
  8. vector < int > id, low_id;
  9. vector < vector < int > > G, Components;
  10. stack < pair < int , int > > S;
  11. vector < pair < int , int > > bridges;
  12. unordered_set < int > articulations;
  13.  
  14. inline void min_self(int& a, int b) {
  15.     a = min(a, b);
  16. }
  17.  
  18. void BC(int u, int v) {
  19.     vector < int > new_c;
  20.     int x, y;
  21.     do {
  22.         x = S.top().first, y = S.top().second;
  23.         S.pop();
  24.         new_c.emplace_back(x), new_c.emplace_back(y);
  25.     } while(x != u || y != v);
  26.     Components.emplace_back(new_c);
  27. }
  28.  
  29. void DFS(int node, int parent, int val, int root) {
  30.     id[node] = low_id[node] = val;
  31.     int cnt = 0;
  32.     for(int vec : G[node]) {
  33.         if(vec == parent)
  34.             continue;
  35.         if(id[vec] == -1) {
  36.             ++cnt;
  37.             S.emplace(node, vec);
  38.             DFS(vec, node, val + 1, root);
  39.             min_self(low_id[node], low_id[vec]);
  40.             if(low_id[vec] >= id[node]) {
  41.                 if(low_id[vec] > id[node])
  42.                     bridges.emplace_back(node, vec);
  43.                 BC(node, vec);
  44.                 if(node != root)
  45.                     articulations.insert(node);
  46.             }
  47.         }
  48.         else
  49.             min_self(low_id[node], id[vec]);
  50.     }
  51.     if(node == root && cnt > 1)
  52.         articulations.insert(node);
  53. }
  54.  
  55. int main() {
  56.     fin.sync_with_stdio(false);
  57.     fout.sync_with_stdio(false);
  58.     fin.tie(nullptr);
  59.     fout.tie(nullptr);
  60.     int task, N, M;
  61.     fin >> task >> N >> M;
  62.     G.resize(N + 1), id.assign(N + 1, -1), low_id.resize(N + 1);
  63.     while(M--) {
  64.         int u, v;
  65.         fin >> u >> v;
  66.         G[u].emplace_back(v);
  67.         G[v].emplace_back(u);
  68.     }
  69.     for(int node = 1; node <= N; ++node)
  70.         if(id[node] == -1)
  71.             DFS(node, 0, 0, node);
  72.     if(task == 1) {
  73.         fout << Components.size() << '\n';
  74.         for(auto C : Components) {
  75.             sort(C.begin(), C.end());
  76.             C.erase(unique(C.begin(), C.end()), C.end());
  77.             fout << C.size() << ' ';
  78.             for(int node : C)
  79.                 fout << node << ' ';
  80.             fout << '\n';
  81.         }
  82.     }
  83.     else
  84.         if(task == 2) {
  85.             fout << articulations.size() << '\n';
  86.             for(int node : articulations)
  87.                 fout << node << ' ';
  88.         }
  89.     else {
  90.         fout << bridges.size() << '\n';
  91.         for(auto b : bridges)
  92.             fout << b.first << ' ' << b.second << '\n';
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment