ec1117

Untitled

Oct 24th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. /**
  2.  * Description: Biconnected components of edges. Removing any vertex in BCC
  3.     * doesn't disconnect it. To get block-cut tree, create a bipartite graph
  4.     * with the original vertices on the left and a vertex for each BCC on the right.
  5.     * Draw edge $u\leftrightarrow v$ if $u$ is contained within the BCC for $v$.
  6.     * Self-loops are not included in any BCC while BCCS of size 1 represent
  7.     * bridges.
  8.  * Time: O(N+M)
  9.  * Source: GeeksForGeeks (corrected)
  10.  * Verification:
  11.     * USACO December 2017, Push a Box -> https://pastebin.com/yUWuzTH8
  12.     * https://cses.fi/problemset/task/1705/
  13.  */
  14.  
  15. struct BCC {
  16.     vector<vpi> adj; vpi ed;
  17.     vector<vi> comps; // edges for each bcc
  18.     int N, ti = 0; vi disc, st;
  19.     void init(int _N) { N = _N; disc.rsz(N), adj.rsz(N); }
  20.     void ae(int x, int y) {
  21.         adj[x].eb(y,sz(ed)), adj[y].eb(x,sz(ed)), ed.eb(x,y); }
  22.     int dfs(int x, int p = -1) { // return lowest disc
  23.         int low = disc[x] = ++ti;
  24.         trav(e,adj[x]) if (e.s != p) {
  25.             if (!disc[e.f]) {
  26.                 st.pb(e.s); // disc[x] < LOW -> bridge
  27.                 int LOW = dfs(e.f,e.s); ckmin(low,LOW);
  28.                 if (disc[x] <= LOW) { // get edges in bcc
  29.                     comps.eb(); vi& tmp = comps.bk; // new bcc
  30.                     for (int y = -1; y != e.s; )
  31.                         tmp.pb(y = st.bk), st.pop_back();
  32.                 }
  33.             } else if (disc[e.f] < disc[x]) // back-edge
  34.                 ckmin(low,disc[e.f]), st.pb(e.s);
  35.         }
  36.         return low;
  37.     }
  38.     void gen() { F0R(i,N) if (!disc[i]) dfs(i);  }
  39. };
Add Comment
Please, Sign In to add comment