-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path10147.cpp
92 lines (75 loc) · 1.8 KB
/
10147.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
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <tuple>
#include <cmath>
#include <cstdio>
using namespace std;
vector<int> parent, sz;
int get_parent(int a) {
if (parent[a] == a) {
return a;
}
return parent[a] = get_parent(parent[a]);
}
void join(int a, int b) {
a = get_parent(a);
b = get_parent(b);
if (a != b) {
if (sz[a] < sz[b]) {
swap(a, b);
}
parent[b] = a;
sz[a] += sz[b];
}
}
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
int x, y;
cin >> n;
vector<pair<int,int>> towns(n);
parent.resize(n);
sz.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
sz[i] = 1;
cin >> towns[i].first >> towns[i].second;
}
vector<tuple<double,int,int>> edges;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dist = hypot(towns[i].first - towns[j].first, towns[i].second - towns[j].second);
edges.push_back({dist, i, j});
}
}
sort(edges.begin(), edges.end());
int m;
cin >> m;
while (m--) {
cin >> x >> y;
--x, --y;
join(x, y);
}
bool constructed = false;
for (auto [c, a, b]: edges) {
int pa = get_parent(a);
int pb = get_parent(b);
if (pa != pb) {
cout << a + 1 << " " << b + 1 << endl;
join(pa, pb);
constructed = true;
}
}
if (!constructed) {
cout << "No new highways need" << endl;
}
if (t) cout << endl;
}
return 0;
}