sirkit-cpp-library

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

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

支配木

Graph/Dominator_Tree.cpp · ソース · 文書 現行ACあり Last Commit

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

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

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

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

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

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

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

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

有向グラフで root から v へ向かうすべての経路が u を通るとき、u は v を支配します。dominator_tree は、各頂点を支配する自身以外の頂点のうち、木で最も近い頂点を求めます。

const std::vector<std::vector<int>> graph{{1, 2}, {3}, {3}, {4}, {}, {}};
const auto parent = sirkit::dominator_tree(graph, 0); // {0, 0, 0, 0, 3, -1}

dominator_tree

std::vector<int> dominator_tree(const std::vector<std::vector<int>> &graph, int root);

graph[v] に頂点 v の出辺の行き先を入れます。頂点番号は0始まりです。root 自身には root、到達不能な頂点には -1 を返します。自己ループと多重辺も扱えます。

頂点数を N、辺数を M として、時間は O((N+M) log(N+1))、領域は O(N+M) です。

使用例 / Verify

参考 Luzhiled

ソースのAPI注記
dominator_tree(graph,root): 有向グラフの各頂点の直近支配頂点を返す。
root自身はroot、rootから到達不能なら-1。自己ループ・多重辺も可。
O((N+M) log(N+1))時間・O(N+M)領域。DFSと祖先の経路圧縮は反復で実行。
rootは0以上N未満。検査有効時はrootと到達可能な頂点の出辺の行き先を検査する。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Dominator Tree dominator_tree.cpp ✓ 現行AC AC

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

ローカル・CI検査

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

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

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

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考: https://ei1333.github.io/library/graph/others/dominator-tree.hpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/dominator_tree.md
//
// API一覧
// dominator_tree(graph,root): 有向グラフの各頂点の直近支配頂点を返す。
// root自身はroot、rootから到達不能なら-1。自己ループ・多重辺も可。
// O((N+M) log(N+1))時間・O(N+M)領域。DFSと祖先の経路圧縮は反復で実行。
// rootは0以上N未満。検査有効時はrootと到達可能な頂点の出辺の行き先を検査する。

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

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

namespace detail
{
inline void CheckDominatorTreeSize(std::size_t size)
{
    if (size >= static_cast<std::size_t>(std::numeric_limits<int>::max()))
    {
        throw std::length_error("dominator_tree: vertex count");
    }
}
} // namespace detail

inline std::vector<int> dominator_tree(const std::vector<std::vector<int>> &graph, int root)
{
    detail::CheckDominatorTreeSize(graph.size());
#ifndef SKIP_RUNTIME_CHECKS
    if (root < 0 || static_cast<std::size_t>(root) >= graph.size())
    {
        throw std::out_of_range("dominator_tree: root");
    }
#endif
    // DFS順は1始まりとし、0は未訪問および未接続を表す。
    std::vector<int> number(graph.size());
    std::vector<int> vertex{0, root}, parent{0, 0};
    struct Frame
    {
        int vertex;
        std::size_t next = 0;
    };
    std::vector<Frame> stack{{root}};
    number[root] = 1;
    while (!stack.empty())
    {
        auto &frame = stack.back();
        const int from = frame.vertex;
        if (frame.next == graph[from].size())
        {
            stack.pop_back();
            continue;
        }
        const int to = graph[from][frame.next++];
#ifndef SKIP_RUNTIME_CHECKS
        if (to < 0 || static_cast<std::size_t>(to) >= graph.size())
        {
            throw std::out_of_range("dominator_tree: edge endpoint");
        }
#endif
        if (number[to] == 0)
        {
            number[to] = static_cast<int>(vertex.size());
            vertex.push_back(to);
            parent.push_back(number[from]);
            stack.push_back({to});
        }
    }

    const int size = static_cast<int>(vertex.size());
    std::vector<std::vector<int>> predecessors(size), bucket(size);
    for (int i = 1; i < size; ++i)
    {
        for (int to : graph[vertex[i]])
        {
            predecessors[number[to]].push_back(i);
        }
    }

    std::vector<int> semi(size), label(size), ancestor(size), immediate(size);
    std::iota(semi.begin(), semi.end(), 0);
    std::iota(label.begin(), label.end(), 0);
    std::vector<int> path;
    const auto eval = [&](int v)
    {
        path.clear();
        for (int u = v; ancestor[ancestor[u]] != 0; u = ancestor[u])
        {
            path.push_back(u);
        }
        // 最小の半支配頂点を与えるラベルを伝えながら祖先を圧縮する。
        for (auto it = path.rbegin(); it != path.rend(); ++it)
        {
            const int u = *it;
            const int previous = ancestor[u];
            if (semi[label[previous]] < semi[label[u]])
            {
                label[u] = label[previous];
            }
            ancestor[u] = ancestor[previous];
        }
        return label[v];
    };

    for (int i = size - 1; i >= 2; --i)
    {
        for (int previous : predecessors[i])
        {
            semi[i] = std::min(semi[i], semi[eval(previous)]);
        }
        bucket[semi[i]].push_back(i);
        ancestor[i] = parent[i];
        auto &pending = bucket[parent[i]];
        for (int node : pending)
        {
            const int representative = eval(node);
            immediate[node] = semi[representative] < semi[node] ? representative : parent[i];
        }
        pending.clear();
    }

    std::vector<int> result(graph.size(), -1);
    result[root] = root;
    for (int i = 2; i < size; ++i)
    {
        if (immediate[i] != semi[i])
        {
            immediate[i] = immediate[immediate[i]];
        }
        result[vertex[i]] = vertex[immediate[i]];
    }
    return result;
}

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