因为结论满足单调性,所以用二分。
我们用并查集合并,看看联通块的个数是否小于等于s。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 510;
const double eps = 1e-4;
int n, m, x[N], y[N], fa[N];
double d[N][N];
int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
bool check(double now) {
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++)
if (d[i][j] <= now) fa[findfa(i)] = findfa(j);
int sum = 0;
for (int i = 1; i <= n; i++)
if (fa[i] == i) sum++;
return sum <= m;
}
int main() {
int tt; cin >> tt;
while (tt--) {
cin >> m >> n;
for (int i = 1; i <= n; i++)
scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
d[i][j] = (double)sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
double l = 0.0, r = 14145.0, mid;
while (l + eps < r) {
mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
printf("%.2lf\n", r);
}
return 0;
}