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

思路:原来是觉得像昨天写的那题一样用bellman判断一下能不能成环

但是这道题问的不只是一只牛 而是所有牛 而且成环不成环并不能判断他的排名能不能确定

对于一头牛 如果我们知道有x只牛能赢他 他能赢y只牛 并且x+y=n-1的话 那么他的排名是可以确定的

所以就用floyd跑一遍 就可以把传递的关系建立起来了

emmmm传递闭包?题解上有这么说 但是不是很理解有什么关系......因为用了floyd???

唉离散学了都忘光了 感觉之前学的真的都忘了

然后遍历每一头牛看看行不行

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#include<queue>
#include<stack>
#define inf 0x3f3f3f3f using namespace std; int n, m, d[105][105]; void floyd()
{
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(d[i][k] && d[k][j]){
d[i][j] = 1;
}
}
}
}
} int main()
{
while(cin>>n>>m){
for(int i = 0; i < m; i++){
int a, b;
cin>>a>>b;
d[a][b] = 1;
}
floyd();
int ans = 0;
for(int i = 1; i <= n; i++){
int num = 0;
for(int j = 1; j <= n; j++){
if(d[i][j] || d[j][i]){
num++;
}
}
if(num == n - 1){
ans++;
}
} cout<<ans<<endl;
}
return 0;
}

最新文章

  1. Java_正则表达式
  2. Golang通过Thrift框架完美实现跨语言调用
  3. Poj(1325),最小点覆盖
  4. apiCloud结合layer实现动态数据弹出层
  5. java基础之 创建对象的几种方式
  6. ssm开发的一点小技巧
  7. *[topcoder]LCMSetEasy
  8. Android Bundle的使用
  9. .TextOut文字保存为图片
  10. Windows 8.1 Hyper-V安装的虚拟机
  11. 推荐的五款市面上常用的免费CMS建站系统
  12. 当图片验证码遇上JSP
  13. Java代码优化小结(三)
  14. js 前端有消息了 声音提示给用户
  15. Cocos2d-js 3.0 颜色变换(调整sprite/图片的色调)
  16. OAuth2.0 授权的工作原理
  17. Beaglebone Black教程Beaglebone Black中的Cloud9 IDE基本使用
  18. [实战]MVC5+EF6+MySql企业网盘实战(2)——验证码
  19. win8.1下右下角出现大小写切换状态显示框解决方案
  20. DOM API querySelector与querySelectorAll的用法

热门文章

  1. git链接github仓库
  2. OpenVPN多处理之-多队列TUN多实例
  3. 8 -- 深入使用Spring -- 1...4 重写占位符配置器
  4. 【代码审计】CLTPHP_v5.5.3后台任意文件删除漏洞分析
  5. RF-获取上个月份
  6. HttpClient(四)-- 使用代理IP 和 超时设置
  7. Linux init 命令
  8. AliRedis单机180w QPS, 8台服务器构建1000w QPS Cache集群
  9. Apache Kafka 0.11版本新功能简介
  10. inux跟踪线程的方法:LWP和strace命令