【BZOJ2429】[HAOI2006]聪明的猴子【最小生成树】

【题目链接】

求出最小生成树的最大边权,扫一遍,统计答案。

/* Pigonometry */
#include <cstdio>
#include <utility>
#include <algorithm>

using namespace std;

typedef pair<int, int> pii;

const int maxn = 1005, maxm = 505, maxg = 500005, inf = 0x3f3f3f3f;

int n, m, num[maxm], fa[maxn];
pii p[maxn];

struct _edge {
	int u, v, w;

	bool operator < (const _edge &x) const {
		return w < x.w;
	}
} g[maxg];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

inline int sqr(int x) {
	return x * x;
}

inline int dis(pii a, pii b) {
	return sqr(a.first - b.first) + sqr(a.second - b.second);
}

inline int calc() {
	int tot = 0;
	for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++)
		g[++tot] = (_edge){i, j, dis(p[i], p[j])};
	sort(g + 1, g + 1 + tot);

	int cnt = 0;
	for(int i = 1; i <= tot; i++) {
		int u = find(g[i].u), v = find(g[i].v);
		if(u != v) {
			++cnt;
			fa[u] = v;
			if(cnt == n - 1) return g[i].w;
		}
	}
	return inf;
}

int main() {
	m = iread();
	for(int i = 1; i <= m; i++) num[i] = iread();
	n = iread();
	for(int i = 1; i <= n; i++) fa[i] = i, p[i].first = iread(), p[i].second = iread();

	int bot = calc(), ans = 0;
	for(int i = 1; i <= m; i++) if(num[i] * num[i] >= bot) ans++;
	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(【BZOJ2429】[HAOI2006]聪明的猴子【最小生成树】)