競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
森の各頂点をちょうど一度ずつ使い、頂点数が同じパスに分けます。分解できれば、各パスを端からたどる順に頂点列で返します。
partition_forest_into_equal_paths(n, edges, path_size) に頂点数、0 始まりの無向辺、パス一つに含める頂点数を渡します。パスの頂点数は 1 以上です。森の全頂点を分解できなければ nullopt が返ります。
const std::vector<std::pair<int, int>> edges{{0, 1}, {1, 2}, {2, 3}};
auto paths = sirkit::partition_forest_into_equal_paths(4, edges, 2);
assert(paths && paths->size() == 2);
頂点数を n、辺数を m とすると、時間・領域はともに O(n + m) です。各成分の葉から順に、未完成のパスを親へ引き継ぐか、そこで完成させるかを決めます。
partition_forest_into_equal_paths(n,edges,path_size): 森の頂点を同じ頂点数のパスに分ける。 頂点は0始まり。edges は無向辺 (u,v) の列。path_size は1以上で、各パスに含める頂点数。 分解できれば各パスの頂点列を返す。できなければ nullopt。時間・領域 O(n+m)。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Path Decomposition of a Tree | equal_tree_path_partition.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/equal_tree_path_partition.md
//
// API一覧
// partition_forest_into_equal_paths(n,edges,path_size): 森の頂点を同じ頂点数のパスに分ける。
// 頂点は0始まり。edges は無向辺 (u,v) の列。path_size は1以上で、各パスに含める頂点数。
// 分解できれば各パスの頂点列を返す。できなければ nullopt。時間・領域 O(n+m)。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_EQUAL_TREE_PATH_PARTITION_DEFINED
#define SIRKIT_EQUAL_TREE_PATH_PARTITION_DEFINED
inline std::optional<std::vector<std::vector<int>>> partition_forest_into_equal_paths(
int n, std::span<const std::pair<int, int>> edges, int path_size)
{
#ifndef SKIP_RUNTIME_CHECKS
if (n < 0)
{
throw std::length_error("partition_forest_into_equal_paths: negative vertex count");
}
if (path_size <= 0)
{
throw std::invalid_argument("partition_forest_into_equal_paths: path size must be positive");
}
#endif
std::vector<std::vector<int>> graph(n);
for (const auto &[u, v] : edges)
{
#ifndef SKIP_RUNTIME_CHECKS
if (u < 0 || u >= n || v < 0 || v >= n)
{
throw std::out_of_range("partition_forest_into_equal_paths: invalid endpoint");
}
if (u == v)
{
throw std::invalid_argument("partition_forest_into_equal_paths: self-loop");
}
#endif
graph[u].push_back(v);
graph[v].push_back(u);
}
std::vector<int> parent(n, -1);
std::vector<int> order;
order.reserve(static_cast<std::size_t>(n));
for (int root = 0; root < n; ++root)
{
if (parent[root] != -1)
{
continue;
}
parent[root] = root;
auto next = order.size();
order.push_back(root);
while (next != order.size())
{
const int u = order[next++];
for (int v : graph[u])
{
if (v == parent[u])
{
continue;
}
#ifndef SKIP_RUNTIME_CHECKS
if (parent[v] != -1)
{
throw std::invalid_argument("partition_forest_into_equal_paths: graph must be a forest");
}
#endif
parent[v] = u;
order.push_back(v);
}
}
}
std::vector<int> remainder(n), first(n, -1), second(n, -1);
std::vector<int> closed;
for (auto it = order.rbegin(); it != order.rend(); ++it)
{
const int u = *it;
int length = 1;
// 子部分木から出せる未完成のパスは1本まで。2本をつないだらここで閉じる。
for (int v : graph[u])
{
if (v == parent[u] || remainder[v] == 0)
{
continue;
}
if (first[u] == -1)
{
first[u] = v;
}
else if (second[u] == -1)
{
second[u] = v;
}
else
{
return std::nullopt;
}
length += remainder[v];
}
if (length == path_size)
{
closed.push_back(u);
}
else
{
if (length > path_size || second[u] != -1 || parent[u] == u)
{
return std::nullopt;
}
remainder[u] = length;
}
}
std::vector<std::vector<int>> result;
result.reserve(closed.size());
for (int u : closed)
{
auto &path = result.emplace_back();
path.reserve(static_cast<std::size_t>(path_size));
for (int v = u; v != -1; v = first[v])
{
path.push_back(v);
}
std::ranges::reverse(path);
for (int v = second[u]; v != -1; v = first[v])
{
path.push_back(v);
}
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1