最短路径--Floyd--Cow Contest

最短路径–Floyd–Cow Contest

Description
N(1≤)N≤100)牛,方便编号1.N正在参加一场节目比赛。我们都知道,有些牛的编码比其他的要好。每头牛都有一定的技能等级,这在竞争对手中是独一无二的。

比赛进行了几轮面对面的比赛,每次是两头牛之间的比赛.如果牛A比牛有更高的技术水平B(1≤)A ≤ N;1≤B ≤ N; A ≠ B),然后是牛A总能打败牛B.

农夫约翰正试图按技术水平对奶牛进行排名。的结果M(1≤)M≤4,500)两轮牛轮,确定从结果可以精确确定的奶牛的数量。保证各轮会谈的结果不会相互矛盾。

Input
*第1行:两个空格分隔的整数:N和M
*第2行。M+1:每一行包含两个空格分隔的整数,描述竞争对手和结果(第一个整数,A)一轮比赛的胜利者:A和B

Output
*第1行:表示可以确定其等级的奶牛数量的单个整数

Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2

牛的排名由他打败的加上打败他的加起来为n-1来确定,使用cmp[ i ][ j ]来表示i是否打败j

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>

using namespace std;
const int inf=0x3f3f3f3f;
const int MAX=105;
int N,M;
int cmp[MAX][MAX];
int main()
{
	scanf("%d%d",&N,&M);
	memset(cmp,0,sizeof(cmp));
	int a,b;
	for(int i=0;i<M;i++)
	{
		cin>>a>>b;
		cmp[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(cmp[i][k]==1&&cmp[k][j]==1)
				{
					cmp[i][j]=1;					
				}
			}
		}	
	}
	int num=0;
	for(int i=1;i<=N;i++)
	{			int temp=0;
		for(int j=1;j<=N;j++)
		{
			if(cmp[i][j]==1||cmp[j][i]==1)				
			{
					temp=temp+1;
			} 
		}
		if(N-1==temp)		
		num=num+1; 		
	}
	cout<<num<<endl;
	return 0;
}
发布了19 篇原创文章 · 获赞 0 · 访问量 208

猜你喜欢

转载自blog.csdn.net/qq_45282116/article/details/104447433