POJ 3660 Cow Contest(floyd的小技巧)

原题地址;http://poj.org/problem?id=3660

题意:n个牛进行比赛,现已知m个关系, 牛u可以胜过牛v。 问最后可以确定排名位数的有几个牛

思路:我们可以转化一下思路,如果一头牛可以确定名次,那么名次比他高的牛和名次比他低的牛的数量相加是n-1.那么我们就可以将原问题转化为一个图论问题,.如果a赢了b,那就从a连一条边到b.最后只要一头一头牛判断就行了.

#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MOD 1e9+7
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 5;
int mp[1004][1005];
int n, m;
void floyd() {
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
            }
        }
    }
}
int main() {
    scanf("%d%d", &n, &m);
    memset(mp, 0x3f, sizeof(mp));
    for(int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        mp[u][v] = 1;
    }
    floyd();
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        int cnt = 0;
        int MAX = 0;
        for(int j = 1; j <= n; j++) {
            if(mp[j][i] != INF) cnt++;
            if(mp[i][j] != INF) cnt++;
        }
        if(MAX + cnt == n - 1) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/81263892