Cow Contest POJ - 3660 Floyd算法,关系链图

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头奶牛,依次标号为1~n,每一头奶牛都有一个能力值(不重复)。现在两两之间不重复的进行m场battle,自然,能力值高的奶牛将战胜能力值低的奶牛。给出m场battle的最终胜负结果(a,b)(a为胜者),问:根据已知结
果,有多少头奶牛在整体中的排名能被确定。

思路:首先得明确一个点,。最短路一般是求最短的路径或时间或其他,而这个题却是用
最短路的思想来判断两头奶牛之间的关联,首先一个数组ma用于存放念头奶牛之间是否有关联,因为battle有胜负之分,所以关联是单向的。在Floyd算法中只需要判断两点之间是否有单项的关联,最后判断名次时,要明白
一头奶牛在整体中的排名能被确定 <=> 它能被x头不同的奶牛直接或间接打败,同时也能直接或间接打败y头奶牛,也就是它与其他奶牛有双向的关联。

代码:
 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 998244353
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int main()
22 {
23     //n头奶牛,m场对决
24     int n,m;
25     //ma用于存放两头奶牛之间是否有关系。
26     int ma[110][110];
27     scanf("%d %d",&n,&m);
28     //初始化为0表示都没有关系
29     memset(ma,0,sizeof(ma));
30     int a,b;
31     for(int i=1;i<=m;i++)
32     {
33         scanf("%d %d",&a,&b);
34         //为1表示a和b之间有关系,而且是a到b单向的关系
35         ma[a][b]=1;
36     }
37     //Floyd算法核心
38     for(int k=1;k<=n;k++)
39     {
40         for(int i=1;i<=n;i++)
41         {
42             for(int j=1;j<=n;j++)
43             {
44                 //如果i到k有关系,并且k带j有关系,则i可以与j有关系
45                 if(ma[i][k]&&ma[k][j])
46                 {
47                     ma[i][j]=1;
48                 }
49             }
50         }
51     }
52     //判断排位名次
53     int ans=0;
54     for(int i=1;i<=n;i++)
55     {
56         int num=1;
57         for(int j=1;j<=n;j++)
58         {
59             //如果i能到j,或j能到i,则两者之间有关系
60             if(ma[i][j]||ma[j][i])
61             {
62                 num++;
63             }
64         }
65         //如果i与其他奶牛都有关系,则可以判断i的名次
66         if(num==n)
67         {
68             ans++;
69         }
70     }
71     printf("%d\n",ans);
72 }

 

 


上一篇:[kuangbin] POJ3189 Steady Cow Assignment 二分图多重匹配+二分


下一篇:Silver Cow Party POJ - 3268