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.
* 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
* Line 1: A single integer representing the number of cows whose ranks can be determined
题目大意:FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。在赛场上,奶牛们按1..N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B) ,那么她们的对决中,编号为A的奶牛总是能胜出。 FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M(1 <= M <= 4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。
思路:以为你n<=100,所以可用floyd算法; 用floyd算法遍历,确定没头牛之间的关系;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int maps[105][105];
int main()
{
int n,m;
int i,j,k;
while(~scanf("%d %d",&n,&m))
{
memset(maps,0,sizeof(maps));//将所有的点初始为0
int x,y;
int ans=0,sum=0;
while(m--)
{
scanf("%d %d",&x,&y);
maps[x][y]=1;//1代表x的能力强于y
}
for(k=1; k<=n; k++)
{
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(maps[i][k]==1 && maps[k][j]==1)
maps[i][j]=1;//传递每头牛之间的关系
}
}
}//floyd
for(i=1; i<=n; i++)
{
sum=0;
for(j=1; j<=n; j++)
{
if(maps[i][j]==1)
sum++;
}
for(j=1; j<=n; j++)
{
if(maps[j][i]==1)
sum++;
}
if(sum==n-1)//当时我就不理解为什么sum==n-1时ans++,现在明白了,
ans++;//如果第i头牛与其它的牛都有输赢关系的话,它本身也就确定了
}
printf("%d\n",ans);
}
return 0;
}