summaryrefslogtreecommitdiff
path: root/graph/kruskal.cpp
blob: a9556321d7d5a8e270c8758043691e12a0570dec (plain)
1
2
3
4
5
6
7
8
9
// Union-Find Implementierung von oben. Laufzeit: O(n*log(n))
sort(edges.begin(), edges.end());
vector<ii> mst; int cost = 0;
for (auto &e : edges) {
  if (findSet(e.from) != findSet(e.to)) {
    unionSets(e.from, e.to);
    mst.push_back(ii(e.from, e.to));
    cost += e.cost;
}}