ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest

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;
}

  

时间: 2025-01-05 14:18:51

ACM: POJ 3660 Cow Contest - Floyd算法的相关文章

poj 3660 Cow Contest(warshall算法)

poj 3660 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 co

POJ 1125 Stockbroker Grapevine (Floyd算法)

Floyd算法计算每对顶点之间的最短路径的问题 题目中隐含了一个条件是一个人可以同时将谣言传递给多个人 题目最终的要求是时间最短,那么就要遍历一遍求出每个点作为源点时,最长的最短路径长是多少,再求这些值当中最小的是多少,就是题目所求 #include<bits/stdc++.h> using namespace std; int n,x,p,t; int m[120][120],dist[120][120],Max[120]; void floyd(int n,int m[][120],int

[ACM] POJ 3318 Matrix Multiplication (随机化算法)

Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16118   Accepted: 3485 Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? Input The first line of input contains a posit

nyoj-211-Cow Contest(floyd算法)

题目链接 1 /* 2 Name:nyoj-211-Cow Contest 3 Copyright: 4 Author: 5 Date: 2018/4/27 21:02:06 6 Description: 7 floyd算法 8 大佬的惊奇思路 9 */ 10 #include <iostream> 11 #include <cstdio> 12 #include <cstring> 13 using namespace std; 14 15 const int MAX

【POJ】3660 Cow Contest

题目链接:http://poj.org/problem?id=3660 题意:n头牛比赛,有m场比赛,两两比赛,前面的就是赢家.问你能确认几头牛的名次. 题解:首先介绍个东西,传递闭包,它可以确定尽可能多的元素之间的关系. 然后回到这道题,怎么能确认这头牛的名次,也就是不管它胜还是败都能推导出其他n-1头牛跟它的关系.具体思想看代码.QWQ 代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4

POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)

Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5611   Accepted: 2209 Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout

[ACM] POJ 3270 Cow Sorting (置换,贪心)

Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5946   Accepted: 2263 Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...

POJ 3615 Cow Hurdles (Floyd算法)

Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6142   Accepted: 2752 Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are

POJ3660:Cow Contest(Floyd传递闭包)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接:http://poj.org/problem?id=3660 Description: N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all k