• 【Tarjan缩点】POJ2186 Popular Cows


    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 35644   Accepted: 14532

    Description

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
    popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

    Input

    * Line 1: Two space-separated integers, N and M 

    * Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

    Output

    * Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

    Sample Input

    3 3
    1 2
    2 1
    2 3
    

    Sample Output

    1
    题目大意:
      给定一个有向图,求有多少个顶点是由任何顶 点出发都可达的。
     
    题解:
      Tarjan缩点构成DAG
      若DAG上面如果有唯一的出度为0的点,则该点能被所有的点可达。那么该点所代表的连通分量上的所有的原图中的点,都能被原图中的所有点可达,则该连通分量的点数,就是答案。 
      若DAG上面如果有不止一个出度为0的点,则这些点互相不可达,原问题无解,答案为0
     
    代码:
    //by 减维
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<cstdlib>
    #include<ctime>
    #include<cmath>
    #include<algorithm>
    #define ll long long
    #define maxn
    using namespace std;
    
    struct edge{
        int fr,to,ne;
    }e[500005];
    
    int n,m,ecnt,cnt,num,tot,ans,ans2;
    int map[100005],dfn[100005],low[100005],chu[100005],shu[100005];
    int head[100005],zhan[100005];
    bool pd[100005];
    
    void add(int x,int y)
    {
        e[++ecnt].to=y;
        e[ecnt].fr=x;
        e[ecnt].ne=head[x];
        head[x]=ecnt;
    }
    
    void tarjan(int x)
    {
        dfn[x]=low[x]=++num;
        zhan[++tot]=x;
        pd[x]=1;
        for(int i=head[x];i;i=e[i].ne)
        {
            int dd=e[i].to;
            if(!dfn[dd]){
                tarjan(dd);
                low[x]=min(low[x],low[dd]);
            }else if(pd[dd]){
                low[x]=min(dfn[dd],low[x]);
            }
        }
        if(dfn[x]==low[x]){
            cnt++;
            int t;
            do{
                t=zhan[tot--];
                map[t]=cnt;
                pd[t]=0;
                shu[cnt]++;
            }while(t!=x);
        }
    }
    
    void init()
    {
        cnt=ecnt=num=tot=ans=0;
        memset(e,0,sizeof(e));
        memset(pd,0,sizeof(pd));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(map,0,sizeof(map));
        memset(chu,0,sizeof(chu));
        memset(shu,0,sizeof(shu));
        memset(head,0,sizeof(head));
        memset(zhan,0,sizeof(zhan));
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            init();
            for(int i=1;i<=m;++i){
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,y);
            }
            for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i);
            //for(int i=1;i<=n;++i)printf("%d ",dfn[i]);printf("
    ");
            //for(int i=1;i<=n;++i)printf("%d ",low[i]);printf("
    ");
            //for(int i=1;i<=n;++i)printf("%d ",map[i]);printf("
    ");
            for(int i=1;i<=ecnt;++i)
            {
                int x=e[i].fr,dd=e[i].to;
                if(map[x]!=map[dd])chu[map[x]]++;
            }
            memset(pd,0,sizeof(pd));
            for(int i=1;i<=n;++i)
                if(chu[map[i]]==0&&!pd[map[i]])
                    pd[map[i]]=1,ans++,ans2=shu[map[i]];
            if(ans==1)printf("%d
    ",ans2);
            else printf("0
    ");
        }
    }
  • 相关阅读:
    华为交换机配置命令总结
    Linux 系统启动项修复
    Linux菜鸟成长日记 ( Linux 下的 ftp 文件传输协议 )
    Linux 查看用户命令
    linux 查看过滤命令命令
    Linux篇---ftp服务器的搭建
    linux挂载详解
    园区IP地址规划(非常详细)
    Linux创建、删除文件和文件夹命令
    CentOs 7 安装 Xampp
  • 原文地址:https://www.cnblogs.com/rir1715/p/7658199.html
Copyright © 2020-2023  润新知