ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-09-09 10:02:28  阅读:226  来源: 互联网

标签:奶牛 ma 3660 cow Contest Cow int cows include


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 }

 

 


标签:奶牛,ma,3660,cow,Contest,Cow,int,cows,include
来源: https://www.cnblogs.com/mzchuan/p/11490020.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有