#pragma once #include <algorithm> #include <vector> #include "Mylib/DataStructure/UnionFind/unionfind.cpp" #include "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> std::vector<edge<T>> kruskal(const graph<T> &graph) { const int n = graph.size(); std::vector<edge<T>> edges; for (auto &v : graph) { for (auto &e : v) { edges.push_back(e); } } std::sort( edges.begin(), edges.end(), [](const auto &a, const auto &b) { return a.cost < b.cost; }); unionfind uf(n); std::vector<edge<T>> ret; for (auto &e : edges) { if (not uf.is_same(e.from, e.to)) { uf.merge(e.from, e.to); ret.push_back(e); } } return ret; } } // namespace haar_lib
#line 2 "Mylib/Graph/MinimumSpanningTree/kruskal.cpp" #include <algorithm> #include <vector> #line 3 "Mylib/DataStructure/UnionFind/unionfind.cpp" #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/kruskal.cpp" namespace haar_lib { template <typename T> std::vector<edge<T>> kruskal(const graph<T> &graph) { const int n = graph.size(); std::vector<edge<T>> edges; for (auto &v : graph) { for (auto &e : v) { edges.push_back(e); } } std::sort( edges.begin(), edges.end(), [](const auto &a, const auto &b) { return a.cost < b.cost; }); unionfind uf(n); std::vector<edge<T>> ret; for (auto &e : edges) { if (not uf.is_same(e.from, e.to)) { uf.merge(e.from, e.to); ret.push_back(e); } } return ret; } } // namespace haar_lib