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个关系,每次输入的x,y代表x胜过y,求出能够确定当前奶牛和其他所有奶牛的关系的奶牛有几头。
思路:对于每两个奶牛之间有三种关系, 1.没关系 2. a胜过b 3. a输给b,我们用dis[i][j] 代表第i只奶牛和第j只奶牛的关系。我们首先可以对开始输入的奶牛的关系建图,之后用floyed跑一遍图,遍历完所有点点之间的关系,最后判断每一个点,若与其他n-1个点都有关系则ans++
 #include<iostream>
#include<algorithm>
#include<vector>
#include<queue> using namespace std;
const int INF = 0x3f3f3f3f;
int dis[][];
int n, m;
void floyed()
{
for (int k = ; k <= n; k++)
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
if ((dis[i][k] == && dis[k][j] == ) || (dis[i][k] == && dis[k][j] == ))
dis[i][j] = dis[i][k]; //判断ij之间的关系
}
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n >> m) {
memset(dis, , sizeof(dis));
for (int a, b, i = ; i <= m; i++) {
cin >> a >> b;
dis[a][b] = ;//a胜b
dis[b][a] = ;//a输给b
}
floyed();
int ans = ;
for (int i = ; i <= n; i++) {
int cnt = ;
for (int j = ; j <= n; j++) {
if (i == j)continue;
if (dis[i][j])cnt++;
}
if (cnt == (n - ))ans++;
}
cout << ans << endl;
}
return ;
}

最新文章

  1. HTTP权威指南-基础知识
  2. 最小生成树——kruskal算法
  3. (C++) System return error codes.
  4. 数据结构算法C语言实现(十九)--- 5.5&amp;5.6&amp;5.7广义表
  5. C# 代码笔记
  6. Unity NGUI 2D场景添加按钮
  7. 【python】linux将python改为默认3.4版本
  8. 百度360争推1TB永久网盘
  9. 【转】NSDictionary以及NSMutableDictionary的用法
  10. ArrayList与LinkedList实现比较
  11. IIS 7 支持10万并发请求
  12. C#获得命令提示符输出
  13. [转]ORACLE递归查询
  14. html、text、val、attr、prop区别。this.value和$(this).val()区别以及return用法
  15. Java中数组、List、Set互相转换
  16. XStream进行xml和bean互转
  17. WRT 下 C++ wstring, string, String^ 互转
  18. ES6生成器函数generator
  19. python中的*arg和**kwargs
  20. 我仅使用到的dd if

热门文章

  1. oracle-ORA-00942错误
  2. JavaScript--开关思想
  3. Android开发-API指南-&lt;activity-alias&gt;[原创译文]
  4. Linux下的python安装
  5. Python学习之路5☞文件处理
  6. 中文乱码在java中URLEncoder.encode方法要调用两次解决
  7. [转]embedding technic:从SNE到t-SNE再到LargeVis
  8. AT2377 Blue and Red Tree
  9. toString和valueOf使得对象访问时显示一个特定格式的字符串,但是可以进行数字运算
  10. 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&amp;shell&amp;script&amp;copy