Cow Contest
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 14306
Accepted: 8013
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 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.
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
题目给出了每头牛的对决的胜负情况,问有多少头牛的排名可以被确定。
很明显如果有一头牛知道了有m头牛赢了它,有n头牛输给他,并且m+n==所有牛的数量-1.简单的说,就是我必须知道除了我以外其他所有人的和我比较的情况,包括直接比较的和间接比较的。那么现在我只知道直接比较的情况,但是1赢了2,2赢了3.显然1也赢了3。因此主要的目标就是找出所有人两两之间的胜负情况。
说实话每次最短路径的算法都刷新我的认知,本题便使用Flody来试探路径的连通性。
#include <cstdio>
#include <iostream>
using namespace std;
void flody(int map[][105],int m)
{
for (int k = 1;k <= m; k++){
for (int i = 1;i <= m; i++){
for (int j = 1;j <= m; j++){
map[i][j] = map[i][j] || (map[i][k] && map[k][j]);
}
}
}
}
int main ()
{
int m,n;
while (~scanf ("%d%d",&m,&n)){
int map[105][105] = {0};
for (int i = 1;i <= n; i++){
int a,b;
cin >> a >> b;
map[a][b] = 1;//map[a][b] = 1,意味着a赢了b
}
flody(map,m);
int ans = 0;
for (int i = 1;i <= m; i++){
int cut = 0;
for (int k = 1;k <= m; k++){
if (map[i][k] || map[k][i])//无论胜负,只需要知道可以确定胜负关系即可
cut++;
}
if (cut == m-1)//其他所有人和自己的胜负关系都可以确定那么本身的排名一定可以确定
ans++;
}
cout << ans << endl;
}
return 0;
}