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代码:
 1 #include <stdio.h>
 2 #include <string.h>
 3 int p[110][110];
 4 int n,m;
 5 void floyd()
 6 {
 7     int i,j,k;
 8     for (k = 1; k <= n; k ++)
 9     {
10         for (i = 1; i <= n; i ++)
11         {
12             for (j = 1; j <= n; j ++)
13             {
14                 if (p[i][k] && p[k][j])  //间接相连也表示能够打败
15                     p[i][j] = 1;
16             }
17         }
18     }
19 }
20 int main ()
21 {
22     int i,j,a,b;
23     while (~scanf("%d%d",&n,&m))
24     {
25         memset(p,0,sizeof(p));
26 
27         for (i = 0; i < m; i ++)
28         {
29             scanf("%d%d",&a,&b);
30             p[a][b] = 1;
31         }
32         floyd();
33         int ans,sum = 0;
34         for (i = 1; i <= n; i ++)
35         {
36             ans = 0;
37             for (j = 1; j <= n; j ++)
38             {
39                 ans += p[i][j];  //他打败的牛的个数
40                 ans += p[j][i];  //打败他的牛的个数
41             }
42             if (ans == n-1)
43                 sum ++;
44         }
45         printf("%d\n",sum);
46     }
47     return 0;
48 }
View Code

 



posted @ 2016-09-12 21:01  gaoyanliang  阅读(316)  评论(0编辑  收藏  举报