POJ-3660 Cow Contest
Posted urahyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");
以上是关于POJ-3660 Cow Contest的主要内容,如果未能解决你的问题,请参考以下文章
POJ3660:Cow Contest(Floyd传递闭包)
POJ-3660.Cow Contest(有向图的传递闭包)