nyoj Cow Contest (传递闭包)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #include<climits>
11 #include<cmath>
12 #include<stdlib.h>
13 #include<vector>
14 #include<stack>
15 #include<set>
16 #define INF 2000000000
17 #define MAXN 110
18 #define maxn 1000010
19 #define Mod 1000007
20 #define N 1010
21 using namespace std;
22 typedef long long LL;
23 
24 int n, m;
25 bool G[MAXN][MAXN];
26 int u, v;
27 
28 void Floyd()
29 {
30     for (int k = 1; k <= n; ++k)
31         for (int i = 1; i <= n; ++i)
32             for (int j = 1; j <= n; ++j)
33                 G[i][j] |= G[i][k] & G[k][j];
34 }
35 
36 int main()
37 {
38     while (~scanf("%d%d", &n, &m), n + m) {
39         memset(G,0,sizeof(G));
40         for (int i = 0; i < m; ++i) {
41             scanf("%d%d", &u, &v);
42             G[u][v] = true;
43         }
44         Floyd();
45         int sum,ans = 0;
46         for (int i = 1; i <= n; ++i) {
47             sum = 0;
48             for (int j = 1; j <= n; ++j) {
49                 if (G[i][j] || G[j][i]) sum++;
50             }
51             if (sum == n - 1) ans++;
52         }
53         cout << ans << endl;
54     }
55     return 0;
56 }
代码君

 

posted @ 2015-03-15 20:28  UsedRose  阅读(104)  评论(0编辑  收藏  举报