Dual connected components of undirected graphs (tarjan template)

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Stack> #include <vector>using namespace std; #define MAXN 7500#define inf 0x3f3f3f3fint n,m;int G[MAXN][MAXN]; int Clock;int low[maxn],pre[maxn];stack<int>s;int bc;vector<int>bcc[maxn];int dfs (int u,int fa) {low[u]=p     Re[u]=++clock;     S.push (U);        for (int v=1;v<=n;v++) {if (!g[u][v]) continue;            if (!pre[v]) {int Lowv=dfs (V,U);            Low[u]=min (LOW[U],LOWV);                if (Lowv>=pre[u]) {bc++;                Bcc[bc].clear ();                int tmp=-1;                    while (!s.empty ()) {tmp=s.top ();                    S.pop ();                    Bcc[bc].push_back (TMP);                if (tmp==u) break;  } if (tmp!=-1) S.push (TMP); Cut the top to add back, arbitrarily cut the top is at least two different two components of the common point}} else if (pre[v]<pre[u]&&fa!=v) {low[u]=min (   LOW[U],PRE[V]);     }} return low[u];}    void Inital () {clock=0;    bc=0;    memset (pre,0,sizeof Pre);    memset (low,inf,sizeof low);    while (!s.empty ()) {s.pop ();    }}int Main () {int u,v;    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d", &n,&m)) {inital ();            for (int i=0;i<m;i++) {scanf ("%d%d", &u,&v);        G[u][v]=g[v][u]=1;        } for (int i=1;i<=n;i++) {if (!pre[i]) DFS (I,-1);        } for (int i=1;i<=n;i++) {printf ("%d%d\n", pre[i],low[i]);        } printf ("Below is the point Unicom component%d:\n", BC);            for (int i=1;i<=bc;i++) {printf ("%d:", i);            for (int j=0;j<bcc[i].size (); j + +) {printf ("%d", bcc[i][j]);        } printf ("\ n"); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dual connected components of undirected graphs (tarjan template)

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.