您的位置:首页 > 其它

POJ 3660 Cow Contest

2015-09-16 13:00 155 查看
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的强,那么最终可以确定名次的奶牛个数是多少。(又是奶牛,真是偏爱啊~~)
分析一下,我们先进行一下最短路径的计算(用Floyd算法最合适),那么只要G[i][j]不为INF,就代表i的能力比j的强,那么当不为INF都情况=n-1时,表明这个奶牛和其它奶牛的关系确定了,那么它的名次也就出来了,统计这样的奶牛个数就OK了。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int N=110;
const int INF=0x3f3f3f3f;

int G

, n;

void Init()
{
int i, j;

for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
G[i][j] = INF;
G[i][i] = 0;
}
}

void Dist() ///用Floyd算法可以将每两个奶牛的关系表示出来
{
int i, j, k;

for (k = 1; k <= n; k++)
{
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
G[i][j] = min(G[i][j], G[i][k]+G[k][j]);
}
}
}

int main ()
{
int m, a, b, c
, ans, i, j;

while (scanf("%d%d", &n, &m) != EOF)
{
Init();
memset(c, 0, sizeof(c)); ///c数组保存确定关系的次数
ans = 0;

while (m--)
{
scanf("%d%d", &a, &b);
G[a][b] = 1;
}

Dist();

for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (G[i][j] != INF && i != j) ///要是相等了就没有意义了
{
c[i]++;
c[j]++;
}
}
}

for (i = 1; i <= n; i++)
{
if (c[i] == n-1)
ans++;
}

printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: