POJ 3660 图传递闭包

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 ≤ AN; 1 ≤ BN; AB), 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

floyd拓扑判断连通性,如果出度+入度=n-1则可判断排名。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <cmath>
11 #include <stdlib.h>
12 #define N 105
13 using namespace std;
14 int n,m;
15 int g[N][N];
16 int u,v;
17 void floyd()
18 {
19     for(int k = 1;k<=n;k++)
20         for(int i = 1;i<=n;i++)
21             for(int j = 1;j<=n;j++)
22                 g[i][j] |= g[i][k]&&g[k][j];
23 }
24 int main()
25 {
26     //freopen("caicai.txt","r",stdin);
27     scanf("%d%d",&n,&m);
28     for(int i = 0;i<m;i++)
29     {
30         scanf("%d%d",&u,&v);
31         g[u][v] = 1;
32     }
33     floyd();
34     int ans = 0;
35     for(int i = 1;i<=n;i++)
36     {
37         int cnt = 0;
38         for(int j = 1;j<=n;j++)
39         {
40             if(i==j) continue;
41             if(g[i][j]||g[j][i])
42                 cnt++;
43         }
44         if(cnt==n-1)
45             ans++;
46     }
47     cout<<ans<<endl;
48     return 0;
49 }
View Code

 


posted @ 2016-09-19 22:06  vegetable_vegetable  阅读(122)  评论(0编辑  收藏  举报