Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13133 | Accepted: 7312 |
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
啊啊啊啊!最短路啊最短路啊最短路啊啊啊!~~~本题完全可以用Floyd的中转思想确定cowA与cowB的关系,是A强于B,A弱于B,亦或是AB没有关系。若A强于B,B强于C,那么A一定强于C!Floyd三层循环每次更新不同牛之间的关系。距离用强弱关系表示即可(这一点当时没想到所以并不知道和最短路有什么关系。。。醉醉的。。。), 值为1表示A强于B,为-1表示A弱于B,为0表示AB无关系,若比A强的牛与比A弱牛之和为N-1,那么A的位置也就是名次一定是确定的,因此最后只需检查更新后的表中有几头这样的牛即可!真是遇题不能拖啊,感谢学长大人的开导哇,让我对Floyd有更深的理解/抱拳 附代码:
#include<stdio.h>
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int a[110][110];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=0;
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[x][y]=1;
a[y][x]=-1;
}
for(int k=1;k<=n;k++)
for(int i=1; i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][k]==1&&a[k][j]==1)
{
a[i][j]=1;
a[j][i]=-1;
}
int sum=0;
for(int i=1;i<=n;i++)
{
int q=0;
for(int j=1;j<=n;j++)
{
if(i!=j&&(a[i][j]==-1||a[i][j]==1))
q++;
}
if(q==n-1)
sum++;
}
printf("%d\n",sum);
}
return 0;
}