Description
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种比赛结果,求一共有多少头牛的排名被确定了。
思路:如果一头牛与所有别的牛的比赛结果都确定,就是说这头牛的排名确定了,可用Floyd来做
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int MAX = 105;
const int INF = 0x3f3f3f3f;
struct A{
int vexnum;
int arcnum;
int arcs[MAX][MAX];
}G;
int dis[MAX][MAX]; //所有点之间的最短距离
//多源最短路Floyd算法
int Floyd(void){
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
dis[i][j] = G.arcs[i][j];
}
}
for(int k=1 ; k<=G.vexnum ; k++){
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
//关系要能传递(图的传递闭包)且i,j输赢未知
if((dis[i][k]+dis[k][j])==2 || (dis[i][k]+dis[k][j])==-2){
dis[i][j] = (dis[i][k]+dis[k][j]>0)?1:-1;
}
// if(dis[i][k]==dis[k][j] && (dis[i][k]==1 || dis[i][k]==-1)){
// dis[i][j]=dis[k][j];
// }
}
}
}
int ans=0;
for(int i=1 ; i<=G.vexnum ; i++){
int flag=0;
for(int j=1 ; j<=G.vexnum ; j++){
if(dis[i][j] != INF){
flag++;
}
}
//与每个对手都能确定输赢
if(flag == G.vexnum){
ans++;
}
}
return ans;
}
int main(void){
cin>>G.vexnum>>G.arcnum;
//初始化图
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
G.arcs[i][j] = (i==j)?0:INF;
}
}
//输入权值胜为1,败为-1,INF表示未知,0表示为自己
for(int i=1 ; i<=G.arcnum ; i++){
int x,y;
cin>>x>>y;
G.arcs[x][y] = 1;
G.arcs[y][x] = -1;
}
cout<<Floyd()<<endl;
return 0;
}