-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1922.cpp
67 lines (59 loc) · 1.2 KB
/
1922.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
typedef struct edge{
int weight, u, v;
bool operator<(struct edge &rhs) {
return weight < rhs.weight;
}
} edge;
std::vector<int> parent;
std::vector<int> rank;
int find(int x) {
if (parent[x] == x) return x;
parent[x] = find(parent[x]);
return parent[x];
}
void merge(int x, int y) {
int root_x = find(x);
int root_y = find(y);
if (root_x != root_y) {
if (rank[root_x] == rank[root_y]) {
parent[root_y] = root_x;
rank[root_x]++;
}
else if (rank[root_x] < rank[root_y]) {
parent[root_y] = root_x;
}
else {
parent[root_x] = root_y;
}
}
}
int main(void) {
int n, m;
std::vector<edge> edges;
std::cin >> n >> m;
while (m--) {
int u, v, w;
std::cin >> u >> v >> w;
edges.push_back({ w, u-1, v-1 });
}
std::sort(edges.begin(), edges.end());
parent.resize(n);
rank.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 1;
}
int cost_of_MST = 0;
for (auto edge : edges) {
if (find(edge.u) != find(edge.v)) {
cost_of_MST += edge.weight;
merge(edge.u, edge.v);
}
}
std::cout << cost_of_MST;
return 0;
}