Cow Contest
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10780 Accepted: 6003
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种奶牛之间的实力关系,如示例中给出5头奶牛和5中实力关系,第一个是4号奶牛实力大于3号,第二个是4号奶牛实力大于2号。。。,求在这n个牛中实力能确定排名的有几头,如示例中1、3、4号牛实力大于2号,2号大于5号,则5号牛实力排名为第5名,2号牛为第4名,故输出2,,
思路:如果一头牛和其他n-1头牛的实力关系确定,那么这头牛的排名也就确定了,但有可能两头牛之间的关系不是直接确定,而是通过其他牛确定,这样就可以想到最短路算法。。根据利害关系构成一个有向图,然后用Floyd算法确定任意两头牛之间的关系。。
AC代码:
#include<stdio.h>
#include<string.h>
int map[105][105];
int main(){
int i,j,k;
int m,n;
int x,y;
scanf("%d%d",&n,&m);
memset(map,0,sizeof(map));
for(i=0;i<m;i++){
scanf("%d%d",&x,&y);
map[x-1][y-1]=1; ///减1是为了让下标从0开始
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++){
///如果i能打败k,k能打败j,则i能打败j
if(map[i][k] && map[k][j])
map[i][j]=1;
}
int sum=0;
for(i=0;i<n;i++){
int tmp=0;
for(j=0;j<n;j++){
if(map[i][j]|| map[j][i])///i和j之间是否存在利害关系
tmp++;
}
//printf("%d\n",tmp);
if(tmp==n-1)
sum++;
}
printf("%d\n",sum);
return 0;
}