POJ:3660 Cow Contest (传递闭包 + Floyd)

POJ 3660 http://poj.org/problem?id=3660

思路:

  • 传递闭包

  • 输入A > B,那么我们可以建立一套A —>B 的边。 然后求出传递闭包。

  • 判断一个人是否知道排名,如果知道大于它的人数+小于它的人数 == n-1那么就可以知道排名了。

    大于它的人数: g[u][v] 代表u 的等级大于 v (g[u][v] == 1代表有边)

    小于它的人数:g[v][u] 代表v 的等级大于v

Floyd代码实现

#ifndef ONLINE_JUDGE
#pragma warning(disalbe : 4996)
#endif
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100 + 5;
int n, m, u, v, g[N][N];
void floyd() {
	for (int k = 1; k <= n; ++k)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				g[i][j] |= g[i][k] & g[k][j];
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		cin >> u >> v;
		g[u][v] = 1;
	}
	floyd();
	
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int cnt = 0;
		for (int j = 1; j <= n; j++) {
			if (g[j][i] || g[i][j]) cnt++;
		}
		if (cnt == n - 1) ans++;//如果cnt == n - 1说明于各点都连同了即排名可以写出
	}
	cout << ans << endl;
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("out.txt");
#endif // !ONLINE_JUDGE
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/RioTian/p/12917292.html