cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: Kruskal 法
(include/emthrm/graph/kruskal.hpp)

最小全域木 (minimum spanning tree)

時間計算量

$O(\lvert E \rvert \log{\lvert V \rvert})$

仕様

Prim 法

名前 戻り値 要件
template <typename CostType>
CostType prim(const std::vector<std::vector<Edge<CostType>>>& graph, const int root = 0);
根を $\mathrm{root}$ とするグラフ $\mathrm{graph}$ の最小全域木のコスト $\mathrm{graph}$ は双方向の辺の情報をもつ。

Kruskal 法

名前 戻り値 備考
template <typename CostType>
CostType kruskal(const std::vector<std::vector<Edge<CostType>>>& graph);
グラフ $\mathrm{graph}$ の最小全域木のコスト $\mathrm{graph}$ には単方向の辺の情報のみでよい。

参考文献

Prim 法

Kruskal 法

TODO

Submissons

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_KRUSKAL_HPP_
#define EMTHRM_GRAPH_KRUSKAL_HPP_

#include <algorithm>
#include <ranges>
#include <vector>

#include "emthrm/data_structure/union-find/union-find.hpp"
#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType>
CostType kruskal(const std::vector<std::vector<Edge<CostType>>>& graph) {
  const int n = graph.size();
  const auto jv = graph | std::views::join;
  std::vector<Edge<CostType>> edges(jv.begin(), jv.end());
  std::ranges::sort(edges);
  CostType res = 0;
  UnionFind uf(n);
  for (const Edge<CostType>& e : edges) {
    if (uf.unite(e.src, e.dst)) res += e.cost;
  }
  return res;
}

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_KRUSKAL_HPP_
#line 1 "include/emthrm/graph/kruskal.hpp"



#include <algorithm>
#include <ranges>
#include <vector>

#line 1 "include/emthrm/data_structure/union-find/union-find.hpp"



#include <utility>
#line 6 "include/emthrm/data_structure/union-find/union-find.hpp"

namespace emthrm {

struct UnionFind {
  explicit UnionFind(const int n) : data(n, -1) {}

  int root(const int ver) {
    return data[ver] < 0 ? ver : data[ver] = root(data[ver]);
  }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool is_same(const int u, const int v) { return root(u) == root(v); }

  int size(const int ver) { return -data[root(ver)]; }

 private:
  std::vector<int> data;
};

}  // namespace emthrm


#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 10 "include/emthrm/graph/kruskal.hpp"

namespace emthrm {

template <typename CostType>
CostType kruskal(const std::vector<std::vector<Edge<CostType>>>& graph) {
  const int n = graph.size();
  const auto jv = graph | std::views::join;
  std::vector<Edge<CostType>> edges(jv.begin(), jv.end());
  std::ranges::sort(edges);
  CostType res = 0;
  UnionFind uf(n);
  for (const Edge<CostType>& e : edges) {
    if (uf.unite(e.src, e.dst)) res += e.cost;
  }
  return res;
}

}  // namespace emthrm
Back to top page