(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 ...
随机推荐
- Chapter 1 First Sight——35
The final bell rang at last. I walked slowly to the office to return my paperwork. 最后下课铃响了.我走到了办公室上讲 ...
- java IO类图
- time_t
所在的头文件为 time.h 定义为: #ifndef __TIME_T #define __TIME_T /* 避免重复定义 time_t */ typedef long time_ ...
- Idea 设置根目录
1.在根目录下新建一个目录yx360-war-ctm-tea,在该目录下新建一个build.gradle文件,输入: apply plugin: 'war' 来引入war插件,war插件会在项目的目录 ...
- Hadoop YARN ERROR 1/1 local-dirs are bad *, 1/1 log-dirs are bad *
转 http://blog.csdn.net/u012303571/article/details/46913471 查看 nodemanager 日志发下 如下信息 2015-07-16 1 ...
- 萝卜德森的sublime笔记中文翻译版
我已经使用subliem编辑器版本2接近2个月了,并且我在其中找到了一堆有用的技巧.我发觉应该写下这些技巧,为那些对此感兴趣的人们.我会尽力的详细描述,那些看起来像魔法一样的东西,因为很多非常“酷”的 ...
- 笨方法学python--数字和数学计算
1 数学运算符号 + plus 加号 - minus 减号 / slash 除法 * asterisk 乘法 % percent 模除 求余 < less than 小于号 > great ...
- DB9 公头母头引脚定义及连接、封装
DB9 公头母头引脚定义及连接.封装 转自:http://blog.csdn.net/yangshuodianzi/article/details/8997478 1.实物及引脚简介 在做开发的时候经 ...
- 【jsp 分页】mysql limit方式进行分页
项目结构示意图: splitPage |-com.balfish.bean Goods.java |-com.balfish.dao GoodsDao.java |-com.bal ...
- boost库之graph入门
#include <boost/graph/undirected_graph.hpp> #include <boost/graph/adjacency_list.hpp> us ...