cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: グラフ/Kruskal 法
(test/graph/kruskal.test.cpp)

Depends on

Code

/*
 * @title グラフ/Kruskal 法
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A
 */

#include <iostream>
#include <vector>

#include "emthrm/graph/edge.hpp"
#include "emthrm/graph/kruskal.hpp"

int main() {
  int v, e;
  std::cin >> v >> e;
  std::vector<std::vector<emthrm::Edge<long long>>> edge(v);
  for (int i = 0; i < e; ++i) {
    int s, t, w;
    std::cin >> s >> t >> w;
    edge[s].emplace_back(s, t, w);
    edge[t].emplace_back(t, s, w);
  }
  std::cout << emthrm::kruskal(edge) << '\n';
  return 0;
}
#line 1 "test/graph/kruskal.test.cpp"
/*
 * @title グラフ/Kruskal 法
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A
 */

#include <iostream>
#include <vector>

#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 1 "include/emthrm/graph/kruskal.hpp"



#include <algorithm>
#include <ranges>
#line 7 "include/emthrm/graph/kruskal.hpp"

#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


#line 12 "test/graph/kruskal.test.cpp"

int main() {
  int v, e;
  std::cin >> v >> e;
  std::vector<std::vector<emthrm::Edge<long long>>> edge(v);
  for (int i = 0; i < e; ++i) {
    int s, t, w;
    std::cin >> s >> t >> w;
    edge[s].emplace_back(s, t, w);
    edge[t].emplace_back(t, s, w);
  }
  std::cout << emthrm::kruskal(edge) << '\n';
  return 0;
}
Back to top page