-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackerrank_kruskal_special_subtree.cc
92 lines (75 loc) · 2.02 KB
/
hackerrank_kruskal_special_subtree.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
UnionFind(int n) {
parent_.assign(n, 0);
rank_.assign(n, 0);
for (int i = 0; i < n; i++) {
parent_[i] = i;
}
}
int findSet(int i) {
if (parent_[i] == i) {
return i;
}
return parent_[i] = findSet(parent_[i]);
}
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int pi = findSet(i);
int pj = findSet(j);
if (rank_[pi] > rank_[pj]) {
parent_[pj] = pi;
} else {
parent_[pi] = pj;
if (rank_[pi] == rank_[pj]) {
rank_[pj]++;
}
}
}
}
private:
vector<int> parent_;
vector<int> rank_;
};
int main() {
ios_base::sync_with_stdio(false);
int V, E, u, v, w;
vector<pair<int, pair<int, int>>> edges;
cin >> V >> E;
for (int i = 0; i < E; i++) {
cin >> u >> v >> w;
u--, v--;
edges.push_back({w, {u, v}});
}
// nice lambda sorting
sort(edges.begin(), edges.end(), [](const pair<int, pair<int, int>> &edge1,
const pair<int, pair<int, int>> &edge2) {
return (edge1.first < edge2.first)
? true
: ((edge1.first == edge2.first)
? ((edge1.first + edge1.second.first +
edge1.second.second) <
(edge2.first + edge2.second.first +
edge2.second.second))
: false);
});
UnionFind uf(V);
int num_edges = 0;
long long total_mst_weight = 0;
for (int i = 0; i < E; i++) {
pair<int, pair<int, int>> crt_edge = edges[i];
if (!uf.isSameSet(crt_edge.second.first, crt_edge.second.second)) {
total_mst_weight += crt_edge.first;
num_edges += 1;
uf.unionSet(crt_edge.second.first, crt_edge.second.second);
}
// MST is complete, no need to check other edges
if (num_edges == V - 1) {
break;
}
}
cout << total_mst_weight << "\n";
}