[Connected graph] Double connected template Tarjan, connected graph template tarjan
Compared to the algorithm used to calculate the nodes for an undirected graph, the algorithm only has one more stack to store all the edges without any nodes. When the nodes are shut down, all the edges are popped up for storage.
Int dfs (int u, int fa) {int lowu = dfn [u] = ++ deep; int son = 0; for (int I = head [u]; ~ I; I = e [I]. next) {int v = e [I]. v; Pair p = Pair (u, v); if (! Dfn [v]) {s. push (p); son ++; int lowv = dfs (v, u); lowu = min (lowu, lowv); if (lowv> = dfn [u]) {iscut [u] = 1; bcc_cnt ++; // bcc [bcc_cnt] numbered from 1. clear (); while (1) {Pair x = s. top (); s. pop (); if (bcc_id [x. u]! = Bcc_cnt) {bcc [bcc_cnt]. PB (x. u); bcc_id [x. u] = bcc_cnt;} if (bcc_id [x. v]! = Bcc_cnt) {bcc [bcc_cnt]. PB (x. v); bcc_id [x. v] = bcc_cnt;} if (x. u = u & x. v = v) break;} // all the sides are displayed for storage, until we met u-> v} else if (dfn [v] <dfn [u] & v! = Fa) {s. push (p); lowu = min (lowu, dfn [v]) ;}} if (fa =-1 & son = 1) iscut [u] = 0; return lowu ;}
If the graph is not connected, add the following:
For (int I = 0; I <n; I ++) {if (! Dfn [I]) dfs (I,-1);} // run tarjan once for each connected graph