K - Cow Contest

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个已知的奶牛的关系,每个结果a,b代表a大于b,求排名确切的奶牛有几只。

解题思路:假设有三头奶牛a,b,c,已知a大于b,b大于c,从这里可以很快看出a大于c,所以该题的每次判断涉及了三头奶牛的关系,而题目的n<=100,于是可以想到Floyd将隐藏的奶牛关系表示出来,最后遍历二维数组计算奶牛出现的次数。另外,经过测试,位运算相比条件判断效率较高。

代码:

#include
using namespace std;
const int N=110;
int n,m;
int f[N][N]={0},cnt[N]={0};
int main()
{int i,j,k;scanf("%d%d",&n,&m);for(i=1;i<=m;i++){int a,b;scanf("%d%d",&a,&b);f[a][b]=1;}for(k=1;k<=n;k++){for(i=1;i<=n;i++){for(j=1;j<=n;j++){f[i][j]=f[i][j]|(f[i][k]&f[k][j]);}}}for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(f[i][j]){cnt[i]++;cnt[j]++;}}}int sum=0;for(i=1;i<=n;i++){if(cnt[i]==n-1)sum++;}printf("%d\n",sum);return 0;
} 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部