-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10600.cpp
More file actions
89 lines (84 loc) · 1.97 KB
/
Copy path10600.cpp
File metadata and controls
89 lines (84 loc) · 1.97 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (UVa) 10600
#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;
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 i == p[i] ? i : (p[i] = find(p[i])); }
bool same(int i, int j) { return find(i) == find(j); }
void join(int i, int j) {
if (!same(i, j)) {
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];
}
}
}
};
int main() {
int ca;
GET(ca);
while (ca--) {
int n, m;
int a, b, c;
bool use;
GET(n);
GET(m);
vector<tuple<int, int, int, bool>> edges;
for (int i = 0; i < m; ++i) {
GET(a);
GET(b);
GET(c);
--a;
--b;
edges.push_back(make_tuple(c, a, b, true));
}
sort(edges.begin(), edges.end());
// initial MST
UnionFind uf(n);
vector<bool> mst(m, false);
int min_cost = 0;
for (int i = 0; i < m; ++i) {
tie(c, a, b, use) = edges[i];
if (!uf.same(a, b)) {
uf.join(a, b);
min_cost += c;
mst[i] = true;
}
}
// find second best MST
int min_cost2 = INT_MAX;
vector<int> skip;
for (int i = 0; i < m; ++i)
if (mst[i]) skip.push_back(i);
for (int s : skip) {
get<3>(edges[s]) = false;
UnionFind uf2(n);
int cost = 0;
for (int i = 0; i < m; ++i) {
tie(c, a, b, use) = edges[i];
if (use && !uf2.same(a, b)) {
uf2.join(a, b);
cost += c;
}
}
bool ok = true;
for (int i = 1; i < n; ++i) {
ok &= uf2.same(i, 0);
}
if (ok) min_cost2 = min(min_cost2, cost);
get<3>(edges[s]) = true;
}
printf("%d %d\n", min_cost, min_cost2);
}
return 0;
}