Compared to the algorithm of finding the joint point of the graph, there is only one stack, which is used to store all the sides that do not have the joint point, and all the edges are ejected after encountering the joint point.
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++;//starting from 1 numbered bcc[bcc_cnt].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; }//eject all sides for storage until 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 diagram is not connected, add this:
for (int i = 0; i< N; i++) { if (!dfn[i]) DFS (i,-1); } Run once for each connected graph Tarjan
"Connected graph" Double connected template Tarjan