POJ-3660 Cow Contest

Problem 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

题意

有一群牛,他们之间两两进行比赛,现在只知道他们中某几个的输赢关系,问能确定几只牛的排名?

解法

这道题要用的传递闭包,只要把他们的传递闭包求出来(这里用到了Folyd-WarShall算法,因为若牛A打败了牛B,牛B又打败了牛C,那么可以知道牛A也能打败牛C,在这里就表示为可达),然后计算其中出度与入度之和为N-1的结点的数目就可以知道那几只牛的排名可以确定了(因为那只牛被某几只牛打败了,又打败了某几只牛,所以。。。)

Solution

#include<iostream>
using namespace std;
#define MAX 100
int G[MAX][MAX];
int WarShall(int G[][MAX], int n) {
	int count = 0;
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (G[i][k] == 1 && G[k][j] == 1) {
					G[i][j] = 1;
				}
			}
		}
    }

	for (int i = 0; i < n; i++) {
		int sum = 0;
		for (int j = 0; j < n; j++) {
			if (G[i][j] == 1||G[j][i]==1) {    //如果打败了谁或被谁打败了
				sum++;                         //也就是求出度与入度之和
				
			}
		}
		if (sum == n - 1) {
			count++;
		}
	}
	return count;
}
int main() {
	int n, m;
	int u, v;
	int result;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> u >> v;
		G[u-1][v-1] = 1;
	}
	result = WarShall(G, n);
	cout << result;
	system("pause");
}
posted @   裏表異体  阅读(109)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示