• HDU 3836 Equivalent Sets


    Equivalent Sets

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

    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.
     
    Source
     
    Recommend
    xubiao   |   We have carefully selected several similar problems for you:  3835 3828 3834 3830 3829 
    #include<iostream>
    #include<cstdio> 
    #include<cstring>
    #include<algorithm>
    #define MAXN 100000+15
    using namespace std;
    int n,m,tot,tim,top,sumcol,ans,bns;
    int to[MAXN],from[MAXN],net[MAXN],x[MAXN],col[MAXN],into[MAXN],out[MAXN];
    int dfn[MAXN],low[MAXN],vis[MAXN],stack[MAXN],visstack[MAXN];
    void add(int u,int v){
        to[++tot]=v;x[tot]=u;net[tot]=from[u];from[u]=tot;
    }
    void tarjin(int now){
        low[now]=dfn[now]=++tim;
        stack[++top]=now;
        visstack[now]=1;
        vis[now]=1;
        for(int i=from[now];i;i=net[i])
            if(visstack[to[i]])
                low[now]=min(low[now],dfn[to[i]]);
            else if(!vis[to[i]]){
                tarjin(to[i]);
                low[now]=min(low[now],low[to[i]]);
            }
        if(dfn[now]==low[now]){
            sumcol++;
            col[now]=sumcol;
            while(stack[top]!=now){
                col[stack[top]]=sumcol;
                visstack[stack[top]]=0;
                top--;
            }
            visstack[now]=0;
            top--;
        } 
    }
    int T;
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            top=0;tot=0;sumcol=0;tim=0;
            ans=0;bns=0;
            memset(to,0,sizeof(to));
            memset(low,0,sizeof(low));
            memset(dfn,0,sizeof(dfn));
            memset(vis,0,sizeof(vis));
            memset(col,0,sizeof(col));
            memset(net,0,sizeof(net));
            memset(out,0,sizeof(out));
            memset(into,0,sizeof(into));
            memset(from,0,sizeof(from));
            memset(stack,0,sizeof(stack));
            memset(visstack,0,sizeof(visstack));
            for(int i=1;i<=m;i++){
                int a,b;
                cin>>a>>b;
                add(a,b);
            }
            for(int i=1;i<=n;i++)
                if(!vis[i])    tarjin(i);
            if(sumcol==1){
                cout<<"0"<<endl;
                continue;
            }
            for(int i=1;i<=tot;i++)
                if(col[to[i]]!=col[x[i]]){
                    into[col[to[i]]]++;
                    out[col[x[i]]]++;
                }
            for(int i=1;i<=sumcol;i++){
                if(into[i]==0)    ans++;
                if(out[i]==0)    bns++;
            }
            cout<<max(ans,bns)<<endl;
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    [debug]重定义默认參数
    UVA 1329 Corporative Network【并查集】
    fork同一时候创建多个子进程的方法
    CSS3弹性布局内容对齐(justify-content)属性使用具体解释
    python监控linux性能以及进程消耗的性能
    Android实战简易教程-第十三枪(五大布局研究)
    Linux 截图
    (hdu step 7.1.6)最大三角形(凸包的应用——在n个点中找到3个点,它们所形成的三角形面积最大)
    【SPOJ-GSHOP】Rama and Friends【贪心】【细节】
    【编程题目】在字符串中找出连续最长的数字串,并把这个串的长度返回
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7400254.html
Copyright © 2020-2023  润新知