POJ3660 Cow Contest【關係閉包】

Cow Contest
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20736 Accepted: 11476

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

Sample Output

2

Source
USACO 2008 January Silver

問題鏈接POJ3660 Cow Contest
問題簡述:給定n頭牛和m對關係<a, b>表示牛a能打敗牛b,關係是可傳遞的。 計算能確定多少牛的排名?
問題分析:關係閉包Floyd算法實現。
程序說明:(略)
參考鏈接:(略)
題記:(略)

AC的C++語言程序如下:

/* POJ3660 Cow Contest */

#include <iostream>
#include <bitset>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100;
bitset<N + 1> g[N + 1];

int main()
{
    int n, m, u, v;
    while(~scanf("%d%d", &n, &m)) {
        for(int i = 1; i <= n; i++)
            g[i].reset();

        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &u, &v);
            g[u][v] = 1;
        }

        // 位串實現關係閉包計算
        for(int u = 1; u <= n; u++)
            for(int v = 1; v <= n; v++)
                if(g[v][u]) g[v] |= g[u];

        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int cnt = 0;
            for(int j = 1; j <= n; j++)
                if(g[i][j] || g[j][i]) cnt++;
            if(cnt == n - 1) ans++;
        }

        printf("%d\n", ans);
    }

    return 0;
}

AC的C++語言程序如下:

/* POJ3660 Cow Contest */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100;
int g[N + 1][N + 1];

void floyd(int n)
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(g[i][j]) {
                for(int k = 1; k <= n; k++)
                    if(g[k][i]) g[k][j] = 1;
            }
}

int main()
{
    int n, m, u, v;
    while(~scanf("%d%d", &n, &m)) {
        memset(g, 0, sizeof(g));

        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &u, &v);
            g[u][v] = 1;
        }

        floyd(n);

        int ans = 0;
        for(int i = 1; i <= n; i++) {
            int cnt = 0;
            for(int j = 1; j <= n; j++)
                if(g[i][j] || g[j][i]) cnt++;
            if(cnt == n - 1) ans++;
        }

        printf("%d\n", ans);
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章