A - Cow Contest

A - Cow Contest

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 ≤ AN; 1 ≤ BN; AB), 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

题目描述:

有N头牛,有M行关系,M行中第一个可以打败第二个。问有多少头牛的名次可以确定。

分析:

假定我们已经确定好他们相对的排名(排名中有重复),比如有4头牛,4能打败2和3,2和3能打败1。那么它们的等级就是1是1级,2和3是2级,4是3级。像这样,等级没有重复的,我们就可以确定。

那么怎么找没重复的关系呢,我们可以给每头牛设一个上下限(就是这头牛可以放在他的上下限范围内的任何一级)。然后用一个桶记录每个等价有多少头牛。我们只要数出有多少个只有一头牛的等级就行。

确定他们的关系,我采用floyd,算出每两点之间最长距离。

注意:如果要用桶排序查找没有重复的,那么要注意桶的查找范围(即最低级到最高级)

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h> 
#define max(x,y) x>y?x:y;
#define min(x,y) x<y?x:y;
using namespace std;
const int INF=-100000; 
int dp[106][106];
int bot[106]={0};
struct range
{
    int top;
    int bto;
    range()
    {
        top=101;
        bto=0; 
    }
}cow[106];
int main()
{
    int n,m;
    int ans;
    scanf("%d%d",&n,&m);
    for(int i=0;i<=100;i++)
        for(int j=0;j<=100;j++)
        {
            dp[i][j]=INF;
        }
    for(int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        dp[b][a]=1;
    }
    
    //求2点间最长距离 
    for(int k=0;k<=n;k++)
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                {
                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
                }
    //求下限 
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            
            cow[i].bto=max(cow[i].bto,dp[j][i]);
        }
    }
    //最高的在第几级 
    int rangeM=0;
    //求上限 
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(dp[i][j]==1)
            cow[i].top=min(cow[i].top,cow[j].bto);
            rangeM=max(rangeM,cow[j].bto);
        }
    }
    /*
    for(int i=1;i<=n;i++)
    {
        printf("%d=%d-->%d\n",i,cow[i].bto,cow[i].top);
    }*/
    for(int i=1;i<=n;i++)
    {
        for(int j=cow[i].bto;j<cow[i].top;j++)
        {
            bot[j]++;
         } 
    }
    for(int i=0;i<=rangeM;i++)
    {
        if(bot[i]==1) ans++;
        //printf("%d ",bot[i]);
    }
    printf("%d\n",ans);
    return 0;
} 

 

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12219879.html