Strong connected component hdu3836 of a direction graph

Source: Internet
Author: User

Do the problem today. Tarjan algorithm for strongly connected components of graph theory No, so just follow this blog https://www.byvoid.com/blog/scc-tarjan/learn a bit, feel very good
topic is to say equivalence proof, a,b,c, D Mutual proof, the least increase in the number of deduction, a can launch B,b can launch C,c can launch D,d can launch a, at least 4 times. The strong connected components are first identified, then each strong connected component is reduced to a point, and a DAG is obtained. Next, there is a node (remember, here each node relative to the original image of a strong connected component) of the 0,b node of the degree of 0, then max{a,b} is the answer. Note The special case: When the original image is already strong connected, the answer is 0, not 1. The
defines Lowlink (U) as the sequence number (timestamp) of the node U search, and the Pre (U) is the order number of the nodes in the earliest stack that the subtree of U or u can trace back to. By definition,
when Lowlink (u) =pre (u), all nodes on the search subtree with u are a strongly connected component

#include <iostream> #include <vector> #include <stack> #include <stdio.h> #include <string.h

> #define MAXN 50005 using namespace std;
vector<int>g[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
SCC_CNT is the GCC counter, Sccno[i] is the SCC number for I, stack<int> S; void Dfs (int u) {pre[u]=lowlink[u]=++dfs_clock;//here is used to mark this point processed, the next time Dfs can skip this point s.push (U);//Every search to a point, press into the stack fo
        R (int i=0; i<g[u].size (); i++)//traversing point {int v=g[u][i] connected to u;
            if (!pre[v])//If this point has not been traversed (not on the stack), then search for this point deeply {dfs (v);
        Lowlink[u]=min (Lowlink[u],lowlink[v]);
        else if (!sccno[v])//(on stack) {lowlink[u]=min (lowlink[u],pre[v]);
        } if (Lowlink[u]==pre[u])//found a root {scc_cnt++;
            while (1) {int x=s.top ();//The point in the stack above this point is all out of the stack into a strong connected component S.pop ();
            sccno[x]=scc_cnt;
        if (x==u) break; }} VOID FIND_SCC (int n) {dfs_clock=scc_cnt=0;
    memset (sccno,0,sizeof (SCCNO));
    memset (pre,0,sizeof (pre));
for (int i=0; i<n; i++) if (!pre[i)) Dfs (i);
}//above is template int IN0[MAXN],OUT0[MAXN];
    int main () {int n,m;
        while (~SCANF ("%d%d", &n,&m)) {for (int i=0; i<n; i++) g[i].clear ();
            for (int i=0; i<m; i++) {int u,v;
            scanf ("%d%d", &u,&v);
            u--;
            v--;
        G[u].push_back (v);
        } FIND_SCC (n);
            for (int i=1; i<=scc_cnt; i++) in0[i]=out0[i]=1;//Attach the indentation of each strong connected component to 1 for (int u=0; u<n; u++)
                for (int i=0; i<g[u].size (); i++) {int v=g[u][i]; if (Sccno[u]!=sccno[v])//If these two are not a bunch, then there must be a connection between them, then the two pairs of degrees is not 0, because we are asking for 0, so this piece to deal with In0[sccno[v]]=out0
        [sccno[u]]=0;//processed here into 0 for the relative illumination of 1 and is an int a=0,b=0; for (int i=1; i<=scc_cnt;
            i++) {if (In0[i])//1 indicates the need to offset the situation of 0, a++;
        if (Out0[i]) b++;
        int Ans=max (A,B);//MAX offset is the least side if (scc_cnt==1) ans=0 that needs to be added;
    printf ("%d\n", ans);
return 0;
 }

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.