POJ 3660 Cow Contest(floyd传递闭包)

2023-11-10 00:08

本文主要是介绍POJ 3660 Cow Contest(floyd传递闭包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Cow Contest

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18254 Accepted: 10129

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 ≤ NA ≠ 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

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传递闭包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/379254

相关文章

Closures(闭包)

Closures(闭包) 本节主要讲groovy中的一个核心语法:closurs,也叫闭包。闭包在groovy中是一个处于代码上下文中的开放的,匿名代码块。它可以访问到其外部的变量或方法。 1. 句法 1.1 定义一个闭包 { [closureParameters -> ] statements } 其中[]内是可选的闭包参数,可省略。当闭包带有参数,就需要->来将参数和闭包体相分离。

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

poj 2391 Ombrophobic Bovines (网络流)

这是一道很经典的网络流的题目。首先我们考虑假如我们的时间为无穷大。我们吧每个点拆成2个点 i和i' .。虚拟源点s和汇点t。对于每个点建边(s,i, a[i])  (i‘,t,ib[i]) 。 其中a[i]为给点有多少牛,b[i]为容量。i和j连通 建边 (i,j',inf);如果最大流==所有牛的个数,就可能装下所有的牛。那么现在我们考虑时间。假设最大时间为T.那么如果i到j的的最短时间>T

poj 1330 LCA 最近公共祖先

水题目。直接上代码了。 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点。实际上集合内的每个点是相同的,这样的集合就是一个强连通分量。 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图。在dag的图上面进行dp。可以先用拓扑排序后dp。或者建反响边记忆化搜索 。 VIEW

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

Maven的依赖传递、依赖管理、依赖作用域

在Maven项目中通常会引入大量依赖,但依赖管理不当,会造成版本混乱冲突或者目标包臃肿。因此,我们以SpringBoot为例,从三方面探索依赖的使用规则。 1、 依赖传递 依赖是会传递的,依赖的依赖也会连带引入。例如在项目中引入了spring-boot-starter-web依赖: <dependency><groupId>org.springframework.boot</groupId>

C#启动另外一个C#程序,并传递参数

第一个程序:             using System.ComponentModel; using System.IO;         private void button1_Click(object sender, EventArgs e)         {             string target = Path.GetDirectory