NYOJ 211 Cow Contest (弗洛伊德+传递闭包 )
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 (弗洛伊德+传递闭包 )的更多相关文章
- nyoj 211——Cow Contest——————【floyd传递闭包】
		
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
 - nyoj 211 Cow Contest
		
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=211 思路:我的思路是对每一个点,向上广搜,向下广搜,看总共能不能搜到n-1个结点,能,表 ...
 - POJ 3660 Cow Contest(传递闭包floyed算法)
		
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5989 Accepted: 3234 Descr ...
 - POJ3660——Cow Contest(Floyd+传递闭包)
		
Cow Contest DescriptionN (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...
 - POJ3660 Cow Contest —— Floyd 传递闭包
		
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
 - POJ 3660 Cow Contest【传递闭包】
		
解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...
 - POJ3660 Cow Contest  floyd传递闭包
		
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
 - poj 3660 Cow Contest (传递闭包)
		
/* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...
 - POJ 3660	Cow Contest 弗洛伊德
		
题意难懂是POJ的标配,这都TM赖本泽马. 题意:有N头牛进行了M场比赛,比赛双方是A - B 且总是A赢(前面的那个数赢),如果说A赢B,B赢C 则可以确定A赢C.问最终多少头牛的排名可以确定. 思 ...
 
随机推荐
- SSM框架学习思维导图
			
SSM框架学习思维导图 2017年08月11日 20:17:28 阅读数:1141 放上前段时间学习SSM框架以及Spring.SpringMVC.MyBatis的学习结果,输出思维导图一共四幅图.这 ...
 - Wannafly挑战赛21:C - 大水题
			
链接:Wannafly挑战赛21:C - 大水题 题意: 现在给你N个正整数ai,每个数给出一“好数程度” gi(数值相同但位置不同的数之间可能有不同的好数程度).对于在 i 位置的数,如果有一在j位 ...
 - Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建
			
相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.4.4-nts-Win32-VC9-x86.zip 如果是win2 ...
 - (转) Unreal Engine 4 Custom Shaders Tutorial
			
说明: 1.这里的Custom Shaders 为且仅为 Custom Node的使用和USF的包含.并非全局Shader和Material Shader. 2.原文来源:https://www.ra ...
 - hyperledger composer
			
hyperledger composer 网站搜集 https://hyperledger.github.io/composer/latest/introduction/introduction.ht ...
 - 从零开始搭建一个react项目
			
Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...
 - 测试理论- the conten of test plan
			
1 testing objects 测试对象 2 testing scope 测试范围 3 testing the frame (?) 4 the environment 5 reason for t ...
 - 点击查看大图Activity
			
1.使用方式 Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);//0,索 ...
 - c# 生成dll
			
进入项目属性栏里,选择输出类型为类库.
 - python基础之列表解析
			
python列表解析:是一个让人欣喜的术语,你可以在一行使用一个for循环将所有的值放在一个列表之中.python列表解析属于python的迭代中的一种,相比python for循环速度会快很多. e ...