-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathminspantree.cc
69 lines (64 loc) · 1.23 KB
/
minspantree.cc
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
// https://open.kattis.com/problems/minspantree
#include <bits/stdc++.h>
using namespace std;
using ii = tuple<int, int>;
using iii = tuple<int, int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using viii = vector<iii>;
vi p, s;
int find(int a) {
if (a == p[a])
return a;
return p[a] = find(p[a]);
}
bool unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return 0;
if (s[a] < s[b])
swap(a, b);
p[b] = a;
s[a] += s[b];
return 1;
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
while (1) {
int n, m, u, v, w, W = 0;
cin >> n >> m;
if (!n)
break;
p = vi(n);
for (int i = 0; i < n; i++)
p[i] = i;
s = vi(n, 1);
viii e(m);
for (int i = 0; i < m; i++) {
cin >> u >> v >> w;
if (u > v)
swap(u, v);
e[i] = {w, u, v};
}
sort(e.begin(), e.end());
vii r;
for (iii x : e) {
tie(w, u, v) = x;
if (unite(u, v)) {
r.push_back({u, v});
W += w;
}
}
if (r.size() != n - 1) {
cout << "Impossible\n";
continue;
}
sort(r.begin(), r.end());
cout << W << '\n';
for (int i = 0; i < n - 1; i++) {
tie(u, v) = r[i];
cout << u << ' ' << v << '\n';
}
}
}