zoukankan      html  css  js  c++  java
  • hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets

    Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
    Total Submission(s): 2798    Accepted Submission(s): 962


    Problem Description
    To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
    You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
    Now you want to know the minimum steps needed to get the problem proved.
     

    Input
    The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
    Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
     

    Output
    For each case, output a single integer: the minimum steps needed.
     

    Sample Input
    4 0 3 2 1 2 1 3
     

    Sample Output
    4 2
    Hint
    Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
     
    求将原图的强连通分量缩点,得到有向无环图,求至少加多少条边能够使这个图变成一幅强连通图,max(入度为0的点,出度为0的点)即为答案。



    #include"stdio.h"
    #include"string.h"
    #include"queue"
    #include"vector"
    #include"algorithm"
    using namespace std;
    #define N 20005
    #define M 50005
    #define min(a,b) (a<b?a:b)
    const int inf=1000000;
    struct node
    {
        int u,v,next;
    }e[M];
    int t,bcnt,index,stop,ans;
    int head[N],dfn[N],low[N],stap[N],mark[N],be[N];
    int indeg[N],out[N];
    void add(int u,int v)
    {
        e[t].u=u;
        e[t].v=v;
        e[t].next=head[u];
        head[u]=t++;
    }
    void tarjan(int u)
    {
        int i,v;
        dfn[u]=low[u]=++index;
        stap[++stop]=u;
        mark[u]=1;
        for(i=head[u];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(mark[v])
                low[u]=min(low[u],dfn[v]);
        }
        if(low[u]==dfn[u])
        {
            bcnt++;
            do
            {
                v=stap[stop--];
                mark[v]=0;
                be[v]=bcnt;
            }
            while(u!=v);
            ans++;
        }
    }
    void solve(int n)
    {
        int i;
        memset(dfn,0,sizeof(dfn));
        index=stop=bcnt=0;
        for(i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
    }
    void work(int n)
    {
        int i,j,u,v,t1,t2;
        memset(indeg,0,sizeof(indeg));
        memset(out,0,sizeof(out));
        for(i=1;i<=n;i++)
        {
            u=be[i];
            for(j=head[i];j!=-1;j=e[j].next)
            {
                v=be[e[j].v];
                if(u!=v)
                {
                    indeg[v]++;
                    out[u]++;
                }
            }
        }
        t1=t2=0;
        for(i=1;i<=bcnt;i++)
        {
            if(indeg[i]==0)
                t1++;
            if(out[i]==0)
                t2++;
        }
        printf("%d
    ",t1>t2?t1:t2);
    }
    int main()
    {
        int n,m,u,v;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            t=0;
            memset(head,-1,sizeof(head));
            while(m--)
            {
                scanf("%d%d",&u,&v);
                add(u,v);
            }
            ans=0;
            solve(n);
           // printf("%d
    ",ans);
            if(ans==1)
                printf("0
    ");
            else
                work(n);
        }
        return 0;
    }


  • 相关阅读:
    开源一个适用iOS的数据库表结构更新机制的代码
    耗油是什么??主要起什么作用???_百度知道
    跨平台C++ 功能全面的Socket类库,支持TCP,UDP,限速等等_陈华_新浪博客
    ASICME Avalon|阿瓦隆比特币矿机
    水淀粉_百度百科
    HyperDex.org
    不忘初心:从阿里运维工程师到水果贩子的“降级”路 | 卖水果的工程师
    用Github账号领Ripple币
    简洁 轻快 完美支持 Windows 8 Modern UI 单行输入,轻盈流畅
    赴美旅游签证,不求人!
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7293800.html
Copyright © 2011-2022 走看看