Cow Contest/POJ 3660
题目
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.
题目来源:POJ 3660
题目链接
题意
有n头牛,他们的等级为1…n,(每头牛等级不一样),他们进行了M轮比拼,等级高的牛会赢,告诉你这M轮的结果,问有多少头牛的等级是可以确定地。
解法
一头牛的等级能够确定只有当它与其余n-1头牛的关系确定时才能确定,所以我们可以用M轮比拼的结果来构图,然后利用有点类似于Floyd的方法来确定牛之间的关系。
代码:
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
int a[110][110],n,x,y,sum,ans,m;
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
a[x][y]=1;a[y][x]=-1;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int k=1;k<=n;k++)
if (!a[i][j])
{
if (a[i][k]==1 && a[k][j]==1) a[i][j]=1,a[j][i]=-1;
if (a[i][k]==-1 && a[k][j]==-1) a[i][j]=-1,a[j][i]=1;
}
for (int i=1;i<=n;i++)
{
bool flag=true;
for (int j=1;j<=n;j++) if (!a[i][j] && i!=j) flag=false;
if (flag) ans++;
}
printf("%d",ans);
return 0;
}