競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
全頂点を二つの空でない集合に分け、両方の集合をまたぐ辺の重みの和を最小にします。
const std::vector<sirkit::GlobalMinCutEdge> edges{{0, 1, 7}, {1, 2, 2}};
const auto cut = sirkit::global_min_cut(3, edges); // 重み2、片側は頂点2
global_min_cutstd::optional<GlobalMinCutResult> global_min_cut(int n,
std::span<const GlobalMinCutEdge> edges);
GlobalMinCutEdge{u, v, weight} で無向辺を渡します。頂点番号は [0,n) です。重みは std::uint64_t で、省略すると1です。平行辺は重みを足し、自己辺は無視します。自己辺を除いた全辺の重みの合計は std::uint64_t に収めます。
戻り値の weight は最小の重み、side は片側の頂点一覧です。頂点が2個未満なら std::nullopt を返します。$n$ 頂点 $m$ 辺で $O(n^3+m)$ 時間・$O(n^2)$ 領域です。
GlobalMinCutEdge {u,v,weight=1}: 無向辺と非負のuint64_t重み。
GlobalMinCutResult {weight,side}: 最小カットの重みと片側の頂点一覧(順不同)。
global_min_cut(n,edges): 全頂点を二つの空でない集合に分けるときの最小カット。n<2ではnullopt。
平行辺は加算し、自己辺は無視する。自己辺以外の全重みの合計はuint64_tに収める。
Stoer–Wagner法。n頂点m辺でO(n³+m)時間・O(n²)領域。入力は変更しない。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| 大域最小カットの入出力テスト | global_min_cut_operations.cpp | ローカル補足 | — |
| 大域最小カットの独立参照テスト | global_min_cut_reference.cpp | ローカル補足 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: Stoer & Wagner, A Simple Min-Cut Algorithm, https://doi.org/10.1145/263867.263872
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/global_min_cut.md
//
// API一覧
// GlobalMinCutEdge {u,v,weight=1}: 無向辺と非負のuint64_t重み。
// GlobalMinCutResult {weight,side}: 最小カットの重みと片側の頂点一覧(順不同)。
// global_min_cut(n,edges): 全頂点を二つの空でない集合に分けるときの最小カット。n<2ではnullopt。
// 平行辺は加算し、自己辺は無視する。自己辺以外の全重みの合計はuint64_tに収める。
// Stoer–Wagner法。n頂点m辺でO(n³+m)時間・O(n²)領域。入力は変更しない。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <numeric>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_GLOBAL_MIN_CUT_DEFINED
#define SIRKIT_GLOBAL_MIN_CUT_DEFINED
struct GlobalMinCutEdge
{
int u = 0;
int v = 0;
std::uint64_t weight = 1;
};
struct GlobalMinCutResult
{
std::uint64_t weight = 0;
std::vector<int> side;
};
namespace detail
{
inline std::size_t GlobalCutMatrixSize(std::size_t n, std::size_t max_cells)
{
if (n != 0 && n > max_cells / n)
{
throw std::length_error("global_min_cut: graph exceeds matrix capacity");
}
return n * n;
}
}
inline std::optional<GlobalMinCutResult> global_min_cut(int n, std::span<const GlobalMinCutEdge> edges)
{
#ifndef SKIP_RUNTIME_CHECKS
if (n < 0)
{
throw std::invalid_argument("global_min_cut: negative vertex count");
}
#endif
std::vector<std::uint64_t> matrix;
matrix.resize(detail::GlobalCutMatrixSize(n, matrix.max_size()));
const auto row = [&](int vertex)
{
return matrix.data() + static_cast<std::size_t>(vertex) * n;
};
std::uint64_t total = 0;
for (const auto &[u, v, weight] : edges)
{
#ifndef SKIP_RUNTIME_CHECKS
if (u < 0 || u >= n || v < 0 || v >= n)
{
throw std::out_of_range("global_min_cut: invalid endpoint");
}
#endif
if (u == v)
{
continue;
}
if (weight > std::numeric_limits<std::uint64_t>::max() - total)
{
throw std::overflow_error("global_min_cut: total edge weight does not fit");
}
total += weight;
row(u)[v] += weight;
row(v)[u] += weight;
}
if (n < 2)
{
return std::nullopt;
}
std::vector<int> active(n), tail(n), next(n, -1);
std::iota(active.begin(), active.end(), 0);
std::iota(tail.begin(), tail.end(), 0);
std::vector<std::uint64_t> adjacent_weight(n);
std::optional<GlobalMinCutResult> best;
while (active.size() >= 2)
{
std::fill(adjacent_weight.begin(), adjacent_weight.end(), std::uint64_t{0});
// すでに選んだ頂点への重み和が最大の頂点を選ぶ。
for (std::size_t i = 0; i + 1 < active.size(); ++i)
{
auto max_index = i;
for (auto j = i + 1; j < active.size(); ++j)
{
if (adjacent_weight[active[j]] > adjacent_weight[active[max_index]])
{
max_index = j;
}
}
std::swap(active[i], active[max_index]);
const auto *from = row(active[i]);
for (auto j = i + 1; j < active.size(); ++j)
{
const auto to = active[j];
adjacent_weight[to] += from[to];
}
}
const auto last = active.back();
active.pop_back();
const auto previous = active.back();
const auto cut = adjacent_weight[last];
if (!best || cut < best->weight)
{
best.emplace();
best->weight = cut;
for (int v = last; v >= 0; v = next[v])
{
best->side.push_back(v);
}
if (cut == 0)
{
return best;
}
}
// 最後の2頂点を縮約し、元の頂点を連結リストで追跡する。
const auto *removed = row(last);
auto *merged = row(previous);
for (std::size_t i = 0; i + 1 < active.size(); ++i)
{
const auto v = active[i];
merged[v] += removed[v];
row(v)[previous] = merged[v];
}
next[tail[previous]] = last;
tail[previous] = tail[last];
}
return best;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1