POJ 3660 Cow Contest【思维】

题目链接:http://poj.org/problem?id=3660

题意:N个奶牛,要进行M场比赛,每场比赛有两个奶牛A B,意思是A能打败B,问最后有几个奶牛的排名是确定的。

题解:
不得不说看到题目的时候一脸茫然啊…..
思考,如果A能打败B,B能打败C。那么A就能打败C。
这怎么那么像floyd啊..至少像一个类floyd的dp。
如果A能干掉B,那么就记为1,否则记为-1,用一个floyd来传递一下,最后数一数如果有p个人干掉了第i号奶牛,q个人被第 i 号奶牛干掉,如果p+q == n-1的话,就说明i号奶牛的排名是确定的了。码了一发就过了。

代码:

// by DenyTianly
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map> // STL
#include <string> 
#include <vector>
#include <queue>
#include <stack>
#define mpr make_pair

using namespace std;

typedef long long LL;

const int inf = 1 << 26;

int n, m;
int mp[105][105];

void floyd() {
    for ( int k = 0; k < n; k ++ ) {
        for ( int i = 0; i < n; i ++ ) {
            for ( int j = 0; j < n; j ++ ) {
                if(mp[i][j] != 0) continue;
                if(mp[i][k] == mp[k][j] && mp[i][k] != 0) mp[i][j] = mp[i][k];
            }
        }
    }
}

int main(){
//  freopen("POJ3660.in", "r", stdin);
    memset(mp, 0, sizeof(mp));
    scanf("%d %d", &n, &m);
    for ( int i = 0; i < m; i ++ ) {
        int x, y;
        scanf("%d %d", &x, &y);
        -- x; -- y;
        mp[x][y] = 1; mp[y][x] = -1;
    }
    floyd();

    int ans = 0;
    for ( int i = 0; i < n; i ++ ) {
        int ko = 0, koed = 0;
        for ( int j = 0; j < n; j ++ ) {
            if(i == j) continue;
            if(mp[i][j] == 1) ko ++;
            if(mp[i][j] == -1) koed ++;
        }

        if(ko+koed == n-1) ++ ans;
    }
    printf("%d\n", ans);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34896694/article/details/77505368