Popular Cows poj2186

在一个牛群中,通过分析牛之间的社交关系,确定哪些牛被认为是最受欢迎的。利用图论中的强连通分量算法解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 33277 Accepted: 13559

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. 

Source

USACO 2003 Fall


题目描述:
  有n只牛,牛之间存在一些关系,比如a认为b很受欢迎,b认为c很受欢迎,这样呢,a也会认为c很受欢迎,问根据给出的关系,有多少头牛被其他所有的牛都认为是受欢迎的?


解题思路:

        对于一个有向无环图来说,其中有且仅有一个点出度为零,那么这个特殊的点,可以由其他任何点到达。那么接下来我们直接对所给的图进行强连通分量划分,然后把每个强连通分量看做一个点,判定出度为零的点有几个,如果有一个就输出这个点对应的强连通分量含有的节点个数,否则为零。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <ctime>
#include <queue>
using namespace std;

int n,m,s=0,top=0,index=0;
int first[10001],zhan[10001];
int low[10001],num[10001];
bool dian[10001];
struct tarjan{int x,y,next;};
tarjan bian[100001];

int sum=0,father[10001]={0},maxx=0,p,ans=0,visit[10001];

void build(int x,int y)
{
   s++;
   bian[s].next=first[x];
   first[x]=s;
   bian[s].x=x;
   bian[s].y=y;
}

void dfs(int x)
{
   index++;
   low[x]=index;
   num[x]=index;
   top++;
   zhan[top]=x;
   dian[x]=true;

   int k,i;
   k=first[x];
   while(k!=-1)
   {
   	 if(num[bian[k].y]==0)
   	 {
   	   dfs(bian[k].y);
   	   low[x]=min(low[x],low[bian[k].y]);
   	 }
   	 else if(bian[k].y) low[x]=min(low[x],num[bian[k].y]);
   	 k=bian[k].next;
   }

   if(low[x]==num[x])
   {
   	 sum++;
   	 i=zhan[top];
   	 while(i!=x)
   	 {
   	   dian[i]=false;
   	   father[i]=sum;
   	   top--;
   	   i=zhan[top];
   	 }
   	 dian[i]=false;
   	 father[i]=sum;
   	 top--;
   }
}

int main()
{
   //freopen("lx.in","r",stdin);
   //freopen("lx.out","w",stdout);
   memset(first,-1,sizeof(first));
   memset(zhan,0,sizeof(zhan));
   memset(low,0,sizeof(low));
   memset(num,0,sizeof(num));
   memset(dian,false,sizeof(dian));
   memset(bian,0,sizeof(bian));
   memset(visit,0,sizeof(visit));

   scanf("%d%d",&n,&m);
   for(int i=1;i<=m;i++)
   {
   	 int x,y;
   	 scanf("%d%d",&x,&y);
   	 build(x,y);
   }

   for(int i=1;i<=n;i++) if(num[i]==0) dfs(i);

   for(int i=1;i<=n;i++)
     for(int j=first[i];j!=-1;j=bian[j].next)
       if(father[i]!=father[bian[j].y]) visit[father[i]]++;

   for(int i=1;i<=sum;i++)
   	 if(visit[i]==0)
   	 {
   	   p=i;
   	   maxx++;
   	 }

   if(maxx==1)
   {
   	 ans=0;
   	 for(int i=1;i<=n;i++)
   	   if(father[i]==p) ans++;
   	 cout<<ans<<endl;
   }
   else cout<<"0"<<endl;

   return 0;
}

题目:http://poj.org/problem?id=2186

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值