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
题意: n 头牛 之间进行编程竞赛 , 按照技能水平的高低决胜负,水平高的获胜,给出 m 组 比赛结果(每组结果分别是两头牛进行比赛(A 和 B) 胜利的放在前面 (A)) 问有多少头牛可以确定 能力水平 。 思路: (最后一句问题亮了 mmp 我怎么知道你技能水平怎么判断),仔细思考发现如果可以确定一头牛的技能水平 则 这头牛 必须和其他牛 直接或者间接(通过其他牛) 的存在胜负关系 然后问题就可以转化成 :扩展每个节点和其他节点的胜负关系,然后 节点和其他节点存在的 胜负关系数量 为 n - 1 , 则可以确定此牛的 技能等级关系 。 再之后问题转化成 , 多源最短路 floyd 算法的 伸展过程(逐渐确定节点之间关系),最后统计一下 胜负关系数量为 n - 1 的节点数量就好了 /*题目大意是说:给出牛之间的强弱关系,让你确定有多少头牛能够确定其排名。
用Floyd做,对每给的一个胜负关系连一条边,最后跑一次Floyd,然后判断一头牛所确定的关系是否是n-1次,若是,则这头牛的排名可以确定 */
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int N=1005;
const int inf=0x3f3f3f;
int mp[N][N];
int main()
{
memset(mp,0,sizeof(mp));
int n,m;
int a,b;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
mp[a][b]=1;
}
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(mp[i][k]&&mp[k][j])
mp[i][j]=1;
}
int ans=0;
for(int i=1; i<=n; i++)
{
int sum=0;
for(int j=1; j<=n; j++)
sum=sum+mp[i][j]+mp[j][i];
if(sum==n-1)
ans++;
}
printf("%d\n",ans);
return 0;
}