競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
2人が交互に有向辺をたどり、動けなくなった側が負けるゲームを解きます。同じ頂点や辺を何度でも使えます。本体
solve_directed_game(n, edges) は $0$ 始まりの端点の組を受け取り、各頂点に対して手番側の結果を返します。
outcome は DirectedGameOutcome::win、lose、draw のいずれかです。plies は勝つ側が早く終わらせ、負ける側が長く粘る場合の残り手数です。引き分けなら -1 です。next はその手数を達成する行先です。引き分けでは負けを避けられる行先を返し、動けない頂点では -1 になります。動けない頂点を負けとして確定し、辺を逆向きにたどって結果を伝えます。勝ちも負けも確定しない頂点は引き分けです。時間・領域は $O(n+m)$ です。
const std::array edges{std::pair{0, 1}, std::pair{2, 2}};
const auto positions = sirkit::solve_directed_game(3, edges);
// 0 では勝ち、1 では負け、2 では引き分け。
solve_directed_game(n,edges): 2人が有向辺を交互にたどり、動けない側が負けるゲームを解く。
edgesは0始まりの(from,to)。同じ頂点や辺を再訪可。時間・領域O(n+m)。
各頂点のDirectedGamePosition{outcome,plies,next}を返す。
outcomeは手番側のwin/lose/draw。無限に続けて互いに負けを避けられるならdraw。
pliesは勝者が早く終わらせ、敗者が粘る場合の残り手数。drawなら-1。
nextはその手数を達成できる移動先。drawでは引き分けを保つ行先。終端は-1。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Shiritori | directed_game.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/directed_game.md
//
// API一覧
// solve_directed_game(n,edges): 2人が有向辺を交互にたどり、動けない側が負けるゲームを解く。
// edgesは0始まりの(from,to)。同じ頂点や辺を再訪可。時間・領域O(n+m)。
// 各頂点のDirectedGamePosition{outcome,plies,next}を返す。
// outcomeは手番側のwin/lose/draw。無限に続けて互いに負けを避けられるならdraw。
// pliesは勝者が早く終わらせ、敗者が粘る場合の残り手数。drawなら-1。
// nextはその手数を達成できる移動先。drawでは引き分けを保つ行先。終端は-1。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_DIRECTED_GAME_DEFINED
#define SIRKIT_DIRECTED_GAME_DEFINED
enum class DirectedGameOutcome : std::uint8_t
{
win,
lose,
draw,
};
struct DirectedGamePosition
{
DirectedGameOutcome outcome = DirectedGameOutcome::draw;
int plies = -1;
int next = -1;
};
inline std::vector<DirectedGamePosition> solve_directed_game(
int n, std::span<const std::pair<int, int>> edges)
{
#ifndef SKIP_RUNTIME_CHECKS
if (n < 0)
{
throw std::invalid_argument("directed game: negative vertex count");
}
#endif
using Outcome = DirectedGameOutcome;
std::vector<std::size_t> remaining(n);
std::vector<std::vector<int>> predecessors(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("directed game: endpoint out of range");
}
#endif
++remaining[u];
predecessors[v].push_back(u);
}
std::vector<DirectedGamePosition> result(n);
std::vector<int> pending;
pending.reserve(n);
for (int vertex = 0; vertex < n; ++vertex)
{
if (remaining[vertex] == 0)
{
result[vertex] = {Outcome::lose, 0, -1};
pending.push_back(vertex);
}
}
// 確定頂点を手数の昇順で処理する。勝ち側は最初の負け遷移、負け側は最後の勝ち遷移を選ぶ。
for (std::size_t i = 0; i < pending.size(); ++i)
{
const int v = pending[i];
const auto position = result[v];
for (const int u : predecessors[v])
{
if (result[u].outcome != Outcome::draw)
{
continue;
}
if (position.outcome == Outcome::lose)
{
result[u] = {Outcome::win, position.plies + 1, v};
pending.push_back(u);
}
else if (--remaining[u] == 0)
{
result[u] = {Outcome::lose, position.plies + 1, v};
pending.push_back(u);
}
}
}
for (const auto &[u, v] : edges)
{
if (result[u].outcome == Outcome::draw && result[u].next == -1 && result[v].outcome == Outcome::draw)
{
result[u].next = v;
}
}
return result;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1