Cow Contest
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16316 | Accepted: 9126 |
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
Source
问题链接:POJ3660 Cow Contest
问题描述:有n个奶牛(从1开始编号)进行比赛,有m个结果,一个结果用a b描述,表示a奶牛打败b奶牛,在这种情况下a奶牛也能打败b奶牛能打败的奶牛,问通过这m个结果能确定多少个奶牛的名次。
解题思路:共有n奶牛,如果能打败奶牛a的奶牛有x个,能被奶牛打败的奶牛有y个,且x+y=n-1那么奶牛a的名次就是确定的,且其名次为第x+1名。传递闭包,使用Floyd算法进行求解
AC的C++程序:
#include<iostream>
#include<cstring>
using namespace std;
const int N=105;
bool r[N][N];//r[i][j]表示奶牛i能打败奶牛j
int main()
{
int n,m,i,j,k,a,b;
scanf("%d%d",&n,&m);
memset(r,false,sizeof(r));
while(m--){
scanf("%d%d",&a,&b);
r[a][b]=true;//a能打败b
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(r[i][k]&&r[k][j])//i能打败k,k能打败j,则i就能打败j;闭包传递
r[i][j]=true;
int ans=0;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==j) continue;
if(r[i][j]==false&&r[j][i]==false) break;//不能够确定i和j的关系
}
if(j>n)
ans++;
}
printf("%d\n",ans);
return 0;
}