From 88d04413ebaab961f849ac6ef3d6ff2179253d41 Mon Sep 17 00:00:00 2001 From: Gloria Mundi Date: Sat, 7 Jun 2025 21:20:34 +0200 Subject: make union find a struct, remove kruskal --- content/graph/kruskal.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'content/graph/kruskal.cpp') diff --git a/content/graph/kruskal.cpp b/content/graph/kruskal.cpp index d42800d..98a2682 100644 --- a/content/graph/kruskal.cpp +++ b/content/graph/kruskal.cpp @@ -1,9 +1,11 @@ -ranges::sort(edges, less{}); -vector 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 edges, vector &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; +} -- cgit v1.2.3