zoukankan      html  css  js  c++  java
  • 【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
    ");
        }
    }
  • 相关阅读:
    Button与ImageButton的区别
    TCP、HTTP、Socket
    ASPX开发基础
    WebForm复杂控件
    Webform 内置对象 Session对象、Application全局对象,ViewState
    JavaScript
    .net
    postman-学习笔记
    2021第一天上班,立个flag
    js函数累加
  • 原文地址:https://www.cnblogs.com/rir1715/p/7658199.html
Copyright © 2011-2022 走看看