-
Notifications
You must be signed in to change notification settings - Fork 3
/
a.cpp
54 lines (46 loc) · 1.13 KB
/
a.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
#include <bits/stdc++.h>
using namespace std;
int N;
vector<pair<double,double>> T;
vector<int> uf;
int ufFind(int i) {
if (uf[i] == i || uf[i] < 0) return uf[i] = i;
return uf[i] = ufFind(uf[i]);
}
void ufUnion(int a, int b) {
uf[b] = a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int E, P;
cin >> N >> E >> P;
T.resize(N), uf.assign(N, -1);
for (auto& p : T) cin >> p.first >> p.second;
for (int i = 1; i < E; ++i) {
int a = ufFind(0), b = ufFind(i);
if (a != b) ufUnion(a, b);
}
while (P--) {
int a, b;
cin >> a >> b;
a = ufFind(a-1), b = ufFind(b-1);
if (a != b) ufUnion(a, b);
}
vector<tuple<double,int,int>> edges;
edges.reserve(N*N);
for (int i = 0; i < N; ++i) for (int j = i+1; j < N; ++j) {
double dx = T[i].first-T[j].first, dy = T[i].second-T[j].second;
edges.emplace_back(sqrt(dx*dx+dy*dy), i, j);
}
sort(edges.begin(), edges.end());
double res = 0.0;
for (auto& [d, i, j] : edges) {
int a = ufFind(i), b = ufFind(j);
if (a == b) continue;
ufUnion(a, b);
res += d;
}
cout << setprecision(12) << res << endl;
return 0;
}