Cow Contest

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

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.

输入
* 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

There 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算法,找到了每个点与其他点之间的关系,就能够确定该点与其他点关系的个数,如果等于n-1就说明该点的顺序是确定的。。。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int maxn = 110;
int n,m;
bool map[maxn][maxn];int main()
{   while(scanf("%d%d",&n,&m)!=EOF && m + n){memset(map,false,sizeof(map));int a,b;for(int i = 1; i <= m; i++){scanf("%d%d",&a,&b);map[a][b] = true;}for(int k = 1; k <= n; k++)for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)if(map[i][k] && map[k][j])map[i][j] = true;int ans = 0, cnt = 0;for(int k = 1; k <= n; k++){ cnt = 0;for(int i = 1; i <= n; i++){if(i == k) continue;if(map[i][k]) cnt++;if(map[k][i]) cnt++;}if(cnt == n-1) ans++;}printf("%d\n",ans);}return 0;
}

nyoj 211 (Floyd算法求传递闭包)相关推荐

  1. C语言用warshall算法求传递闭包transitive closure(附完整源码)

    用warshall算法求传递闭包transitive closure warshall算法求传递闭包完整源码 warshall算法求传递闭包完整源码 #include <stdbool.h> ...

  2. Floyd算法求无向图最小环

    原理可看菊苣博文:http://www.cnblogs.com/khan724/p/4383686.html 自己代码中解释一些小细节.该算法适用于无向图,而有向图的最小环,实际上就是初始化所有点为i ...

  3. 动态规划 - Floyd算法求最短路径 - (Matlab建模)

    Floyd算法又称为弗洛伊德算法.插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似.该算法名称以创始人之一.1978年图灵奖获得者.斯坦福大学计算 ...

  4. warshall算法求传递闭包c++_【建模小课堂】图论算法

    图论算法 图论算法在计算机科学中扮演着很重要的角色,它提供了对很多问题都有效的一种简单而系统的建模方式.很多问题都可以转化为图论问题,然后用图论的基本算法加以解决.这类问题算法主要包括Dijkstra ...

  5. Floyd算法求最短路径(附代码实例)

    Floyd算法 使用范围: 1)求每对顶点的最短路径; 2)有向图.无向图和混合图; 算法思想: 直接在图的带权邻接矩阵中用插入顶点的方法依次递推地构造出n个矩阵D(1), D(2), -, D(n) ...

  6. Floyd算法求最小环

    /**算法引入:*求一个图G中的最小环路的朴素算法为:每次找到一条边,删除了求这两点之间的最短路径;*若能求出,则这条最短路径与原来的边构成一个环,不过时间复杂度略高;**算法思想;*Floyd算法是 ...

  7. Floyd算法求最短路

    Floyd算法(基于动态规划):用于求多源汇最短路 初始化:用邻接矩阵d[i,j]存储中所有的边,floyd算法就是三重循环 for(k = 1;k <= n;k ++){ for(i = 1; ...

  8. Python Floyd算法求最短路径

    Floyd算法简介: Floyd算法 floyd算法(多源最短路径) python实现 例题 求下图各节点最短路径 Floyd算法如下: from math import * import numpy ...

  9. Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)

    一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...

最新文章

  1. 1050. 螺旋矩阵(25)
  2. 016_SpringBoot整合MyBatis
  3. 商品规格表设计_400㎡美容院装修设计,为什么说无中式不贵气?
  4. Go的strconv二
  5. 全链路压测构建高可用应用最佳实践
  6. 解决问题:HTTP 错误 401.1 - 未授权:登录失败【转】
  7. 生活在别处——“Samsung Cloud Print”云打印体验
  8. DWR第四篇之对象传参
  9. Python 2 和 3 的区别记录
  10. 武汉php东和,武汉探东之旅,未完成
  11. 宽带光纤接入网的概念和典型应用类型
  12. windows2016安装.net3.5错误:0x80070057;错误:0x800f081f
  13. 科技爱好者周刊(第 151 期):NFT 是什么,听说能赚钱
  14. 如何高效回复审稿意见?(附常用审稿意见回复模板)
  15. Batch Normalization介绍
  16. Unity实战——模拟太阳系
  17. Apache Jakarta 项目介绍
  18. 雨松MOMO 之 开始学习搭建界面自适应屏幕(一)
  19. Smart210学习记录------块设备
  20. 《仓库管理系统》主要功能展示

热门文章

  1. e 签宝携手神策数据,数据赋能智能办公产品服务双升级
  2. 【留用】C#的一些好的书籍
  3. 在Linux上自动调整屏幕亮度保护眼睛
  4. 【SSH网上商城项目实战20】在线支付平台的介绍
  5. 关闭Eclipse的控制台console自动跳出
  6. CPU和GPU的区别
  7. if you want to go to ruiyuan fund
  8. 【转】c++虚函数实现原理
  9. 易百教程人工智能python修正-人工智能NLTK性别发现器
  10. git 与团队协同开发,避免冲掉别人代码的方法