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 的节点数量就好了
/*题目大意是说:给出牛之间的强弱关系,让你确定有多少头牛能够确定其排名。
dfs : 以每个顶点正向和反向(反向存图)分别遍历一次,就可以求出该顶点的出度和如度之和。满足 D入 + D出 == N-1,就可以确定其排名。参见离散数学。
floyd : relation[i][j] = 1 或者 relation[j][i] = 1 表示 i,j 之间有关系。若某一个顶点与其余 n-1个顶点都有关系的话,其排名是确定的。求任意两个顶点之间是否有关系可以用floyd算法。
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int N=106;
const int inf=0x3f3f3f;
int v[N][N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(v,0,sizeof(v));
for(int i=0; i<m; i++)
{
int a,b;
cin>>a>>b;
v[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(v[i][k]&&v[k][j])
v[i][j]=1;
}
int ans=0;
for(int i=1; i<=n; i++)
{
int sum=0;
for(int j=1; j<=n; j++)
sum+=v[i][j]+v[j][i];
if(sum==n-1)
ans++;
}
cout<<ans<<endl;
return 0;
}