[Connected graph] Double connected template Tarjan, connected graph template tarjan

Source: Internet
Author: User

[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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.