本文主要是介绍POJ 3660 Cow Contest(floyd传递闭包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 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 Output * Line 1: A single integer representing the number of cows whose ranks can be determined Sample Input Sample Output Source USACO 2008 January Silver |
题意:
n头牛比赛,m种比赛结果,最后问你一共有多少头牛的排名被确定了,其中如果a战胜b,b战胜c,则也可以说a战胜c,即可以传递胜负。求能确定排名的牛的数目。
分析:
名次确定:
如果一头牛被x头牛打败,打败y头牛,且x+y=n-1,则我们容易知道这头牛的排名就被确定了。
求解:
简单的floyd传递闭包算法,计算出每个顶点的出度与入度 (出度+入度=顶点数-1,则能够确定其编号)。
传递闭包的定义:G的传递闭包定义为G*=(V,E*),其中E={(i,j):图G中存在一条从i到j的路径。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 305
using namespace std;
int n,m;
int mp[maxn][maxn];
int in[maxn];
int out[maxn];
int main()
{scanf("%d%d",&n,&m);int a,b;memset(mp,0,sizeof(mp));for(int i=1; i<=m; i++){scanf("%d%d",&a,&b);mp[a][b]=1;}for(int k=1; k<=n; k++)for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)mp[i][j]|=(mp[i][k]&mp[k][j]);memset(in,0,sizeof(in));memset(out,0,sizeof(out));for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)if(mp[i][j]==1&&i!=j){out[i]++;in[j]++;}int ans=0;for(int i=1; i<=n; i++)if(out[i]+in[i]==n-1)ans++;printf("%d\n",ans);return 0;
}
这篇关于POJ 3660 Cow Contest(floyd传递闭包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!