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. c++ list_iterator demo

    #include <iostream> #include <list> using namespace std; typedef list<int> Integer ...

  2. php常见安全问题

    XSS攻击原理: XSS又叫CSS (Cross Site Script) ,跨站脚本攻击.它指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会 ...

  3. eclipse 创建Makefile工程生成多个执行文件

    1.创建Makefile工程 2.创建inc src Debug 目录 用于存放头文件源文件 3.编写Makefile 需要在有源文件的目标天剑Makefile文件,如下给出一个生成两个target的 ...

  4. 【转】webpack4

    1.不再支持node.js4.X 2.不能用webpack命令直接打包指定的文件,只能使用webpack.config.js进行配置. 即:webpack  demo01.js  bundle01.j ...

  5. [转]Hexo博客添加访问统计 - 记录

    引入不蒜子 <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"> ...

  6. sql声明变量存储查询结果

    with t as 查到条件数据,然后在下面使用到t,用exists做判断会非常慢,改成left join会快很多. 我使用的数据库时2008Sql r2. 文章:SQL数据库中临时表.临时变量和WI ...

  7. 《Effective STL》学习笔记

    http://www.cnblogs.com/arthurliu/archive/2011/08/07/2108386.html 作者:咆哮的马甲 出处:http://www.cnblogs.com/ ...

  8. C++ Profiler工具之初体验

    http://www.cnblogs.com/likwo/archive/2012/12/20/2826988.html 转 http://www.cnblogs.com/lenolix/archiv ...

  9. js计算当前日期上一个月和下一个月

    /**         * 获取上一个月         *         * @date 格式为yyyy-mm-dd的日期,如:2014-01-25         */        funct ...

  10. [剑指Offer] 23.二叉搜索树的后序遍历

    [思路]BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两 ...