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 ≤ N; A ≠ 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个关系,每个关系(a b)告诉你,a比b水平高,问你最后有多少人水平是可以确定的。
思路:刚开始一开这题意就莽拓扑排序,后来发现不对,(想着如果入队多个,说明无序,入队一个就有序,简直睿智)
其实是用floyd计算传递闭包,最后看对每个人是否关系都确定了。
(因为我图中只记录了,谁比谁等级高,所以闭包出来,也只有该点是否比其他点等级高,例如 maps【a】【b】,显然缺少一种别点比他高的情况,其实就是maps【b】【a】)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<iostream> 3 4 int maps[105][105]; 5 6 int n,m; 7 int main() 8 { 9 scanf("%d%d",&n,&m); 10 for(int j=1; j<=m; j++) 11 { 12 int u,v; 13 scanf("%d%d",&u,&v); 14 maps[u][v] = 1; 15 } 16 for(int k=1; k<=n; k++) 17 { 18 for(int i=1; i<=n; i++) 19 { 20 for(int j=1; j<=n; j++) 21 { 22 maps[i][j] |= maps[i][k] && maps[k][j]; 23 } 24 } 25 } 26 int num=n; 27 // for(int i=1;i<=n;i++) 28 // { 29 // for(int j=1;j<=n;j++) 30 // { 31 // printf("%d ",maps[i][j]); 32 // } 33 // puts(""); 34 // } 35 for(int i=1; i<=n; i++) 36 { 37 for(int j=1; j<=n; j++) 38 { 39 if(j == i)continue; 40 if(!maps[i][j] && !maps[j][i]) 41 { 42 num--; 43 break; 44 } 45 } 46 } 47 printf("%d\n",num); 48 }