kuangbin 专题四:H题 Cow Contest ()
H - Cow Contest
POJ3660
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.
* 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
* Line 1: A single integer representing the number of cows whose ranks can be determined
5 5 4 3 4 2 3 2 1 2 2 5Sample Output
2
题意: n 头牛 之间进行编程竞赛 , 按照技能水平的高低决胜负,水平高的获胜,给出 m 组 比赛结果(每组结果分别是两头牛进行比赛(A 和 B) 胜利的放在前面 (A))
问有多少头牛可以确定 能力水平 。
思路: (最后一句问题亮了 mmp 我怎么知道你技能水平怎么判断),仔细思考发现如果可以确定一头牛的技能水平 则 这头牛 必须和其他牛 直接或者间接(通过其他牛) 的存在胜负关系
然后问题就可以转化成 :扩展每个节点和其他节点的胜负关系,然后 节点和其他节点存在的 胜负关系数量 为 n - 1 , 则可以确定此牛的 技能等级关系 。
再之后问题转化成 , 多源最短路 floyd 算法的 伸展过程(逐渐确定节点之间关系),最后统计一下 胜负关系数量为 n - 1 的节点数量就好了
/*题目大意是说:给出牛之间的强弱关系,让你确定有多少头牛能够确定其排名。
用Floyd做,对每给的一个胜负关系连一条边,最后跑一次Floyd,然后判断一头牛所确定的关系是否是n-1次,若是,则这头牛的排名可以确定 */
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std ; #define maxn 200 int n , m ; int mapp[maxn][maxn] ; // 初始化 && 输入 void init() { for(int i=1 ; i<=n ; i++) { for(int j=1 ; j<= n ; j++) { mapp[i][j] = 0 ; } } int a , b ; for(int i=1 ; i<=m ; i++) { scanf("%d%d" , &a , &b ) ; mapp[a][b] = 1 ; } } void floyd() { // 伸展 for(int k = 1 ; k<=n ; k++) { for(int i=1 ; i<=n ; i++) { for(int j=1 ; j<=n ; j++) { if(mapp[i][k] && mapp[k][j]) { mapp[i][j] = 1 ; } } } } } void solve() { // 查询 每个节点和所有其他节点 胜负关系 。 int sum , result = 0 ; for(int i=1 ; i<=n ; i++) { sum = 0 ; for(int j =1 ; j<=n ; j++) { if(mapp[i][j] || mapp[j][i]) { sum ++ ; } } if(sum == n-1) { result++ ; } } printf("%d\n" , result) ; } int main() { while(~scanf("%d%d" , &n , &m)) { init() ; floyd() ; solve() ; } return 0 ; }