Uvalive 4287 SCC-Tarjan added edge to become strongly connected component

Source: Internet
Author: User

It is still a question of strongly connected components, but this question is different: How many directed edges do you need to add at least to turn the entire graph into a strongly connected component

Then the conclusion is that we can find the number of the points with the input degree 0 and the number of the points with the output degree 0, and take the maximum value. How can we prove it... I don't know how to prove it either, but I thought it was just like this. I 'd like to discuss how to prove it with ConG Ge another day.

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <stack>using namespace std;const int N=20010;int pre[N],lowlink[N],sccno[N];vector<int>G[N],G2[N];stack<int> sta;int n,m,vis[N],scc_cnt,dfs_clk;int ind[N],outd[N];void init(){    for (int i=0;i<=n;i++){        pre[i]=0;        lowlink[i]=0;        sccno[i]=0;        G[i].clear();        G2[i].clear();        vis[i]=0;        scc_cnt=dfs_clk=0;        ind[i]=outd[i]=0;    }}void dfs(int u){    pre[u]=lowlink[u]=++dfs_clk;    sta.push(u);    for (int i=0;i<G[u].size();i++){        int v=G[u][i];        if (!pre[v]){            dfs(v);            lowlink[u]=min(lowlink[u],lowlink[v]);        }        else if (!sccno[v]){            lowlink[u]=min(lowlink[u],pre[v]);        }    }    if (lowlink[u]==pre[u]){        scc_cnt++;        for (;;){            int x=sta.top();sta.pop();            sccno[x]=scc_cnt;            if (x==u) break;        }    }}void tarjan(){    for (int i=1;i<=n;i++){        if (!pre[i]) dfs(i);    }    for (int i=1;i<=n;i++){        int u=sccno[i];        for (int j=0;j<G[i].size();j++){            int v=G[i][j];            v=sccno[v];            if (u!=v){                G2[u].push_back(v);                ind[v]++;                outd[u]++;            }        }    }}int main(){    int t;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&m);        init();        int a,b;        while (m--){            scanf("%d%d",&a,&b);            G[a].push_back(b);        }        tarjan();        if (scc_cnt==1){            puts("0");            continue;        }        int ans1=0,ans2=0;        for (int i=1;i<=scc_cnt;i++){            if (ind[i]==0) ans1++;            if (outd[i]==0) ans2++;        }        printf("%d\n",max(ans1,ans2));    }    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.