码迷,mamicode.com
首页 > 编程语言 > 详细

ACM: POJ 3660 Cow Contest - Floyd算法

时间:2016-08-11 00:23:11      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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≤ NA ≠ 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算法去判断n只牛相互的关系,如果某一只牛和另外n-1只牛都建成了关系,就说明这只牛已经嫩排除顺序了。

AC代码:
/*/

 



#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"

int cow[105][105];

void Floyd(int n) {
	for(int k=1; k<=n; k++) {
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=n; j++) {
				cow[i][j]=cow[i][j]||(cow[i][k]&&cow[k][j]);  //如果两头牛本身关系已经确定或者两头牛和某一头牛同时形成顺序关系,这两头牛的关系就被了确定。 
//				cout<<"["<<cow[i][j]<<"]";
			}
//			puts("");
		}
//		puts("");
	}
}

int main() {
	int n,m,w,l,num,ans;
	while(~scanf("%d%d",&n,&m)) {
		memset(cow,0);
		for(int i=0; i<m; i++) {
			scanf("%d%d",&w,&l);
			cow[w][l]=1;
		}
		Floyd(n);
		ans=0;
		for(int i=1; i<=n; i++) {
			num=0;
			for(int j=1; j<=n; j++) {
				if(cow[i][j]||cow[j][i])num++;//如果这头牛和其他牛的关系被确定就计数 。 
			}
			if(num==n-1)ans++;//如果该牛和其他所有的牛关系都确定了,这头牛的位置就已经找到了。 
		}
		printf("%d\n",ans);
	}
	return 0;
}

  

ACM: POJ 3660 Cow Contest - Floyd算法

标签:

原文地址:http://www.cnblogs.com/HDMaxfun/p/5759191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!