• POJ2186 Popular Cows


    POJ2186 Popular Cows

    Time Limit: 2000MS Memory Limit: 65536K

    Total Submissions: 14474 Accepted: 5747

    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

    Hint

    Cow 3 is the only cow of high popularity. 

    ************************************************************************

    题目大意:给定一个有向图,问有多少个点由任意顶点出发都能达到。

    解题思路:首先,在一个有向无环图中,能被所有点达到点,出度一定是0。

    先求出所有的强连通分支,然后把每个强连通分支收缩成一个点。这样,这个有向图就变成了一个有向无环图。在这个新的图中,只需知道出度为0的点有几个。如果

    出度为0的点超过1个,则输出0;不然就输出出度为0的那个点所代表的那个强连通分支的顶点数即可。

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int>g1[10005],g2[10005];
    int gro[10005],vis[10005],num[10005],gro_id[10005],id[10005];
    int n,m,now;
    
    void dfs1(int s)
    {
        vis[s]=1;
        for(int i=0;i<g1[s].size();i++)
            if(vis[g1[s][i]]==0)
                dfs1(g1[s][i]);
        num[s]=++now;
    }
    
    void dfs2(int s)
    {
        vis[s]=1;
        gro_id[s]=now;
        gro[now]++;
        for(int i=0;i<g2[s].size();i++)
            if(vis[g2[s][i]]==0)
                dfs2(g2[s][i]);
    }
    
    bool cmp(int a,int b)
    {
        return num[a]>num[b];
    }
    
    int main()
    {
        memset(gro,0,sizeof(gro));
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        memset(gro_id,0,sizeof(gro_id));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            id[i]=i;
        for(int i=1;i<=n;i++)
            g1[i].clear(),g2[i].clear();
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            g1[a].push_back(b);
            g2[b].push_back(a);
        }
        now=0;
        for(int i=1;i<=n;i++)
            if(vis[i]==0)dfs1(i);
        sort(id+1,id+1+n,cmp);
        memset(vis,0,sizeof(vis));
        now=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[id[i]])continue;
            now++;
            dfs2(id[i]);
        }
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            for(int j=0;j<g1[i].size();j++)
                if(gro_id[i]!=gro_id[g1[i][j]])
                {
                    vis[gro_id[i]]=1;
                    break;
                }
        vector<int>ans;
        ans.clear();
        for(int i=1;i<=now;i++)
            if(vis[i]==0)
                ans.push_back(i);
        if(ans.size()!=1)printf("0\n");
        else printf("%d\n",gro[ans[0]]);
    }
    

      下面是tarjan的连通算法,速度竟然比上面的慢。。。情何以堪

    #include <stdio.h>
    #include <string.h>
    #include <stack>
    #include <vector>
    using namespace std;
    
    vector<int>gra[10005];
    stack<int>sta;
    int dfn[10005],low[10005],vis[10005],now,id,n,m;
    int gro_id[10005],gro[10005];
    
    void tarjan(int s)
    {
        vis[s]=2;
        dfn[s]=low[s]=++now;
        sta.push(s);
        for(int i=0;i<gra[s].size();i++)
        {
            if(vis[gra[s][i]]==0)
                tarjan(gra[s][i]),low[s]=low[gra[s][i]]<low[s]?low[gra[s][i]]:low[s];
            else
                if(vis[gra[s][i]]==2)
                    low[s]=dfn[gra[s][i]]<low[s]?dfn[gra[s][i]]:low[s];
        }
        if(low[s]==dfn[s])
        {
            id++;
            while(1)
            {
                int t=sta.top();
                gro_id[t]=id;
                vis[t]=1;
                sta.pop();
                gro[id]++;
                if(t==s)break;
            }
        }
    }
    
    int main()
    {
        memset(vis,0,sizeof(vis));
        memset(gro,0,sizeof(gro));
        memset(gro_id,0,sizeof(gro_id));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            gra[i].clear();
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            gra[a].push_back(b);
        }
        now=id=0;
        for(int i=1;i<=n;i++)
            if(!vis[i])tarjan(i);
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            for(int j=0;j<gra[i].size();j++)
                if(gro_id[i]!=gro_id[gra[i][j]])
                {
                    vis[gro_id[i]]=1;
                    break;
                }
        vector<int>ans;
        ans.clear();
        for(int i=1;i<=id;i++)
            if(vis[i]==0)
                ans.push_back(i);
        if(ans.size()!=1)printf("0\n");
        else printf("%d\n",gro[ans[0]]);
    }
    

      

  • 相关阅读:
    freeswitch录音功能
    jdk安装
    maven阿里云镜像
    idea安装
    idea新建maven项目
    tomcat安装
    idea新建maven web项目
    idea新建java项目
    webpack使用
    ACE 安装指南及示例
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2178890.html
Copyright © 2020-2023  润新知