(poj 3660) Cow Contest (floyd算法+传递闭包)

题目链接:http://poj.org/problem?id=3660

Description

N ( ≤ N ≤ ) cows, conveniently numbered ..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 ( ≤ A ≤ N;  ≤ 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 ( ≤ M ≤ ,) 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 : Two space-separated integers: N and M
* Lines ..M+: 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 : A single integer representing the number of cows whose ranks can be determined
  Sample Input Sample Output Source
USACO January Silver

题目大意:有N头牛,每头牛都有个特技。有M场比赛,比赛形式是A B 意味着牛A一定能赢牛B,问在M场比赛后,有几头牛的名次是确定的(不互相矛盾)?

方法:这是最短路练习,但是以最短路的形式无法求出来,看了一下题解发现是最短路的floyd算法写的,这个算法的时间复杂度为O(n3),自己百度了一下这个算法,再求每个点的出度入度的和,如果等于n(算上自己),就能确定

 #include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 500
int a[N][N],G[N][N];
int main()
{
int n,m,e,v;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
a[i][j]=i==j?:;
}
for(int i=;i<m;i++)
{
scanf("%d %d",&e,&v);
a[e][v]=;
}
///floyd算法 五行代码 三层for循环 时间复杂度为O(n3)
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(a[i][j]||(a[i][k] && a[k][j]))
a[i][j]=;
}
}
}
///求每个点的出度入度之和
int ans=,sum=;
for(int i=;i<=n;i++)
{
sum=;
for(int j=;j<=n;j++)
{
if(a[i][j] || a[j][i])
sum++;
}
if(sum==n)
ans++;
}
printf("%d\n",ans);
}
return ;
}
/*
5 5
4 3
4 2
3 2
1 2
2 5
*/
上一篇:hadoop 参数调优重点参数


下一篇:Hadoop参数调优