-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10034.cpp
More file actions
116 lines (101 loc) · 2.76 KB
/
Copy path10034.cpp
File metadata and controls
116 lines (101 loc) · 2.76 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10034
// Title: Freckles
// Link:
// https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=975
// Idea: Boruvka's Algorithm for MST
// Difficulty: easy
// Tags: graph, mst
#include <bits/stdc++.h>
#define GET(x) scanf("%d", &x)
#define GED(x) scanf("%lf", &x)
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
using Edge = pair<pair<int, int>, double>;
struct UnionFind {
vector<int> p, rank;
UnionFind(int n) : p(n), rank(n, 0) { iota(p.begin(), p.end(), 0); }
int find(int i) { return p[i] == i ? i : (p[i] = find(p[i])); }
bool same(int i, int j) { return find(i) == find(j); }
bool join(int i, int j) {
if (same(i, j)) return false;
int x = find(i), y = find(j);
if (rank[x] > rank[y]) {
p[y] = x;
} else {
p[x] = y;
if (rank[x] == rank[y]) {
++rank[y];
}
}
return true;
}
};
double boruvka(int n, const vector<Edge>& edges) {
UnionFind uf(n);
double cost = 0.0;
int selected = 0;
for (int i = 1; selected < n - 1 && i < n; i <<= 1) {
// select edges to add
vector<Edge> closest(n, {{-1, -1}, -1.0});
for (const Edge& e : edges) {
int u, v;
pair<int, int> nodes;
double d;
tie(nodes, d) = e;
tie(u, v) = nodes;
int x = uf.find(u);
int y = uf.find(v);
if (x == y) continue;
// if we compare between nodes instead of parent clusters, we may end up
// adding multiple edges from one cluster to another (e.g. multiple nodes
// in one cluster could connect to the other cluster)
if (closest[x].second == -1.0 || d < closest[x].second) closest[x] = e;
if (closest[y].second == -1.0 || d < closest[y].second) closest[y] = e;
}
// add in edges
for (const Edge& e : closest) {
int u, v;
pair<int, int> nodes;
double d;
tie(nodes, d) = e;
tie(u, v) = nodes;
if (d == -1.0) continue;
if (uf.join(u, v)) {
++selected;
cost += d;
// printf("%d -%.2lf-> %d\n", u, d, v); // debugging
}
}
}
return cost;
}
int main() {
int ca;
GET(ca);
while (ca--) {
int n;
GET(n);
vector<pair<double, double>> pts;
for (int i = 0; i < n; ++i) {
double x, y;
GED(x);
GED(y);
pts.push_back({x, y});
}
vector<Edge> edges;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
double dx = pts[i].first - pts[j].first;
double dy = pts[i].second - pts[j].second;
double d = sqrt(dx * dx + dy * dy);
edges.push_back({{i, j}, d});
}
}
printf("%.2lf\n", boruvka(n, edges));
if (ca) printf("\n");
}
return 0;
}