题目:
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.
输入:
<p>* Line 1: Two space-separated integers: <i>N</i> and <i>M</i><br>* Lines 2..<i>M</i>+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, <i>A</i>, is the winner) of a single round of competition: <i>A</i> and <i>B</i></p>
输出:
<p>* Line 1: A single integer representing the number of cows whose ranks can be determined<br> </p>
题目大意:N个牛进行比赛,现在已知的是M个比赛中,牛x>牛y;求能确定排名牛的数目;
思路:获得每两个牛的关系,如果这个牛胜过a个牛,输过b个牛,a+b=n-1,则可以确定;
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 101;
bool v[N][N];
int main()
{
int n, m;
scanf("%d%d",&n,&m);
memset(v, 0, sizeof(v));
for(int i=1; i<=m; i++)
{
int a, b;
scanf("%d%d",&a,&b);
v[a][b]=1;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
for(int k=1; k<=n; k++)
if(v[k][i]&&v[i][j])
v[k][j]=1; //至此确定所有牛关系;
int ans = 0;
for(int i=1; i<=n; i++)
{
int cnt = 0;
for(int j=1;j<=n;j++)
{
if(v[j][i])
cnt++;
if(v[i][j])
cnt++;
}
if(cn==n-1)
ans++;
}
printf("%d\n", ans);
return 0;
}