Cow Contest
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9763 Accepted: 5490
Description
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.
Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.
Input
- Line 1: Two space-separated integers: N and M
- Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B
Output
- Line 1: A single integer representing the number of cows whose ranks can be determined
Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
【分析】
题意:现有n只奶牛,给出m组奶牛,每组两只奶牛A和B,代表A比B要6,农夫想根据它们6的程度给它们排名次,问能确定排名的奶牛个数。
听网上说这叫什么闭包传递,跑一遍floyd就行,即如果A比B 6,B比C 6,那么A比C 6,然后再找每个点的入度出度,如果度之和为n-1,排名就确定了。
【代码】
//poj 3660 Cow Contest
#include<iostream>
#include<cstdio>
#define fo(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
bool map[101][101];
int ans,du[101];
int main()
{
int n,m,x,y;
scanf("%d%d",&n,&m);
fo(i,1,m)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
fo(k,1,n)
fo(i,1,n)
fo(j,1,n)
if(i!=j && j!=k && i!=k)
if(map[i][k] && map[k][j])
map[i][j]=1;
fo(i,1,n)
{
fo(j,1,n)
if(map[i][j])
du[i]++,du[j]++;
}
fo(i,1,n)
if(du[i]==n-1)
ans++;
printf("%d\n",ans);
return 0;
}