POJ3660 Cow Contest(floyd算法应用)

Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8767   Accepted: 4933

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 ≤ NA ≠ 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

题目的任务是确定有多少个人的比赛排名可以确定,我们这里知道,如果我们确定了一个关系如下:

i个人能够打赢我,并且我知道我能打败j个人,并且i+j==n-1;那说明我的排名就已经确定了。那么我和别人的关系到底是如何的呢?我们这里就可以应用弗洛伊德算法来完成。从j到k的关系如果能能通过i确定关系的话,那么就能确定关系,核心代码列出:

        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])//如果j能够干掉i并且i能干掉k说明j能干掉k
                    map[j][k]=1;
                }
            }
        }
这里我们用弗洛伊德算法就能确定了所有人之间的关系(任意i和j的关系(这里我们应用过最短路算法的小伙伴们都应该很容易理解))。

然后按照关系入度:

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                degree[i]+=map[i][j];//两个方向都要入度(因为我们这里不考虑有向,只要加在一起等于n-1就行)
                degree[j]+=map[i][j];
            }
        }
        for(int i=1;i<=n;i++)if(degree[i]==n-1)output++;
        printf("%d\n",output);
最后我们上完整的AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int map[105][105];
int degree[105];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(degree,0,sizeof(degree));
        memset(map,0,sizeof(map));
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&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 output=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                degree[i]+=map[i][j];
                degree[j]+=map[i][j];
            }
        }
        for(int i=1;i<=n;i++)if(degree[i]==n-1)output++;
        printf("%d\n",output);
    }
}






















你可能感兴趣的:(poj,floyd,3,0,6,6,哈理工1018,POJ3660,3660)