-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10369.cpp
79 lines (71 loc) · 1.79 KB
/
P10369.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<int,int> PP;
typedef pair<long,PP> DPP;
#define X first
#define Y second
long distSq(const PP &a, const PP &b) {
return (a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y);
}
int find(int x, int *parents) {
if(parents[x] != x)
parents[x] = find(parents[x], parents);
return parents[x];
}
bool _union(int x, int y, int *parents, int *ranks) {
int xRoot = find(x, parents);
int yRoot = find(y, parents);
if(xRoot == yRoot)
return false;
// x and y are not already in same set. Merge them.
if(ranks[xRoot] < ranks[yRoot])
parents[xRoot] = yRoot;
else if(ranks[xRoot] > ranks[yRoot])
parents[yRoot] = xRoot;
else {
parents[yRoot] = xRoot;
++ranks[xRoot];
}
return true;
}
int main() {
int N, S, P, parents[500], ranks[500];
PP positions[500];
DPP *dists = new DPP[500*500];
cin >> N;
for(int cas = 0; cas < N; ++cas) {
// Read input:
cin >> S >> P;
for(int i = 0; i < P; ++i) {
cin >> positions[i].X >> positions[i].Y;
}
// Compute dists:
int distsI = 0;
for(int i = 0; i < P; ++i)
for(int j = i+1; j < P; ++j)
dists[distsI++] = DPP(distSq(positions[i], positions[j]), PP(i,j));
sort(dists, dists+distsI);
// Prepare Union-find structure (parents and ranks):
for(int i = 0; i < P; ++i) {
ranks[i] = 0;
parents[i] = i;
}
int trees = P;
long DD = 0;
distsI = 0;
//cerr << "Starting tree merging. trees=P=" << P << endl;
while(trees > S && trees > 1) {
int x = dists[distsI].second.X;
int y = dists[distsI].second.Y;
DD = dists[distsI].first;
++distsI;
if(_union(x, y, parents, ranks))
--trees;
}
printf("%.2f\n", sqrt(DD));
}
return 0;
}