zoukankan      html  css  js  c++  java
  • 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 ≤ AN; 1 ≤ BN; AB), 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 组 比赛结果(每组结果分别是两头牛进行比赛(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
    " , result) ;
        
    }
    int main() {
    
        while(~scanf("%d%d" , &n , &m)) {
            init() ; 
            floyd() ; 
            solve() ; 
    
        }
        return 0 ;
    }
  • 相关阅读:
    HttpModule与HttpHandler详解
    未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。
    javascript的escape()函数
    温故知新 javascript 正则表达式
    大爽Python入门教程 53 文件读写
    大爽Python入门教程 62 变量进阶 局部变量`local`与全局变量`global`
    大爽Python入门教程 61 变量进阶 可变`mutable`、不可变`immutable`
    大爽Python入门教程 57 思路与答案
    大爽Python入门教程 65 函数进阶 内置函数(Builtin Functions)
    大爽Python入门教程 55 实践演示 控制台版本——简易成绩管理系统
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7786781.html
Copyright © 2011-2022 走看看