## 1821: [JSOI2010]Group 部落划分 Group (并查集)

2018年04月24日 ⁄ 综合 ⁄ 共 843字 ⁄ 字号 评论关闭
```#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x*f;
}

struct edge {
int x, y;
double v;
} e[1000001];
int n, k, cnt, tot, x[1001], y[1001], fa[1001];

inline bool cmp(edge a, edge b) {
return a.v < b.v;
}

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

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

inline double dis(int a, int b) {
return sqrt(sqr(x[a] - x[b]) + sqr(y[a] - y[b]));
}

int main() {
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
e[++cnt] = (edge){i, j, dis(i, j)};
sort(e + 1, e + cnt + 1, cmp);
for (int i = 1; i <= cnt; i++) {
int p = find(e[i].x), q = find(e[i].y);
if (p != q) {
if (n > k)n--, fa[p] = q;
else {
printf("%.2lf", e[i].v);
return 0;
}
}
}
return 0;
}```