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

来源:互联网 发布:淘宝靠谱电玩店 编辑:程序博客网 时间:2024/05/22 08:27

题意:有n个牛, 他们之间有m个优劣关系, 求有多少个牛的优劣关系完全确定

思路:求出传递闭包后判断每个牛是否和其他所有牛都有关系,是的话ans++

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 110;int n, m, d[maxn][maxn];int main() {    while(~scanf("%d %d", &n, &m)) {        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= n; j++) {                d[i][j] = 0;            }        }        while (m--) {            int a, b; scanf("%d %d", &a, &b);            d[a][b] = 1;        }        for (int k = 1; k <= n; k++) {            for (int i = 1; i <= n; i++) {                for (int j = 1; j <= n; j++) {                    d[i][j] = d[i][j] || (d[i][k] && d[k][j]);                }            }        }        int ans = 0;        for (int i = 1; i <= n; i++) {            bool ok = true;            for (int j = 1; j <= n; j++) {                if (i != j && !d[i][j] && !d[j][i]) { ok = false; break; }            }            if (ok) ans++;        }        printf("%d\n", ans);    }}