This documentation is automatically generated by online-judge-tools/verification-helper
#include "graph/kruskal.hpp"Graph<T> g;
auto [cost, edges] = kruskal<T>(g);
#pragma once
#include "unionfind/unionfind.hpp"
#include "graph/graph_template.hpp"
#include <algorithm>
template <class T> std::pair<T, std::vector<Edge<T>>> kruskal(Graph<T>& g) {
auto es = g.edges;
std::sort(es.begin(), es.end());
const int n = (int)(g.size());
Unionfind uf(n);
T ret = 0;
std::vector<Edge<T>> es_ret;
es_ret.reserve(n - 1);
for (auto&& e : es) {
if (!uf.same(e.from, e.to)) {
ret += e.cost;
uf.merge(e.from, e.to);
es_ret.push_back(e);
}
}
return {ret, es_ret};
}#line 2 "graph/kruskal.hpp"
#line 2 "unionfind/unionfind.hpp"
#include <algorithm>
#include <cassert>
#include <vector>
// Unionfind
struct Unionfind {
int n;
std::vector<int> parent;
Unionfind() = default;
explicit Unionfind(int n) : n(n), parent(n, -1) {}
int leader(int x) {
assert(0 <= x and x < n);
if (parent[x] < 0) {
return x;
} else {
return parent[x] = leader(parent[x]);
}
}
int merge(int x, int y) {
assert(0 <= x and x < n);
assert(0 <= y and y < n);
x = leader(x);
y = leader(y);
if (x == y) {
return x;
}
if (-parent[x] < -parent[y]) {
std::swap(x, y);
}
parent[x] += parent[y];
parent[y] = x;
return x;
}
bool same(int x, int y) {
assert(0 <= x and x < n);
assert(0 <= y and y < n);
return leader(x) == leader(y);
}
int size(int x) {
assert(0 <= x and x < n);
return -parent[leader(x)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(n), group_size(n);
for (int i = 0; i < n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(n);
for (int i = 0; i < n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(std::remove_if(
result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}
};
#line 2 "graph/graph_template.hpp"
#line 5 "graph/graph_template.hpp"
template <class T> struct Edge {
int from, to;
T cost;
int id;
Edge() = default;
Edge(const int from, const int to, const T cost = T(1), const int id = -1) : from(from), to(to), cost(cost), id(id) {}
friend bool operator<(const Edge<T>& a, const Edge<T>& b) { return a.cost < b.cost; }
friend std::ostream& operator<<(std::ostream& os, const Edge<T>& e) {
// output format: {id: cost(from, to) = cost}
return os << "{" << e.id << ": cost(" << e.from << ", " << e.to << ") = " << e.cost << "}";
}
};
template <class T> using Edges = std::vector<Edge<T>>;
template <class T> struct Graph {
struct EdgeIterators {
public:
using Iterator = typename std::vector<Edge<T>>::iterator;
EdgeIterators() = default;
EdgeIterators(const Iterator& begit, const Iterator& endit) : begit(begit), endit(endit) {}
Iterator begin() const { return begit; }
Iterator end() const { return endit; }
size_t size() const { return std::distance(begit, endit); }
Edge<T>& operator[](int i) const { return begit[i]; }
private:
Iterator begit, endit;
};
int n, m;
bool is_build, is_directed;
std::vector<Edge<T>> edges;
// CSR (Compressed Row Storage) 形式用
std::vector<int> start;
std::vector<Edge<T>> csr_edges;
Graph() = default;
Graph(const int n, const bool directed = false) : n(n), m(0), is_build(false), is_directed(directed), start(n + 1, 0) {}
// 辺を追加し, その辺が何番目に追加されたかを返す
int add_edge(const int from, const int to, const T cost = T(1), int id = -1) {
assert(!is_build);
assert(0 <= from and from < n);
assert(0 <= to and to < n);
if (id == -1) id = m;
edges.emplace_back(from, to, cost, id);
return m++;
}
// CSR 形式でグラフを構築
void build() {
assert(!is_build);
for (auto&& e : edges) {
start[e.from + 1]++;
if (!is_directed) start[e.to + 1]++;
}
for (int v = 0; v < n; v++) start[v + 1] += start[v];
auto counter = start;
csr_edges.resize(start.back() + 1);
for (auto&& e : edges) {
csr_edges[counter[e.from]++] = e;
if (!is_directed) csr_edges[counter[e.to]++] = Edge(e.to, e.from, e.cost, e.id);
}
is_build = true;
}
EdgeIterators operator[](int i) {
if (!is_build) build();
return EdgeIterators(csr_edges.begin() + start[i], csr_edges.begin() + start[i + 1]);
}
size_t size() const { return (size_t)(n); }
friend std::ostream& operator<<(std::ostream& os, Graph<T>& g) {
os << "[";
for (int i = 0; i < (int)(g.size()); i++) {
os << "[";
for (int j = 0; j < (int)(g[i].size()); j++) {
os << g[i][j];
if (j + 1 != (int)(g[i].size())) os << ", ";
}
os << "]";
if (i + 1 != (int)(g.size())) os << ", ";
}
return os << "]";
}
};
#line 5 "graph/kruskal.hpp"
#line 7 "graph/kruskal.hpp"
template <class T> std::pair<T, std::vector<Edge<T>>> kruskal(Graph<T>& g) {
auto es = g.edges;
std::sort(es.begin(), es.end());
const int n = (int)(g.size());
Unionfind uf(n);
T ret = 0;
std::vector<Edge<T>> es_ret;
es_ret.reserve(n - 1);
for (auto&& e : es) {
if (!uf.same(e.from, e.to)) {
ret += e.cost;
uf.merge(e.from, e.to);
es_ret.push_back(e);
}
}
return {ret, es_ret};
}