POJ 3660 Cow Contest——flody求传递闭包

题目大意:有n头牛,两头牛之间可以进行比赛,给出m场比赛的结果,问,有几头牛的战力排名可以确定?

典型的flody求传递闭包问题,之前没仔细想,各种花式拓扑排序,没弄出来。后来才想起是flody

若一场比赛,牛a战胜了牛b,则我们建立一条从a到b的有向边。把每场比赛我们都这么统计一下,就能得到一张有向图。我们分析一下,若a战胜b,b战胜c,则就相当于a战胜了c。所以我们可以对这张有向图,跑一次flody,然后再判断每个结点,是否与其它所有结点都有关联,若有,则这个结点就能确定其战力排名,否则就不能。

 1 #include <algorithm>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <iostream>
 5 #include <cstring>
 6 #include <map>
 7 #include <cmath>
 8 using namespace std;
 9 
10 const int INF = 0x3f3f3f3f;
11 const int maxn = 100 + 10;
12 int G[maxn][maxn];
13 
14 int main(){
15     int N, M;
16     scanf("%d%d", &N, &M);
17     for(int i = 0; i < M; ++i) {
18         int a, b;
19         scanf("%d%d", &a, &b);
20         G[a][b] = 1;
21     }
22 
23     for(int k = 1; k <= N; k++)
24         for(int i = 1; i <= N; ++i)
25             for(int j = 1; j <= N; ++j)
26                 G[i][j] = G[i][j] || (G[i][k] && G[k][j]);
27 
28     int ans = 0;
29     for(int i = 1; i <= N; ++i){
30         bool flag = true;
31         for(int j = 1; j <= N; ++j)
32             if(i != j && !G[i][j] && !G[j][i]) {
33                 flag = false;
34                 break;
35             }
36         if(flag) ans++;
37     }
38 
39     printf("%d
", ans);
40     return 0;
41 }
View Code
原文地址:https://www.cnblogs.com/DynastySun/p/9360851.html