码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-04-14 17:44:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:利用   source   seve   img   output   hide   std   bdd   div   

 

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

 

本题思路:将题目给出的已知边都存入图中,利用传递性求出可能存在的每条边,对于一个学生,用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\n", ans);
37     return 0;
38 }
View Code

 

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

标签:利用   source   seve   img   output   hide   std   bdd   div   

原文地址:https://www.cnblogs.com/bianjunting/p/10705863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!