title: Cow Contest 弗洛伊德+传递闭包 nyoj211

tags: [弗洛伊德,传递闭包]

题目链接

描述

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.

  • 输入

    • 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 BThere are multi test cases.The input is terminated by two zeros.The number of test cases is no more than 20.
  • 输出

    For every case:* Line 1: A single integer representing the number of cows whose ranks can be determined
  • 样例输入

    5 5

    4 3

    4 2

    3 2

    1 2

    2 5

    0 0
  • 样例输出

    2

分析:

使用Floyd算法来判断传递闭包。

首先使用Floyd算法来求出每两个点的最短路径

然后对于能否确定一个牛的位置的判断,我们假设现在一共有5个牛,对于牛A,如果有两个牛可以打败它并且它又可以打败其他的两个牛的话,我们就可以确定它的位置在3号。

所以对于N个牛,如果能打败它和它能打败的牛的总个数为N-1的话,那么它的位置就可以确定出来,这就叫做传递闭包(以前完全没有听说过)。

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
int N,M;
int Tu[102][102];
void Floyd()///弗洛伊德算法,三重for循环
{
for(int k=1; k<=N; k++)
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
{
if(i!=j&&Tu[i][k]!=INF&&Tu[k][j]!=INF&&Tu[i][j]>Tu[i][k]+Tu[k][j])
Tu[i][j]=Tu[i][k]+Tu[k][j];
}
}
int main()
{
int a,b;
while(~scanf("%d%d",&N,&M),N,M)
{
memset(Tu,INF,sizeof(Tu));
while(M--)
{
scanf("%d%d",&a,&b);
Tu[a][b]=1;///表示的是a能战胜b
}
Floyd();
int ans=0;
int sum=0;
for(int i=1; i<=N; i++)
{
sum=0;
for(int j=1; j<=N; j++)
{
if(i!=j)
{
if(Tu[i][j]!=INF)///i能战胜j
sum++;
if(Tu[j][i]!=INF)///j能战胜i
sum++;
}
}
if(sum==N-1)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

NYOJ 211 Cow Contest (弗洛伊德+传递闭包 )的更多相关文章

  1. nyoj 211——Cow Contest——————【floyd传递闭包】

    Cow Contest 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...

  2. nyoj 211 Cow Contest

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=211 思路:我的思路是对每一个点,向上广搜,向下广搜,看总共能不能搜到n-1个结点,能,表 ...

  3. POJ 3660 Cow Contest(传递闭包floyed算法)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3234 Descr ...

  4. POJ3660——Cow Contest(Floyd+传递闭包)

    Cow Contest DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...

  5. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  6. POJ 3660 Cow Contest【传递闭包】

    解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...

  7. POJ3660 Cow Contest floyd传递闭包

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  8. poj 3660 Cow Contest (传递闭包)

    /* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...

  9. POJ 3660 Cow Contest 弗洛伊德

    题意难懂是POJ的标配,这都TM赖本泽马. 题意:有N头牛进行了M场比赛,比赛双方是A - B 且总是A赢(前面的那个数赢),如果说A赢B,B赢C 则可以确定A赢C.问最终多少头牛的排名可以确定. 思 ...

随机推荐

  1. python接口测试(一)——http请求及token获取

    使用python对当前的接口进行简单的测试 1.接口测试是针对软件对外提供服务得接口得输入输出进行得测试,验证接口功能与接口描述文档得一致性 返回结果可以为字符串,json,xml等 2.接口的请求方 ...

  2. docker容器中启动kvm虚拟机

    .安装docker yum install docker systemctl start docker.service systemctl enable docker.service .拉取cento ...

  3. Linux e1000e网卡驱动

    目录 识别网卡 命令行参数 附加配置 技术支持 一.识别网卡e1000e驱动支持Intel所有的GbE PCIe网卡,除了82575,82576,基于82580系列的网卡.提示:Intel(R) PR ...

  4. linux 进程间通信之pipe

    在实际开发过程中,程序员必须让拥有依赖关系的进程集协调,这样才能达到进程的共同目标.  每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内 ...

  5. 第四次JAVA作业

    public class TvbDog { public static void main(String[] args) {  Dog per=new Dog("陈狗"," ...

  6. linux系统基础文件属性

    记录用户登录前显示的信息 cat /etc/issue vim  /etc/motd    设置登录提醒 隐藏执行命令的历史记录用history –d  加上历史记录行号 如history -d 38 ...

  7. PHP判断类型的方法

    1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...

  8. js阻止冒泡事件和默认事件的方法

    阻止默认事件 function stopDeFault(e){ if(e&&e.preventDefault){//非IE e.preventDefault(); }else{//IE ...

  9. SRM710 div1 MagicNim(博弈论)

    题目大意: 给出n+1堆石子,前n堆石子的数量是a[i],最后一堆只有1个石子,但是具有魔力 拿走该石子的一方可以选择接下来是进行普通的Nim游戏还是anti-nim游戏 问是先手必胜还是必败 首先拿 ...

  10. Splunk学习与实践

      一.  Splunk公司与产品 美国Splunk公司,成立于2004年,2012年纳斯达克上市,第一家大数据上市公司,荣获众多奖项和殊荣.总部位于美国旧金山,伦敦为国际总部,香港设有亚太支持中心, ...