POJ 3660 Cow Contest

题目链接:http://poj.org/problem?id=3660

Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10066   Accepted: 5682

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 ≤ NA ≠ 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  然后问你有多少头牛能够确定名次。
解题思路:考虑到能够确定名次的牛需要满足一个条件(打败他的牛的个数加上他打败的牛的个数为n-1)
AC代码:
 #include <stdio.h>
#include <string.h>
int p[][];
int n,m;
void floyd()
{
int i,j,k;
for (k = ; k <= n; k ++)
{
for (i = ; i <= n; i ++)
{
for (j = ; j <= n; j ++)
{
if (p[i][k] && p[k][j]) //间接相连也表示能够打败
p[i][j] = ;
}
}
}
}
int main ()
{
int i,j,a,b;
while (~scanf("%d%d",&n,&m))
{
memset(p,,sizeof(p)); for (i = ; i < m; i ++)
{
scanf("%d%d",&a,&b);
p[a][b] = ;
}
floyd();
int ans,sum = ;
for (i = ; i <= n; i ++)
{
ans = ;
for (j = ; j <= n; j ++)
{
ans += p[i][j]; //他打败的牛的个数
ans += p[j][i]; //打败他的牛的个数
}
if (ans == n-)
sum ++;
}
printf("%d\n",sum);
}
return ;
}
上一篇:POJ 3660—— Cow Contest——————【Floyd传递闭包】


下一篇:POJ - 3660 Cow Contest 传递闭包floyed算法