POJ 3660 Cow Contest

本文介绍了一道关于牛参与编程比赛并根据比赛结果确定牛的技能排名的问题。通过使用Floyd算法来传递实力关系,最终确定能够精确排名的牛的数量。

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 ≤ 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.


【题目分析】
从cow看出来这是从usaco上找的题目。大概的意思是说告诉你一些牛的实力的关系,问你有多少牛的排名可以确定。只需要跑一边floyd,如果这头牛比其他牛强的数目+其他牛比这头牛强的数目==n-1就可以确定排名了。思路很巧妙,就是用floyd传递了闭包。


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
int map[101][101];
using namespace std;
int main()
{
    int n,m;
    while (cin>>n>>m)
    {
        memset(map,0,sizeof map);
        for (int i=1;i<=m;++i)
        {
            int a,b;
            cin>>a>>b;
            map[a][b]=1;
        }
        for (int k=1;k<=n;++k)
            for (int i=1;i<=n;++i)
                for (int j=1;j<=n;++j)
                    map[i][j]|=(map[i][k]&map[k][j]);
        int ans=0;
        for (int i=1;i<=n;++i)
        {
            int cnt=0;
            for (int j=1;j<=n;++j) {if (map[i][j]) cnt++; if (map[j][i]) cnt++;}
            if (cnt==n-1) ans++;
        }
        cout<<ans<<endl;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值