競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
始点と終点を固定し、各辺を1本ずつ使えなくした場合の最短距離を求めます。本体
edge_removal_distances(n, edges, source, target, directed = true)
-> std::vector<int>;
頂点番号は $0$ 始まりで、辺は std::pair<int, int> です。各辺の長さは $1$ です。無向グラフには directed = false を指定し、各辺を一度だけ渡します。自己辺と多重辺も扱えます。
結果の i 番目は入力の辺 i だけを除いたときの最短距離です。到達不能なら -1 を返します。
最短路を始点からの距離で区切り、各段で最短路が通れる辺を数えます。通れる辺が1本しかない段では、その辺を除くと現在の最短路がなくなるので、幅優先探索をやり直します。該当する辺が $b$ 本なら $O((b+1)(n+m))$ 時間、$O(n+m)$ 領域です。
const std::array edges{std::pair{0, 1}, std::pair{0, 2}, std::pair{1, 2}};
const auto result = sirkit::edge_removal_distances(3, edges, 0, 2);
// {1, 2, 1}。辺 0→2 を除くと 0→1→2 の長さが2。
edge_removal_distances(n, edges, source, target, directed=true): 辺を1本ずつ除外した場合の最短距離を、入力の辺順で返す。到達不能は-1。 非重み付きグラフ、頂点0..n-1。無向ではdirected=false。自己辺・多重辺可。 再探索する辺は最短路すべてが通る辺だけ。件数bに対しO((b+1)(n+m))時間、O(n+m)領域。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Blocked Roads | edge_removal_distances.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/edge_removal_distances.md
//
// API一覧
// edge_removal_distances(n, edges, source, target, directed=true):
// 辺を1本ずつ除外した場合の最短距離を、入力の辺順で返す。到達不能は-1。
// 非重み付きグラフ、頂点0..n-1。無向ではdirected=false。自己辺・多重辺可。
// 再探索する辺は最短路すべてが通る辺だけ。件数bに対しO((b+1)(n+m))時間、O(n+m)領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_EDGE_REMOVAL_DISTANCES_DEFINED
#define SIRKIT_EDGE_REMOVAL_DISTANCES_DEFINED
namespace detail
{
using EdgeRemovalAdjacency = std::vector<std::vector<std::pair<int, std::size_t>>>;
inline void edge_removal_bfs(const EdgeRemovalAdjacency &graph, int source, int stop,
std::size_t omitted, std::vector<int> &distance, std::vector<int> &pending)
{
std::ranges::fill(distance, -1);
pending.clear();
pending.push_back(source);
distance[source] = 0;
for (std::size_t i = 0; i < pending.size(); ++i)
{
const int u = pending[i];
if (u == stop)
{
break;
}
for (const auto &[v, id] : graph[u])
{
if (id != omitted && distance[v] == -1)
{
distance[v] = distance[u] + 1;
pending.push_back(v);
}
}
}
}
} // namespace detail
inline std::vector<int> edge_removal_distances(int n, std::span<const std::pair<int, int>> edges,
int source, int target, bool directed = true)
{
if (n < 0)
{
throw std::length_error("edge_removal_distances: negative size");
}
#ifndef SKIP_RUNTIME_CHECKS
if (source < 0 || source >= n || target < 0 || target >= n)
{
throw std::out_of_range("edge_removal_distances: invalid terminal");
}
#endif
detail::EdgeRemovalAdjacency graph(n), reverse(directed ? n : 0);
for (std::size_t id = 0; id < edges.size(); ++id)
{
const auto [u, v] = edges[id];
#ifndef SKIP_RUNTIME_CHECKS
if (u < 0 || u >= n || v < 0 || v >= n)
{
throw std::out_of_range("edge_removal_distances: invalid endpoint");
}
#endif
graph[u].emplace_back(v, id);
if (directed)
{
reverse[v].emplace_back(u, id);
}
else
{
graph[v].emplace_back(u, id);
}
}
std::vector<int> pending, from_source(n);
pending.reserve(n);
detail::edge_removal_bfs(graph, source, -1, edges.size(), from_source, pending);
const int original = from_source[target];
std::vector<int> answer(edges.size(), original);
if (original <= 0)
{
return answer;
}
std::vector<int> to_target(n);
detail::edge_removal_bfs(directed ? reverse : graph, target, -1, edges.size(), to_target, pending);
std::vector<std::size_t> count(original), last(original);
auto check_layer = [&](int u, int v, std::size_t id)
{
const int step = from_source[u];
if (0 <= step && step < original && to_target[v] == original - 1 - step)
{
++count[step];
last[step] = id;
}
};
for (std::size_t id = 0; id < edges.size(); ++id)
{
const auto [u, v] = edges[id];
check_layer(u, v, id);
if (!directed)
{
check_layer(v, u, id);
}
}
std::vector<int> distance(n);
for (int step = 0; step < original; ++step)
{
// Each shortest path crosses this distance layer through one edge.
if (count[step] == 1)
{
const auto removed = last[step];
detail::edge_removal_bfs(graph, source, target, removed, distance, pending);
answer[removed] = distance[target];
}
}
return answer;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1