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

  1. 5 5
  2. 4 3
  3. 4 2
  4. 3 2
  5. 1 2
  6. 2 5

Sample Output

  1. 2
  2.  
  3. 题目意思:评定牛的等级,有n头牛,进行了m场比赛,位置在前面的是胜利的牛的编号,如果一头牛和剩下的牛都能确定等级关系,说明可以去确定该牛的等级
    ,求出可以确定等级的牛的个数
  4.  
  5. 解题思路:这是一个利用最短路floyed算法的一个传递闭包问题,如果一个点和其余各点都确定关系了,那么这个点的等级就可以确定了。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int n,m;
  6. int map[200][200];
  7. int floyd()
  8. {
  9. int i,j,k;
  10. for(k=1; k<=n; k++)
  11. for(i=1; i<=n; i++)
  12. for(j=1; j<=n; j++)
  13. {
  14. if(map[i][k]&&map[k][j])
  15. {
  16. map[i][j]=1;///如果任意两个点能够通过第三个点发生关系,那么说明这两个点也是有关系的
  17. }
  18. }
  19. }
  20. int main()
  21. {
  22. int a,b,i,j,count,sum;
  23. while(scanf("%d%d",&n,&m)!=EOF)
  24. {
  25. memset(map,0,sizeof(map));
  26. for(i=0; i<m; i++)
  27. {
  28. scanf("%d%d",&a,&b);
  29. map[a][b]=1;///可以确定关系的利用邻接矩阵记录为1
  30. }
  31. floyd();
  32. count=0;
  33. for(i=1; i<=n; i++)
  34. {
  35. sum=0;
  36. for(j=1; j<=n; j++)
  37. {
  38. if(map[i][j]||map[j][i])
  39. {
  40. sum++;
  41. }
  42. }
  43. if(sum==n-1)///如果一个点和其余各点的关系确定,那么这个点就可以确定等级
  44. {
  45. count++;
  46. }
  47. }
  48. printf("%d\n",count);
  49. }
  50. return 0;
  51. }

  

  1.  

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. Flask第三方组件之flask_session

    flask默认提供了session, 但是存在以下问题: ① session数据存在客户端, 不安全 ② 大小有限制 ③ 增加了客户端的压力 所以才产生了很多第三方的session机制, 我使用的是f ...

  2. hadoop学习笔记——zookeeper平台搭建

    zookeeper是一个自动管理分布式集群的一个工具,以实现集群的高可用. 比如集群中的一个机器挂掉了,没有zookeeper的话就得考虑挂一个机器对剩下集群工作的影响,而有了zookeeper,它就 ...

  3. Python学习手册之内部方法、操作符重载和对象生命周期

    在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...

  4. 主存和cache的地址映射

    cache是一种高速缓冲寄存器,是为解决CPU和主存之间速度不匹配而采用的一项重要技术. 主存与cache的地址映射方式有全相联方式.直接方式和组相联方式三种. 直接映射(directmapping) ...

  5. 浅谈ConcurrentHashMap实现原理

    我们都知道HashTable是线程安全的类,因为使用了Synchronized来锁整张Hash表来实现线程安全,让线程独占: ConcurrentHashMap的锁分离技术就是用多个锁来控制对Hash ...

  6. Hotspot优化指南(上)

    一次偶然,博主在浏览docs.oracle.com/javase的时候发现了<Hotspot虚拟机垃圾收集调优指南>这篇文档.内心百感交集,之前在看完了周志明的<深入理解Java虚拟 ...

  7. Java 高级应用编程 第二章 集合

    一.Java 中的集合类 1.集合概述 Java中集合类是用来存放对象的 集合相当于一个容器,里面包容着一组对象 —— 容器类 其中的每个对象作为集合的一个元素出现 Java API提供的集合类位于j ...

  8. 20145226夏艺华 《Java程序设计》第1周学习总结

    http://www.cnblogs.com/bestixyh/p/5779286.html 去年暑假写的,确实比较丑陋,保留下来也是为了激励自己作出更多改变.寒假写的每一篇博客都尽最大努力养成了良好 ...

  9. 实现动态的XML文件读写操作(依然带干货)

    前言 最近由于项目需求,需要读写操作XML文件,并且存储的XML文件格式会随着导入的数据不同而随时改变(当然导入的数据还是有一定约束的),这样我们要预先定义好XML文件的格式就不太现实了,如何实现不管 ...

  10. stm32中如何进行printf重定向用于串口调试输出

    1 在main中包含stdio.h 文件 2 Target选项框里选Use MicroLib 选项 3 在main中添加UART1_Configuration()初始化的代码 Uart1初始化,voi ...