-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsu-kruskal.cpp
More file actions
60 lines (54 loc) · 1.34 KB
/
dsu-kruskal.cpp
File metadata and controls
60 lines (54 loc) · 1.34 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
struct UnionFind{
vector<int> sz, parent;
int forests;
UnionFind(int n){
sz = vector<int> (n);
parent = vector<int> (n);
forests = n;
for(int i = 0; i < n; i++){
sz[i] = 1, parent[i] = i;
}
}
int find_set(int x){
if(x == parent[x]) return x;
return parent[x] = find_set(parent[x]);
}
void link(int x, int y){
if(sz[x] > sz[y]) swap(x, y);
parent[x] = y;
sz[y] += sz[x];
}
bool union_sets(int x, int y){
x = find_set(x);
y = find_set(y);
if(x != y){
link(x, y);
forests--;
}
return x != y;
}
};
struct edge{
int from, to, w;
edge(int from, int to, int w):from(from), to(to), w(w){}
bool operator < (const edge & e) const {
return w > e.w;
}
};
ll kruskal(vector<edge> edgeList, int n){
UnionFind uf(n);
vector<edge> edges;
ll mstCost = 0;
priority_queue<edge> pq;
for(int i = 0; i < edgeList.size(); i++)
pq.push(edgeList[i]);
while(!pq.empty()){
edge e = pq.top();
pq.pop();
if(uf.union_sets(e.from, e.to)){
mstCost += e.w;
edges.push_back(e);
}
}
return mstCost;
}