競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
木に辺を追加し、どの 1 頂点を取り除いても残りが連結になるようにします。辺 (u, v) を追加する費用は両端の頂点重みの和です。
augment_subcubic_tree_biconnectivity(weights, edges) に非負の uint64_t の頂点重みと、0 始まりの無向辺を渡します。頂点数は 3 以上で、もとの木の次数はどの頂点も 3 以下です。
戻り値の edges が追加する辺、cost が最小費用です。最小費用が uint64_t の上限を超える場合も辺は得られ、cost は nullopt になります。
葉を巡回順に並べて半周先と結びます。葉が奇数なら、重みが小さく接続条件を満たす頂点を 1 回加えます。時間と領域はともに O(n) です。
augment_subcubic_tree_biconnectivity(weights, edges):
各頂点の次数が3以下の木に、追加後にどの1頂点を削除しても連結な辺集合を構成する。
n>=3、頂点番号は0始まり。辺(u,v)の加算費用weights[u]+weights[v]の総和を最小化。
O(n)時間・領域。頂点重みは非負のuint64_t。
各枝の葉は巡回順で連続し、半周先への辺で枝どうしをつなぐ。
奇数葉では追加頂点を根とし、反対側が単独葉の枝にならない枝の境界に根を挿す。
BiconnectivityAugmentation{edges,cost}: 追加する辺と最小費用。費用が表現範囲外ならcostはnullopt。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| E - Make Biconnected | subcubic_tree_biconnectivity.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/subcubic_tree_biconnectivity.md
//
// API一覧
// augment_subcubic_tree_biconnectivity(weights, edges):
// 各頂点の次数が3以下の木に、追加後にどの1頂点を削除しても連結な辺集合を構成する。
// n>=3、頂点番号は0始まり。辺(u,v)の加算費用weights[u]+weights[v]の総和を最小化。
// O(n)時間・領域。頂点重みは非負のuint64_t。
// 各枝の葉は巡回順で連続し、半周先への辺で枝どうしをつなぐ。
// 奇数葉では追加頂点を根とし、反対側が単独葉の枝にならない枝の境界に根を挿す。
// BiconnectivityAugmentation{edges,cost}: 追加する辺と最小費用。費用が表現範囲外ならcostはnullopt。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_SUBCUBIC_TREE_BICONNECTIVITY_DEFINED
#define SIRKIT_SUBCUBIC_TREE_BICONNECTIVITY_DEFINED
struct BiconnectivityAugmentation
{
std::vector<std::pair<int, int>> edges;
std::optional<std::uint64_t> cost;
};
namespace detail
{
inline void check_subcubic_tree_size(std::size_t vertices, std::size_t edges)
{
if (vertices < 3 || vertices > static_cast<std::size_t>(std::numeric_limits<int>::max()) ||
edges != vertices - 1)
{
throw std::length_error("augment_subcubic_tree_biconnectivity: invalid tree size");
}
}
} // namespace detail
[[nodiscard]] inline BiconnectivityAugmentation augment_subcubic_tree_biconnectivity(
std::span<const std::uint64_t> weights, std::span<const std::pair<int, int>> edges)
{
detail::check_subcubic_tree_size(weights.size(), edges.size());
const int n = static_cast<int>(weights.size());
std::vector<std::array<int, 3>> neighbors(n);
std::vector<int> degree(n);
auto add = [&](int u, int v)
{
#ifndef SKIP_RUNTIME_CHECKS
if (u < 0 || u >= n || v < 0 || v >= n)
{
throw std::out_of_range("augment_subcubic_tree_biconnectivity: invalid vertex");
}
#endif
if (degree[u] == 3)
{
throw std::invalid_argument("augment_subcubic_tree_biconnectivity: degree above three");
}
neighbors[u][degree[u]++] = v;
};
for (const auto &[u, v] : edges)
{
add(u, v);
add(v, u);
}
int leaf_count = 0;
for (const int d : degree)
{
leaf_count += d == 1;
}
int extra = -1;
if (leaf_count % 2 != 0)
{
for (int v = 0; v < n; ++v)
{
// 三叉の木は中心を失うと、中心に接続した辺を使わず3枝をつなぐ必要がある。
if (leaf_count == 3 && degree[v] == 3)
{
continue;
}
if (extra == -1 || weights[v] < weights[extra])
{
extra = v;
}
}
}
const int root = extra == -1 ? 0 : extra;
std::vector<int> order;
order.reserve(static_cast<std::size_t>(leaf_count) + 1);
if (degree[root] == 1)
{
order.push_back(root);
}
std::array<std::size_t, 4> begin{};
std::vector<std::pair<int, int>> pending;
#ifndef SKIP_RUNTIME_CHECKS
std::vector<bool> seen(n);
seen[root] = true;
int visited = 1;
#endif
for (int branch = 0; branch < degree[root]; ++branch)
{
begin[branch] = order.size();
pending.emplace_back(neighbors[root][branch], root);
while (!pending.empty())
{
const auto [v, previous] = pending.back();
pending.pop_back();
#ifndef SKIP_RUNTIME_CHECKS
if (seen[v])
{
throw std::invalid_argument("augment_subcubic_tree_biconnectivity: cycle");
}
seen[v] = true;
++visited;
#endif
if (degree[v] == 1)
{
order.push_back(v);
}
for (int i = 0; i < degree[v]; ++i)
{
const int u = neighbors[v][i];
if (u != previous)
{
pending.emplace_back(u, v);
}
}
}
}
begin[degree[root]] = order.size();
#ifndef SKIP_RUNTIME_CHECKS
if (visited != n)
{
throw std::invalid_argument("augment_subcubic_tree_biconnectivity: disconnected");
}
#endif
if (extra != -1)
{
std::size_t insertion = 0;
if (degree[root] != 1)
{
for (int corner = 0; corner < degree[root]; ++corner)
{
const auto at = begin[corner];
const auto opposite = (at + order.size() / 2) % order.size();
bool singleton = false;
for (int branch = 0; branch < degree[root]; ++branch)
{
if (begin[branch] == opposite && begin[branch + 1] == opposite + 1)
{
singleton = true;
break;
}
}
if (!singleton)
{
insertion = at;
}
}
}
order.insert(order.begin() + static_cast<std::ptrdiff_t>(insertion), extra);
}
BiconnectivityAugmentation result{{}, 0};
const auto half = order.size() / 2;
result.edges.reserve(half);
for (std::size_t i = 0; i < half; ++i)
{
result.edges.emplace_back(order[i], order[i + half]);
}
for (const int v : order)
{
if (!result.cost)
{
break;
}
const auto weight = weights[v];
if (weight > std::numeric_limits<std::uint64_t>::max() - *result.cost)
{
result.cost.reset();
}
else
{
*result.cost += weight;
}
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1