zoukankan      html  css  js  c++  java
  • Poj2186Popular Cows

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 31533   Accepted: 12817

    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. 
     
    【题目分析】
      题目大概是有好多牛,牛(们)之间具有某种关系(鬼知道),这种关系具有传递性,如果有 A欢迎B和B欢迎C,那么就有A欢迎C。我们要找到能让其他所有的牛都欢迎的牛。
      (废话真多)
      就是求强连通分量了,然后找到一个强连通分量,缩点,最后找出度为0的点(这里可以自己草纸划一下),如果有多个出度为0的点,那么肯定没有牛被所有牛欢迎,如果只有一个,输出这个点所代表的强连通分量的长度!!!md
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    using namespace std;
    const int maxn=12000;
    vector<int>tu[maxn];
    vector<int>lt[maxn];
    int n,m,lts=0;
    int js=0;
    int dfn[maxn],low[maxn];
    int zhan[maxn],top=0;
    bool isins[maxn];
    int num[maxn];//num[i]表示i点所在的强连通分量的编号 
    int d[maxn];
    void tarjan(int i)//hhhh 
    {
        int j;
        dfn[i]=low[i]=++js;
        isins[i]=1;
        zhan[top++]=i;
        for(int j=0;j<tu[i].size();j++)
        {
            int tp=tu[i][j];
            if(dfn[tp]==-1)
                tarjan(tp),
                low[i]=min(low[i],low[tp]);
            else if(isins[tp])
                low[i]=min(low[i],dfn[tp]);
        }
        if(dfn[i]==low[i])
        {
            lts++;
            do{
                j=zhan[--top];
                isins[j]=0;
                lt[lts].push_back(j);
                num[j]=lts;//j点所在的强连通分量的编号为lts 
            }while(i!=j); 
        }
    }
    void solve(int n)
    {
        memset(dfn,-1,sizeof dfn);
        memset(low,-1,sizeof low);
        memset(zhan,-1,sizeof zhan);
        memset(isins,0,sizeof isins);
        for(int i=0;i<n;i++)
            if(dfn[i]==-1)
                tarjan(i);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            tu[x-1].push_back(y-1);//我从0开始啊 
        }
        solve(n);
        for(int i=0;i<n;i++)
            for(int j=0;j<tu[i].size();j++)
            {
                int t=tu[i][j];
                if(num[i]!=num[t])//如果i点与他指向的点不在同一个强连通分量中 
                    d[num[i]]++;//i点所在的强连通分量的出度+1 
            }
        int pos=-1;
        int cnt=0;
        for(int i=1;i<=lts;i++)
            if(d[i]==0)//找到出度为0的强连通分量 
                cnt++,
                pos=i;
        if(cnt==1) cout<<lt[pos].size();//如果只有一个出度为0的强连通分量,那么这个强连通分量的长度即答案 
        else cout<<"0";//woc
        return 0;
    }
  • 相关阅读:
    abap开发报表的简单过程
    有关innerHTML的知识
    MS SQL中的return&output的學習
    js获取下拉列表选中项的值和文本(select)以及获取单选按钮(radio)组的值和修改选中项[转]
    ASP.NET AJAX Control Toolkit
    VB.net下有个函数strconv可以进行简体繁体转换
    JavaScript:prototype属性使用说明
    [转]一份ASP内存的释放的实验报告
    什么是 Virtual Machine Additions(虚拟机附加安装模块)?
    Trigger&Procedure的應用
  • 原文地址:https://www.cnblogs.com/xiaoningmeng/p/6071568.html
Copyright © 2011-2022 走看看