Tarjan template (indent, to find strongly connected components of the graph)

Source: Internet
Author: User


Concrete ideas See detailed website: Https://www.byvoid.com/blog/scc-tarjan;

Then the Tarjan template is sorted out, the function code of the specific array is commented.

const int n=100010;struct data{int to,next;} tu[n*2];int head[n];int ip;int dfn[n], low[n];///dfn[] indicates the number of steps in deep search, low[u] means u or u    The subtree can be traced back to the earliest stack of nodes in the sequence number of the int sccno[n];///indent array, which represents a point corresponding to the indentation value of int step;int scc_cnt;///strong connected component number void init () {ip=0; memset (head,-1,sizeof (Head));} void Add (int u,int v) {tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++;}    Vector<int> scc[n];///to get out of the contraction point, scc[i] inside the indentation I this indent specifically which points stack<int> s;void dfs (int u) {dfn[u] = low[u] = ++step;    S.push (U);        for (int i = head[u]; i!=-1; i=tu[i].next) {int v = tu[i].to;            if (!dfn[v]) {DFS (v);        Low[u] = min (Low[u], low[v]);    } else if (!sccno[v]) low[u] = min (Low[u], dfn[v]);        } if (low[u] = = Dfn[u]) {scc_cnt + = 1;        Scc[scc_cnt].clear ();            while (1) {int x = S.top ();            S.pop ();            if (sccno[x]! = scc_cnt) scc[scc_cnt].push_back (x);            SCCNO[X] = scc_cnt;        if (x = = u) break; }    }}void Tarjan (int n) {memset (sccno, 0, sizeof (SCCNO));    memset (DFN, 0, sizeof (DFN));    Step = scc_cnt = 0; for (int i = 1; I <=n; i++) if (!dfn[i]) DFS (i);}


Tarjan template (indent, to find strongly connected components of the 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.