POJ-3660.Cow Contest(有向图的传递闭包)
Posted bianjunting
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-3660.Cow Contest(有向图的传递闭包)相关的知识,希望对你有一定的参考价值。
Cow Contest
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 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 |
本题思路:将题目给出的已知边都存入图中,利用传递性求出可能存在的每条边,对于一个学生,用i -> j表示 i 比 j 强,那么对于所有学生,他的排名被确定的条件就是确定他与其它所有同学的排名情况,即Bigger[ i ] + Smaller[ i ] == n - 1。
参考代码:(不建议看,按照上面的思路实现一波就行了)
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 ", ans); 37 return 0; 38 }
以上是关于POJ-3660.Cow Contest(有向图的传递闭包)的主要内容,如果未能解决你的问题,请参考以下文章
POJ3660:Cow Contest(Floyd传递闭包)