Luogu P2419 [USACO08JAN]牛大赛Cow Contest

传递闭包板子,震惊自己之前居然没学过,特此记录一下。

#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;
} 

posted @ 2019-02-26 23:46  maomao9173  阅读(96)  评论(0编辑  收藏  举报