-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11228.cpp
68 lines (62 loc) · 1.47 KB
/
P11228.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
long distSq(const Point &a, const Point &b) {
long dx = a.XX-b.XX;
long dy = a.YY-b.YY;
return dx*dx+dy*dy;
}
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;
}
typedef pair<long,PI> LPI;
int main() {
int N, R, parents[1000], ranks[1000];
Point positions[1000];
FORCAS {
cin >> N >> R;
R *= R;
vector<LPI> edges;
FORI(N) {
ranks[i] = 0;
parents[i] = i;
cin >> positions[i].XX >> positions[i].YY;
FORJ(i) {
edges.push_back(LPI(distSq(positions[i], positions[j]), PI(i, j)));
}
}
sort(edges.begin(), edges.end());
int states = N;
double roads = 0, rail = 0;
FORIT(vector<LPI>, edges) {
if(it->first > R) {
if(_union(it->second.P1, it->second.P2, parents, ranks)) {
rail += sqrt(it->first);
}
}
else {
if(_union(it->second.P1, it->second.P2, parents, ranks)) {
--states;
roads += sqrt(it->first);
}
}
}
printf("Case #%d: %d %.0lf %.0lf\n", cas+1, states, roads, rail);
}
return 0;
}