【例题题解】Cow Contest:floyed&传递闭包

题目描述

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.

FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。在赛场上,奶牛们按1…N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。 整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B) ,那么她们的对决中,编号为A的奶牛总是能胜出。 FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有 M(1 <= M <= 4,500)轮比赛的结果,希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。比赛结果保证不会自相矛盾。

Solution

这道题用 f l o y e d floyed floyed&传递闭包的思路: f [ i ] [ j ] f[i][j] f[i][j]表示i是否能打败j。
三重循环枚举, f [ i ] [ j ] ∣ = f [ i ] [ k ] &amp; &amp; f [ k ] [ j ] f[i][j]|=f[i][k]\&amp;\&amp;f[k][j] f[i][j]=f[i][k]&&f[k][j]
表示第i头牛能打败的牛所打败的牛一定被第i头牛打败。
即:i打败j,j打败k,则i打败k。
然后对于每一只奶酒,只要打败它或它能打败的奶牛个数为n-1说明排名能够确定。
代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=300;
int n,m,ans=0;
bool f[N][N];
inline void read(int &s)
{
	char c=getchar();s=0;
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') s=s*10+c-48,c=getchar();
}
int main(void)
{
    read(n),read(m);
    for (int i=1;i<=m;++i)
    {
    	int a,b;
    	read(a),read(b);
    	f[a][b]=1;
	}
	for (int k=1;k<=n;++k)
	    for (int i=1;i<=n;++i)
	        for (int j=1;j<=n;++j)
	            f[i][j]|=f[i][k]&&f[k][j];
	for (int i=1;i<=n;++i)
	{
		int sum=0;
		for (int j=1;j<=n;++j)
		    if (j!=i && (f[j][i] || f[i][j])) sum++;
		if (sum==n-1) ans++;
	}
	cout<<ans<<endl;
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值