競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
元の辺が存在しない頂点どうしを結ぶグラフを補グラフと呼びます。無向補グラフ上の距離、最短経路の復元に使う直前の頂点、最短経路数を求めます。本体
complement_bfsComplementBfsResult complement_bfs(int n, std::span<const std::pair<int, int>> edges,
int source = 0, std::uint64_t modulus = 0);
頂点数 n と元の無向辺を渡します。頂点番号は [0,n)。自己辺と多重辺は結果に影響しません。空のグラフでは source=0 を指定します。
戻り値には頂点番号順に以下の配列が入ります。
distance[v] は始点からの辺数距離。到達できなければ -1。parent[v] は最短経路で v の直前に訪れる頂点。始点と到達不能頂点では -1。paths[v] は最短経路数を modulus で割った余り。到達できなければ 0。modulus=0 なら配列は空。補グラフの辺を列挙せず、頂点数 $n$、元の辺数 $m$ に対し $O(n+m)$ 時間・領域です。
const std::array removed{std::pair{0, 3}};
const auto result = sirkit::complement_bfs(4, removed, 0, 998244353);
// 頂点3への最短経路は 0→1→3 と 0→2→3。
// result.distance[3] == 2、result.paths[3] == 2。
complement_bfs(n,edges,source=0,modulus=0): 無向補グラフで幅優先探索。頂点番号は[0,n)。
edgesは元の無向辺。自己辺・平行辺は結果に影響しない。n=0ならsource=0として空の配列を返す。
ComplementBfsResult{distance,parent,paths}: 辺数距離、1つの最短経路での直前の頂点、最短経路数。
到達不能の距離・直前頂点は-1。始点の直前頂点も-1。経路数はmodulusでの剰余、到達不能なら0。
modulus=0なら経路数を数えずpathsは空。補辺を作らずO(n+m)時間・領域。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Counting Shortest Paths | complement_shortest_paths.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(距離ごとの経路数の集計): https://atcoder.jp/contests/abc319/editorial/7121
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/complement_shortest_paths.md
//
// API一覧
// complement_bfs(n,edges,source=0,modulus=0): 無向補グラフで幅優先探索。頂点番号は[0,n)。
// edgesは元の無向辺。自己辺・平行辺は結果に影響しない。n=0ならsource=0として空の配列を返す。
// ComplementBfsResult{distance,parent,paths}: 辺数距離、1つの最短経路での直前の頂点、最短経路数。
// 到達不能の距離・直前頂点は-1。始点の直前頂点も-1。経路数はmodulusでの剰余、到達不能なら0。
// modulus=0なら経路数を数えずpathsは空。補辺を作らずO(n+m)時間・領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_COMPLEMENT_SHORTEST_PATHS_DEFINED
#define SIRKIT_COMPLEMENT_SHORTEST_PATHS_DEFINED
struct ComplementBfsResult
{
std::vector<int> distance;
std::vector<int> parent;
std::vector<std::uint64_t> paths;
};
inline ComplementBfsResult complement_bfs(int n, std::span<const std::pair<int, int>> edges,
int source = 0, std::uint64_t modulus = 0)
{
if (n < 0)
{
throw std::length_error("complement_bfs: negative vertex count");
}
#ifndef SKIP_RUNTIME_CHECKS
if (source < 0 || source >= std::max(n, 1))
{
throw std::out_of_range("complement_bfs: source vertex");
}
#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("complement_bfs: edge endpoint");
}
#endif
graph[u].push_back(v);
graph[v].push_back(u);
}
if (n == 0)
{
return {};
}
ComplementBfsResult result{std::vector<int>(n, -1), std::vector<int>(n, -1), {}};
std::vector<int> next(n), neighbor_of(n, -1), visited;
visited.reserve(n);
std::iota(next.begin(), next.end(), std::uint32_t{1});
int head = 0;
int &before_source = source == 0 ? head : next[source - 1];
before_source = source + 1;
visited.push_back(source);
result.distance[source] = 0;
for (std::size_t cursor = 0; cursor < visited.size(); ++cursor)
{
const int u = visited[cursor];
for (const int v : graph[u])
{
neighbor_of[v] = u;
}
// 未訪問のまま残す候補は元グラフの隣接頂点に限られる。
int *link = &head;
while (*link != n)
{
const int v = *link;
if (neighbor_of[v] == u)
{
link = &next[v];
}
else
{
*link = next[v];
result.distance[v] = result.distance[u] + 1;
result.parent[v] = u;
visited.push_back(v);
}
}
}
if (modulus == 0)
{
return result;
}
result.paths.resize(n);
std::vector<std::uint64_t> layer_total(static_cast<std::size_t>(result.distance[visited.back()]) + 1);
layer_total[0] = result.paths[source] = 1 % modulus;
std::fill(neighbor_of.begin(), neighbor_of.end(), -1);
for (std::size_t cursor = 1; cursor < visited.size(); ++cursor)
{
const int u = visited[cursor];
const int layer = result.distance[u];
auto count = layer_total[layer - 1];
for (const int v : graph[u])
{
if (result.distance[v] == layer - 1 && neighbor_of[v] != u)
{
neighbor_of[v] = u;
// 直前の層のうち、補グラフで辺のない頂点の経路数を引く。
const auto invalid = result.paths[v];
count = count >= invalid ? count - invalid : modulus - (invalid - count);
}
}
result.paths[u] = count;
auto &total = layer_total[layer];
total = total >= modulus - count ? total - (modulus - count) : total + count;
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1