sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

無向辺を向き付けて橋以外を強連結にする

Graph/Bridge_Block_Orientation.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

orient_bridge_blocks(n, edges) は無向辺に一方向を割り当てます。橋を除いてつながっていた頂点は、向き付けた後も互いに行き来できます。本体

頂点は $0$ 始まりで、辺は端点の組を渡します。結果は入力順の (出発点, 到着点) です。自己ループ、多重辺、複数の連結成分にも対応します。

DFS 木の辺は根から離れる向き、その他の辺は祖先に向かう向きにします。橋は片方向にしか通れないため、結果の強連結成分は元のグラフの二辺連結成分と一致します。時間・領域は $O(n+m)$ です。

使用例

const std::vector edges{std::pair{0, 1}, std::pair{1, 2}, std::pair{2, 0}, std::pair{2, 3}};
const auto directed = sirkit::orient_bridge_blocks(4, edges);
// 0、1、2 は互いに到達でき、辺 2 → 3 を使って 3 にも行ける。

使用例・Verify

ソースのAPI注記
orient_bridge_blocks(n,edges): 0始まりの無向グラフの全辺に向きを付ける。入力順の(from,to)を返す。
  結果の強連結成分が、元のグラフから橋をすべて除いた成分と一致する。
  非連結、自己ループ、多重辺も可。DFSは再帰を使わない。O(n+m)時間・領域。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Orientation bridge_block_orientation.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 無し
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(DFSによる向き付け): https://atcoder.jp/contests/arc111/editorial/492
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/bridge_block_orientation.md
//
// API一覧
// orient_bridge_blocks(n,edges): 0始まりの無向グラフの全辺に向きを付ける。入力順の(from,to)を返す。
//   結果の強連結成分が、元のグラフから橋をすべて除いた成分と一致する。
//   非連結、自己ループ、多重辺も可。DFSは再帰を使わない。O(n+m)時間・領域。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_BRIDGE_BLOCK_ORIENTATION_DEFINED
#define SIRKIT_BRIDGE_BLOCK_ORIENTATION_DEFINED

inline std::vector<std::pair<int, int>> orient_bridge_blocks(int n, std::span<const std::pair<int, int>> edges)
{
#ifndef SKIP_RUNTIME_CHECKS
    if (n < 0)
    {
        throw std::invalid_argument("bridge block orientation: negative vertex count");
    }
#endif
    std::vector<std::vector<std::size_t>> adjacent(n);
    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("bridge block orientation: endpoint out of range");
        }
#endif
        adjacent[u].push_back(id);
        if (u != v)
        {
            adjacent[v].push_back(id);
        }
    }

    std::vector<std::pair<int, int>> oriented(edges.size(), {-1, -1});
    std::vector<bool> entered(n);
    std::vector<std::pair<int, std::size_t>> active;
    active.reserve(n);
    for (int root = 0; root < n; ++root)
    {
        if (entered[root])
        {
            continue;
        }
        entered[root] = true;
        active.emplace_back(root, 0);
        while (!active.empty())
        {
            auto &[u, cursor] = active.back();
            if (cursor == adjacent[u].size())
            {
                active.pop_back();
                continue;
            }
            const auto id = adjacent[u][cursor++];
            if (oriented[id].first != -1)
            {
                continue;
            }
            const auto [a, b] = edges[id];
            const int v = a == u ? b : a;
            // 初めて調べる非木辺は、自己ループかDFSの祖先へ進む辺。
            oriented[id] = {u, v};
            if (!entered[v])
            {
                entered[v] = true;
                active.emplace_back(v, 0);
            }
        }
    }
    return oriented;
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page