题目传送门
题目描述:
|英文|
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)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。
分析:做一遍floyed预处理出所有牛之间的关系,然后对于每一头牛进行枚举,如果与当前的牛确定关系的牛的数目是n-1个(除了它之外的所有牛),那么显然这头牛是能够确定排名的,就可以累加。最后输出个数即可
floyed的传递闭包:
f
[
i
]
[
j
]
∣
=
f
[
i
]
[
k
]
&
&
f
[
k
]
[
j
]
f[i][j]|=f[i][k]\&\&f[k][j]
f[i][j]∣=f[i][k]&&f[k][j]
那么具体代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int n,m;
bool f[401][401];
int ans=0;
int main(){
scanf("%d %d",&n,&m);
for (int i=1,x,y;i<=m;i++) scanf("%d %d",&x,&y),f[x][y]=1;
for (int k=1;k<=n;k++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
f[i][j]|=f[i][k]&&f[k][j];
for (int i=1;i<=n;i++){
int sum=0;
for (int j=1;j<=n;j++) sum+=f[i][j]||f[j][i];
ans+=sum==n-1;
}
printf("%d",ans);
return 0;
}