码迷,mamicode.com
首页 > 其他好文 > 详细

Poj2186Popular Cows

时间:2016-11-16 22:20:39      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   mis   set   space   ret   mem   pairs   turn   time   

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;
}

 

Poj2186Popular Cows

标签:des   mis   set   space   ret   mem   pairs   turn   time   

原文地址:http://www.cnblogs.com/xiaoningmeng/p/6071568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!