POJ-3660.Cow Contest(有向图的传递闭包)

 

Cow Contest

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17797   Accepted: 9893

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

Source

USACO 2008 January Silver

 

本题思路:将题目给出的已知边都存入图中,利用传递性求出可能存在的每条边,对于一个学生,用i -> j表示 i 比 j 强,那么对于所有学生,他的排名被确定的条件就是确定他与其它所有同学的排名情况,即Bigger[ i ] + Smaller[ i ] == n - 1。

参考代码:(不建议看,按照上面的思路实现一波就行了)

POJ-3660.Cow Contest(有向图的传递闭包)
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 
 6 const int maxn = 100 + 2, INF = 0x3f3f3f3f;
 7 int n, m, a, b;
 8 bool G[maxn][maxn];
 9 
10 int Floyd_Warshall() {
11     int num = 0;
12     for(int k = 1; k <= n; k ++) {
13         for(int i = 1; i <= n; i ++) {
14             for(int j = 1; j <= n; j ++) {
15                 G[i][j] = G[i][j] || (G[i][k] && G[k][j]);
16             }
17         }
18     }
19     for(int i = 1; i <= n; i ++) {
20         int temp = 0;
21         for(int j = 1; j <= n; j ++)
22             if(G[i][j] || G[j][i]) temp ++;
23         if(temp == n - 1) num ++;
24     }
25     return num;
26 }
27 
28 int main () {
29     scanf("%d %d", &n, &m);
30     int x, y;
31     for(int i = 0; i < m; i ++) {
32         scanf("%d %d", &x, &y);
33         G[x][y] = true;
34     }
35     int ans = Floyd_Warshall();
36     printf("%d\n", ans);
37     return 0;
38 }
View Code

 

上一篇:$POJ3278 Catch That Cow$


下一篇:Tallest cow(差分数组)