[USACO08JAN]牛大赛Cow Contest

                             

                 [USACO08JAN]牛大赛Cow Contest

 

题目背景

[Usaco2008 Jan]

题目描述

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.

FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。在赛场上,奶牛们按1..N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B) ,那么她们的对决中,编号为A的奶牛总是能胜出。 FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M(1 <= M <= 4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。

输入输出格式

输入格式:

第1行: 2个用空格隔开的整数:N 和 M

第2..M+1行: 每行为2个用空格隔开的整数A、B,描述了参加某一轮比赛的奶 牛的编号,以及结果(编号为A,即为每行的第一个数的奶牛为 胜者)

输出格式:

第1行: 输出1个整数,表示排名可以确定的奶牛的数目

输入输出样例

输入样例#1:
5 5
4 3
4 2
3 2
1 2
2 5
输出样例#1:
2

说明

输出说明:

编号为2的奶牛输给了编号为1、3、4的奶牛,也就是说她的水平比这3头奶

牛都差。而编号为5的奶牛又输在了她的手下,也就是说,她的水平比编号为5的

奶牛强一些。于是,编号为2的奶牛的排名必然为第4,编号为5的奶牛的水平必

然最差。其他3头奶牛的排名仍无法确定。

一开始觉得统计入度,从入度为零的点跑SPFA,找最长

结果只有10分TAT

重新看了题目才明白

想确定一头牛的排名,你必须知道其他所有牛和这头牛的关系

所以我们可以用Flody处理牛与牛之间的关系,在统计。

 1 #include <algorithm>
 2 #include <ctype.h>
 3 #include <cstdio>
 4 #include <queue>
 5 
 6 const int MAXN=110;
 7 const int MAXM=4510;
 8 const int INF=0x3f3f3f3f;
 9 
10 int n,m,ans;
11 
12 int f[MAXN][MAXN],ru[MAXN];
13 
14 inline void read(int&x) {
15     int f=1;register char c=getchar();
16     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
17     for(;isdigit(c);x=x*10+c-48,c=getchar());
18     x=x*f;
19 }
20 
21 inline int max(int x,int y) {
22     return x>y?x:y;
23 }
24 
25 int hh() {
26     read(n);read(m);
27     for(int x,y;m--;) {
28         read(x);read(y);
29         f[x][y]=1;
30     }
31     for(int k=1;k<=n;++k) 
32       for(int i=1;i<=n;++i)
33         for(int j=1;j<=n;++j)
34           f[i][j]=f[i][k]&f[k][j]|f[i][j];
35     for(int i=1;i<=n;++i)
36       for(int j=1;j<=n;++j)
37         if(f[i][j]) ++ru[i],++ru[j];
38     for(int i=1;i<=n;++i)
39       if(ru[i]==n-1) ++ans;
40     printf("%d
",ans);
41     return 0;
42 }
43 
44 int sb=hh();
45 int main() {}
代码


作者:乌鸦坐飞机
出处:http://www.cnblogs.com/whistle13326/
新的风暴已经出现 怎么能够停止不前 穿越时空 竭尽全力 我会来到你身边 微笑面对危险 梦想成真不会遥远 鼓起勇气 坚定向前 奇迹一定会出现

 
原文地址:https://www.cnblogs.com/whistle13326/p/7421153.html