summaryrefslogtreecommitdiff
path: root/content/graph/kruskal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'content/graph/kruskal.cpp')
-rw-r--r--content/graph/kruskal.cpp20
1 files changed, 11 insertions, 9 deletions
diff --git a/content/graph/kruskal.cpp b/content/graph/kruskal.cpp
index 987d30b..98a2682 100644
--- a/content/graph/kruskal.cpp
+++ b/content/graph/kruskal.cpp
@@ -1,9 +1,11 @@
-sort(all(edges));
-vector<Edge> mst;
-ll cost = 0;
-for (Edge& e : edges) {
- if (findSet(e.from) != findSet(e.to)) {
- unionSets(e.from, e.to);
- mst.push_back(e);
- cost += e.cost;
-}}
+ll kruskal(int n, vector<Edge> edges, vector<Edge> &mst) {
+ ranges::sort(edges, less{});
+ ll cost = 0;
+ UnionFind uf(n); // union find @\sourceref{datastructures/unionFind.cpp}@
+ for (Edge &e: edges) {
+ if (uf.link(e.from, e.to)) {
+ mst.push_back(e);
+ cost += e.cost;
+ }}
+ return cost;
+}