【POJ 3660 --- Cow Contest】Floyd

【POJ 3660 --- Cow Contest】Floyd

题目来源:点击进入【POJ 3660 — Cow Contest】

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.

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

扫描二维码关注公众号,回复: 9088863 查看本文章

Sample Output

2

解题思路

根据题意分析可知,一头牛排名的确定是根据打败它的牛的数 + 被它打败的牛的数量 等于 n-1 才能确定的。
如果a打败c并且c打败b,那么a自然能打败b。
所以我们可以直接通过Floyd算法进行松弛操作。用map[i][j]表示i是否打败j。最后遍历所有的牛,判断有多少牛符合条件。

AC代码:

#include <iostream>
#include <algorithm>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int MAXN = 105;
int map[MAXN][MAXN];

int main()
{
    SIS;
    int n,m,x,y;
    cin >> n >> m;
    for(int i=0;i<m;i++)
    {
        cin >> x >> y;
        map[x][y]=1;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                if(map[j][i]&&map[i][k])
                    map[j][k]=1;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int res=0;
        for(int j=1;j<=n;j++)
        {
            if(map[i][j] || map[j][i])
                res++;
        }
        if(res==n-1) ans++;
    }
    cout << ans << endl;
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104085055