-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.cpp
More file actions
44 lines (40 loc) · 803 Bytes
/
Kruskal.cpp
File metadata and controls
44 lines (40 loc) · 803 Bytes
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
#include <bits/stdc++.h>
using namespace std;
namespace Kruskal {
struct UF {
vector<int> par;
UF(int n) {
par.resize(n);
iota(par.begin(), par.end(), 0);
}
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
par[u] = v;
}
};
struct Edge {
int u, v;
long long w;
Edge() {}
Edge(int u, int v, long long w) : u(u), v(v), w(w) {}
bool operator<(Edge const& e) const { return w < e.w; }
};
vector<Edge> kruskal(int n, vector<Edge>& edg) {
sort(edg.begin(), edg.end());
UF uf(n + 1);
vector<Edge> ans;
for (Edge const& e : edg) {
if (uf.find(e.u) != uf.find(e.v)) {
uf.merge(e.u, e.v);
ans.push_back(e);
}
}
return ans;
}
}
int main() {
using namespace Kruskal;
}