Description

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 ≤ NA≠ 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.

Input

* 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 B

Output

* Line 1: A single integer representing the number of cows whose ranks can be determined
 

Sample Input

5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

2

题目意思:评定牛的等级,有n头牛,进行了m场比赛,位置在前面的是胜利的牛的编号,如果一头牛和剩下的牛都能确定等级关系,说明可以去确定该牛的等级
,求出可以确定等级的牛的个数 解题思路:这是一个利用最短路floyed算法的一个传递闭包问题,如果一个点和其余各点都确定关系了,那么这个点的等级就可以确定了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m;
int map[200][200];
int floyd()
{
int i,j,k;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
if(map[i][k]&&map[k][j])
{
map[i][j]=1;///如果任意两个点能够通过第三个点发生关系,那么说明这两个点也是有关系的
}
}
}
int main()
{
int a,b,i,j,count,sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
for(i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
map[a][b]=1;///可以确定关系的利用邻接矩阵记录为1
}
floyd();
count=0;
for(i=1; i<=n; i++)
{
sum=0;
for(j=1; j<=n; j++)
{
if(map[i][j]||map[j][i])
{
sum++;
}
}
if(sum==n-1)///如果一个点和其余各点的关系确定,那么这个点就可以确定等级
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}

  


Cow Contest(最短路floyed传递闭包)的更多相关文章

  1. 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 ...

  2. POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 9146 Desc ...

  3. Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 891  Solved: 590 ...

  4. POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )

    题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A  B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...

  5. (poj 3660) Cow Contest (floyd算法+传递闭包)

    题目链接:http://poj.org/problem?id=3660 Description N ( ≤ N ≤ ) cows, conveniently numbered ..N, are par ...

  6. POJ-3660 Cow Contest( 最短路 )

    题目链接:http://poj.org/problem?id=3660 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, ar ...

  7. POJ 3660 cow contest (Folyed 求传递闭包)

    N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we ...

  8. Median Weight Bead(最短路—floyed传递闭包)

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  9. Cow Contest POJ - 3660 floyd传递闭包

    #include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...

随机推荐

  1. Spring Cloud 微服务入门(一)--初识分布式及其发展历程

    分布式开发出现背景 当有计算机出现一段时间之后就开始有人去想如何将不同的电脑进行网络连接,而网络连接之后对于web的项目开发就探索所谓的分布式设计,同时人们也意识到重要的数据必须多份存在.所以分布式就 ...

  2. 转:Java中的cas

    引自:https://blog.csdn.net/mmoren/article/details/79185862 本篇的思路是先阐明无锁执行者CAS的核心算法原理然后分析Java执行CAS的实践者Un ...

  3. 消息队列MSMQ的使用

    1.MSMQ安装 控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定. 2.消息队列的应用场景(转载自http:/ ...

  4. 【读书笔记 - Effective Java】02. 遇到多个构造器参数时要考虑用构建器

    类有多个可选参数的解决方案: 1. 重叠构造器模式可行,但是当有许多参数的时候,客户端代码会很难编写,并且仍然较难以阅读. 2. JavaBeans模式,调用一个无参构造器来创造对象,然后调用sett ...

  5. js 中~~是什么意思?

    其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1~~false == 0~~"" == 0~~[] == 0 ~~undefined ==0~~!undef ...

  6. ubuntu下的python请求库的安装——Selenium,ChromeDriver,GeckoDriver,PhantomJS,aiohttp

    Selenium安装: pip3 install selenium ChromeDriver安装: 在这链接下载对应版本:https://chromedriver.storage.googleapis ...

  7. core dump文件分析和调试

    core介绍 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成"核心转储").我们可以认 ...

  8. [原创]用python检测LVS real server状态实现HTTP高可用

    import httplib import os import time def check_http(i): try: conn=httplib.HTTPConnection(i, 80, time ...

  9. Android Studio 引入 Git 并提交代码

    File -> Settings -> Version Control -> Git -> Path to Git executable -> 选择本地 Git 可执行文 ...

  10. Java设计模式(6)——创建型模式之原型模式(Prototype)

    一.概述 概念 // 引用自<Java与模式> UML图 第二种:登记式 二.实践 先导知识 对象的拷贝: 直接赋值:此时只是相当于a1,a2指向同一个对象,无论哪一个操作的都是同一个对象 ...