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.
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.
* 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
题目描述:
有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