POJ 3660 Cow Contest(floyed运用)

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个关系,每次输入的x,y代表x胜过y,求出能够确定当前奶牛和其他所有奶牛的关系的奶牛有几头。
思路:对于每两个奶牛之间有三种关系, 1.没关系 2. a胜过b 3. a输给b,我们用dis[i][j] 代表第i只奶牛和第j只奶牛的关系。我们首先可以对开始输入的奶牛的关系建图,之后用floyed跑一遍图,遍历完所有点点之间的关系,最后判断每一个点,若与其他n-1个点都有关系则ans++

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 #include<queue>
 5 
 6 using namespace std;
 7 const int INF = 0x3f3f3f3f;
 8 int dis[110][110];
 9 int n, m;
10 void floyed()
11 {
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                 if ((dis[i][k] == 1 && dis[k][j] == 1) || (dis[i][k] == 2 && dis[k][j] == 2))
16                     dis[i][j] = dis[i][k]; //判断ij之间的关系
17             }
18 }
19 int main()
20 {
21     ios::sync_with_stdio(false);
22     while (cin >> n >> m) {
23         memset(dis, 0, sizeof(dis));
24         for (int a, b, i = 1; i <= m; i++) {
25             cin >> a >> b;
26             dis[a][b] = 1;//a胜b
27             dis[b][a] = 2;//a输给b
28         }
29         floyed();
30         int ans = 0;
31         for (int i = 1; i <= n; i++) {
32             int cnt = 0;
33             for (int j = 1; j <= n; j++) {
34                 if (i == j)continue;
35                 if (dis[i][j])cnt++;
36             }
37             if (cnt == (n - 1))ans++;
38         }
39         cout << ans << endl;
40     }
41     return 0;
42 }

猜你喜欢

转载自www.cnblogs.com/wangrunhu/p/9501880.html