(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
*/
(poj 3660) Cow Contest (floyd算法+传递闭包)的更多相关文章
- ACM: POJ 3660 Cow Contest - Floyd算法
链接 Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descri ...
- POJ 3660 Cow Contest (Floyd)
题目链接:http://poj.org/problem?id=3660 题意是给你n头牛,给你m条关系,每条关系是a牛比b牛厉害,问可以确定多少头牛的排名. 要是a比b厉害,a到b上就建一条有向边.. ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3660 Cow Contest(传递闭包floyed算法)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5989 Accepted: 3234 Descr ...
- POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 9146 Desc ...
- POJ - 3660 Cow Contest 传递闭包floyed算法
Cow Contest POJ - 3660 :http://poj.org/problem?id=3660 参考:https://www.cnblogs.com/kuangbin/p/31408 ...
- poj 3660 Cow Contest(传递闭包 Floyd)
链接:poj 3660 题意:给定n头牛,以及某些牛之间的强弱关系.按强弱排序.求能确定名次的牛的数量 思路:对于某头牛,若比它强和比它弱的牛的数量为 n-1,则他的名次能够确定 #include&l ...
随机推荐
- 详细理解servlet实现的几种方式和生命周期
现在很多的开发都是用的框架,然后很多同学学习的时候又是直接接触的框架,对于底层的一些开发,完全没有任何的了解.虽然对于业务上面来说,没有什么问题.但是很多时候当你被面试问到,或者是想要了解框架底层原理 ...
- java发布项目后注意小点,以及对于金额在java中的处理
项目在发布之后,有时会进行一些小的地方的修改,特别是对于一些常量的修改,如定义的一些特殊账户,第三方的key值,当修改的时候,我之前就偷懒过,因为项目在服务器上面,访问速度也受到限制,替换整个项目很麻 ...
- SELECT TOP 1 * FROM是什么意思
SELECT TOP 1 * FROM的含义: 1.select为命令动词,含义为执行数据查询操作: 2.top 1子句含义为查询结果只显示首条记录: 3.*子句表示查询结果包括数据源中的所有字段: ...
- hdu_2243_考研路茫茫——单词情结(AC自动机+矩阵)
题目链接:hdu_2243_考研路茫茫——单词情结 题意: 让你求包含这些模式串并且长度不小于L的单词种类 题解: 这题是poj2788的升级版,没做过的强烈建议先做那题. 我们用poj2778的方法 ...
- ADT(abstract data types)抽象数据类型
1.What is it? An abstract data type is a set of objects together with a set of operations. 抽象数据类型是带有 ...
- 中国内地、台湾、香港、澳门和国外DNS服务器地址列表
中国内地.台湾.香港.澳门和国外DNS服务器地址列表 详细内容 作者:网路岗 来源:局域网监控软件 DNS(Domain Name System)是域名解析服务器的意思,它在互联网的作用是把域名转换成 ...
- CentOS7 PostgreSQL 主从配置( 二)
同步流复制配置PostgreSql的流复制是异步的,缺点是Standby上的数据落后于主库上的数据,如果使用Hot Standby做读写分离,就会存在数据一致性的问题.PostgreSql9.1版本后 ...
- shell中exec解析
参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...
- 我为什么坚持DBA一定要懂开发
我为什么坚持DBA一定要懂开发时间 2016-03-23 15:34:08 张碧池的幸福生活原文 http://pottievil.com/我为什么坚持dba一定要懂开发/主题 DBA 数据库最近手头 ...
- php多进程实现
php多进程实现 PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能在nginx系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. ...