-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkruskal.cpp
52 lines (49 loc) · 1.03 KB
/
kruskal.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
// hoge
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
struct edge {
int a, b;
i64 w;
};
i64 kruskal(vector<edge>& edges, int n) {
vector<int> par(n);
for (int i = 0; i < n; i++) {
par[i] = i;
}
function<int(int)> find = [&](int x) {
if (x == par[x]) return x;
return par[x] = find(par[x]);
};
auto unite = [&](int x, int y) {
x = find(x);
y = find(y);
par[x] = y;
};
auto same = [&](int x, int y) {
return find(x) == find(y);
};
sort(edges.begin(), edges.end(), [](edge a, edge b) {
return a.w < b.w;
});
i64 ret = 0;
for (edge e: edges) {
if (!same(e.a, e.b)) {
unite(e.a, e.b);
ret += e.w;
}
}
return ret;
}
int main() {
int v, e;
cin >> v >> e;
vector<edge> edges;
for (int i = 0; i < e; i++) {
int a, b;
i64 w;
cin >> a >> b >> w;
edges.push_back({a, b, w});
}
cout << kruskal(edges, v) << endl;
}