A - Cow Contest POJ - 3660

有n(1<=n<=100)个学生参加编程比赛。

给出m条实力信息。(1<=M<=4500)

其中每一条的格式为 A B (1<=A<=N,1<=B<=N,A!=B) 意思是A的实力比B强。

如果A比B强且B比C强,那么A一定比C强。

问最后有多少名学生可以确定他的排名。

保证输入信息不存在矛盾

Input

第一行n和m。

以下m行 A B 表示A实力比B强。

Output

输出答案
 

Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
#include<stdlib.h>
#include<stdio.h>
#include<iostream>  
#include<vector>  
#include <algorithm>
#include<math.h>

using namespace std; 

int main()
{
	int map[110][110];
	int n,m;//n名同学 m条信息
	scanf("%d%d",&n,&m);
	for(int i=0;i<105;i++)
		for(int j=0;j<105;j++)map[i][j]=0;
    for(int i=0;i<m;i++){
		int a,b;
		scanf("%d%d",&a,&b);// !没有0同学 数组从一开始
		map[a][b]=1;
	}
	for(int p=1;p<=n;p++){  //利用p同学松弛
		for(int i=1;i<=n;i++)
			for(int j=0;j<=n;j++){
				if(map[i][p]==1&&map[p][j]==1)
					map[i][j]=1;
			}
	}
	int time[105]={0};
	for(int p=1;p<=n;p++){
		for(int i=1;i<=n;i++){
			if(map[p][i]==1)time[p]++;
			if(map[i][p]==1)time[p]++;
		}
	}
	int s=0;
	for(int i=1;i<=n;i++)if(time[i]==(n-1))s++;
	printf("%d\n",s);
}

猜你喜欢

转载自blog.csdn.net/leo_10/article/details/79981885