#pragma once #include <utility> #include <vector> #include "Mylib/DataStructure/UnionFind/unionfind.cpp" #include "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> auto boruvka(const graph<T> &g) { std::vector<edge<T>> ret; const int N = g.size(); unionfind uf(N); std::vector<std::vector<int>> c(N); for (int i = 0; i < N; ++i) c[i].push_back(i); while ((int) (ret.size()) < N - 1) { std::vector<edge<T>> temp; for (auto &a : c) { edge<T> m; bool ok = false; if (a.empty()) continue; for (auto i : a) { for (auto &e : g[i]) { if (uf.is_same(e.from, e.to)) continue; if (not std::exchange(ok, true) or e.cost < m.cost) { m = e; } } } temp.push_back(m); } for (auto &e : temp) { if (uf.is_same(e.from, e.to)) continue; const int i = uf.root_of(e.from); const int j = uf.root_of(e.to); const int k = uf.merge(i, j); if (c[i].size() < c[j].size()) std::swap(c[i], c[j]); c[i].insert(c[i].end(), c[j].begin(), c[j].end()); c[j].clear(); std::swap(c[k], c[i]); ret.push_back(e); } } return ret; } } // namespace haar_lib
#line 2 "Mylib/Graph/MinimumSpanningTree/boruvka.cpp" #include <utility> #include <vector> #line 2 "Mylib/DataStructure/UnionFind/unionfind.cpp" #include <algorithm> #include <numeric> #line 5 "Mylib/DataStructure/UnionFind/unionfind.cpp" namespace haar_lib { class unionfind { int n_, count_; mutable std::vector<int> parent_; std::vector<int> depth_, size_; public: unionfind() {} unionfind(int n) : n_(n), count_(n), parent_(n), depth_(n, 1), size_(n, 1) { std::iota(parent_.begin(), parent_.end(), 0); } int root_of(int i) const { if (parent_[i] == i) return i; else return parent_[i] = root_of(parent_[i]); } bool is_same(int i, int j) const { return root_of(i) == root_of(j); } int merge(int i, int j) { const int ri = root_of(i), rj = root_of(j); if (ri == rj) return ri; else { --count_; if (depth_[ri] < depth_[rj]) { parent_[ri] = rj; size_[rj] += size_[ri]; return rj; } else { parent_[rj] = ri; size_[ri] += size_[rj]; if (depth_[ri] == depth_[rj]) ++depth_[ri]; return ri; } } } int size_of(int i) const { return size_[root_of(i)]; } int count_groups() const { return count_; } auto get_groups() const { std::vector<std::vector<int>> ret(n_); for (int i = 0; i < n_; ++i) { ret[root_of(i)].push_back(i); } ret.erase( std::remove_if( ret.begin(), ret.end(), [](const auto &a) { return a.empty(); }), ret.end()); return ret; } }; } // namespace haar_lib #line 2 "Mylib/Graph/Template/graph.cpp" #include <iostream> #line 4 "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> struct edge { int from, to; T cost; int index = -1; edge() {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int index) : from(from), to(to), cost(cost), index(index) {} }; template <typename T> struct graph { using weight_type = T; using edge_type = edge<T>; std::vector<std::vector<edge<T>>> data; auto& operator[](size_t i) { return data[i]; } const auto& operator[](size_t i) const { return data[i]; } auto begin() const { return data.begin(); } auto end() const { return data.end(); } graph() {} graph(int N) : data(N) {} bool empty() const { return data.empty(); } int size() const { return data.size(); } void add_edge(int i, int j, T w, int index = -1) { data[i].emplace_back(i, j, w, index); } void add_undirected(int i, int j, T w, int index = -1) { add_edge(i, j, w, index); add_edge(j, i, w, index); } template <size_t I, bool DIRECTED = true, bool WEIGHTED = true> void read(int M) { for (int i = 0; i < M; ++i) { int u, v; std::cin >> u >> v; u -= I; v -= I; T w = 1; if (WEIGHTED) std::cin >> w; if (DIRECTED) add_edge(u, v, w, i); else add_undirected(u, v, w, i); } } }; template <typename T> using tree = graph<T>; } // namespace haar_lib #line 6 "Mylib/Graph/MinimumSpanningTree/boruvka.cpp" namespace haar_lib { template <typename T> auto boruvka(const graph<T> &g) { std::vector<edge<T>> ret; const int N = g.size(); unionfind uf(N); std::vector<std::vector<int>> c(N); for (int i = 0; i < N; ++i) c[i].push_back(i); while ((int) (ret.size()) < N - 1) { std::vector<edge<T>> temp; for (auto &a : c) { edge<T> m; bool ok = false; if (a.empty()) continue; for (auto i : a) { for (auto &e : g[i]) { if (uf.is_same(e.from, e.to)) continue; if (not std::exchange(ok, true) or e.cost < m.cost) { m = e; } } } temp.push_back(m); } for (auto &e : temp) { if (uf.is_same(e.from, e.to)) continue; const int i = uf.root_of(e.from); const int j = uf.root_of(e.to); const int k = uf.merge(i, j); if (c[i].size() < c[j].size()) std::swap(c[i], c[j]); c[i].insert(c[i].end(), c[j].begin(), c[j].end()); c[j].clear(); std::swap(c[k], c[i]); ret.push_back(e); } } return ret; } } // namespace haar_lib