传递闭包板子,震惊自己之前居然没学过,特此记录一下。
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, u, v, to[N][N];
int main () {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> u >> v;
to[u][v] = true;
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
to[i][j] = to[i][j] | (to[i][k] & to[k][j]);
//i -直接-> j or i -间接-> j
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
int tot = 0; //可确定关系数
for (int j = 1; j <= n; ++j) {
if (i == j) continue;
tot += (to[i][j] | to[j][i]);
}
ans += (tot == n - 1);
}
cout << ans << endl;
}
原文地址:https://www.cnblogs.com/maomao9173/p/10441030.html
时间: 2024-11-09 09:29:13