最短路(floyd) Cow Contest

来源:互联网 发布:淘宝监控软件 编辑:程序博客网 时间:2024/04/27 20:48

Cow Contest

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 69   Accepted Submission(s) : 37
Problem 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 cowA has a greater skill level than cow B (1 ≤ AN; 1 ≤BN; AB), then cow A will always beat cowB.

Farmer John is trying to rank the cows by skill level. Given a list the results ofM (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 54 34 23 21 22 5
 

Sample Output
2
 题目含义:一群牛打架,求出可以判断名次的牛的个数
题目分析:
   1)一群牛打架,只有知接或者间接和其余所有的牛打架才能够判断名次
   2)多头牛,多个源点,直接用floyd()
     原因:数据量较少
  3)初始化:  memset(VS,0,sizeof VS);
      判断:   if(VS[i][k]!=0 && VS[k][j]!=0)
                  VS[i][j]=1;


#include<stdio.h>

#include<string.h>
#define maxn 105
#define INF 0x3f3f3f3f
int VS[maxn][maxn];
int n,m;
void floyd()
{
    int i,j,k;
  for(k=1;k<=n;k++)
      for(i=1;i<=n;i++)
          for(j=1;j<=n;j++)
          {
              if(VS[i][k]!=0 && VS[k][j]!=0)
                  VS[i][j]=1;
          }

}

int main()
{
    int a,b,i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int number=0;
        int cnt;
        memset(VS,0,sizeof VS);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            VS[a][b]=1;
        }
        floyd();
        for(i=1;i<=n;i++)
        {
            cnt=0;
            for(j=1;j<=n;j++)
            {
            if(VS[i][j]||VS[j][i])
            cnt++;
            }
            if(cnt==n-1)
                number++;
        }
        printf("%d\n",number);


    }
   
    return 0;
}
0 0
原创粉丝点击