[USACO08JAN] Cow Contest S
题目描述
$ 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 N N( 1 ≤ N ≤ 100 1 \leq N \leq 100 1≤N≤100)头奶牛们最近参加了场程序设计竞赛。在赛场上,奶牛们按 1 , 2 , ⋯ , N 1, 2, \cdots, N 1,2,⋯,N 依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为 A A A 的奶牛的编程能力强于编号为 B B B 的奶牛 ( 1 ≤ A , B ≤ N 1 \leq A, B \leq N 1≤A,B≤N, A ≠ B A \neq B A=B),那么她们的对决中,编号为 A A A 的奶牛总是能胜出。 FJ 想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M M M( 1 ≤ M ≤ 4 , 500 1 \leq M \leq 4,500 1≤M≤4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。
输入格式
第一行两个用空格隔开的整数 N , M N, M N,M。
第 2 ∼ M + 1 2\sim M + 1 2∼M+1 行,每行为两个用空格隔开的整数 A , B A, B A,B ,描述了参加某一轮比赛的奶牛的编号,以及结果(每行的第一个数的奶牛为胜者)。
输出格式
输出一行一个整数,表示排名可以确定的奶牛的数目。
样例 #1
样例输入 #1
5 5
4 3
4 2
3 2
1 2
2 5
样例输出 #1
2
提示
样例解释:
编号为 2 2 2 的奶牛输给了编号为 1 , 3 , 4 1, 3, 4 1,3,4 的奶牛,也就是说她的水平比这 3 3 3 头奶牛都差。而编号为 5 5 5 的奶牛又输在了她的手下,也就是说,她的水平比编号为 5 5 5 的奶牛强一些。于是,编号为 2 2 2 的奶牛的排名必然为第 4 4 4,编号为 5 5 5 的奶牛的水平必然最差。其他 3 3 3 头奶牛的排名仍无法确定。
思路
如果这道题不说判断排名确定的话,那么我们可以用拓扑序判断环的思想。但本道题是要通过是否能判断出排名,因此我们就得用传递闭包/
拓展:传递闭包能解决的问题:排名问题,就那种给你大于小于的关系求出最总关系的。
- 注意:传递闭包的
f[i][j]
里面装的是 0 或者 1,当能传递过去就是 1。
代码(注释部分为最初的想法)
//成环无法判断
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N = 2010,M = 4510;
bool st[N];
int dist[N][N];
int n,m;
queue<int>q;
// void add(int a,int b){
// e[idx]=b,ne[idx]=h[a],h[a]=idx++;
// }
//这种思路是判断环
//但本道题是要判断出排名,就得用传递闭包
// void topsort(){
// for(int i=1;i<=n;i++){
// if(din[i]==1){
// q.push(i);
// st[i]=true;
// }
// }
// while(q.size()){
// int t=q.front();
// q.pop();
// for(int i=h[t];~i;i=ne[i]){
// int j=e[i];
// if((--din[j])==1){
// q.push(j);
// st[j]=true;
// }
// }
// }
// }
int main(){
cin>>n>>m;
// memset(h,-1,sizeof h);
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
dist[a][b]=1;//表示i能胜j
}
// topsort();
int res=0;
// for(int i=1;i<=n;i++){
// if(!st[i]){
// res++;
// }
// }
// cout<<n-res;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]|=dist[i][k]&dist[k][j];
}
}
}
for(int i=1;i<=n;i++){
int now=1;
for(int j=1;j<=n;j++){
if(i!=j){
now&=dist[i][j]|dist[j][i];//可以比较,因为这个值要么是1要么是0(不能比较就是0)
}
}
res+=now;
}
cout<<res;
return 0;
}