sirkit-cpp-library

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

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

出次数の偶奇を指定する辺の向き付け

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

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

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

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

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

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

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

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

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

無向グラフの各辺に向きを付け、頂点ごとの出次数を指定した偶奇にそろえます。本体

  • orient_with_outdegree_parity(n, edges, required = {}) の辺は $0$ 始まりの端点の組です。頂点 v で偶数にするなら required[v] = 0、奇数にするなら 1 を指定します。required を省くと全頂点を偶数にします。
  • 辺の入力順に (出発点, 到着点) を返します。実現できなければ std::nullopt です。非連結グラフ、多重辺、自己ループにも対応し、自己ループはその頂点の出次数を 1 増やします。

全域森以外の辺の向きを先に決め、葉から根に向かって残りの辺を調整します。各連結成分で辺数と指定した奇数頂点の数の偶奇が一致するときに限り、向き付けが可能です。時間・領域は $O(n+m)$ です。

使用例

const std::array edges{std::pair{0, 1}, std::pair{1, 2}};
const auto result = sirkit::orient_with_outdegree_parity(3, edges);
// 向きは 1 → 0 と 1 → 2。

使用例・Verify

ソースのAPI注記
orient_with_outdegree_parity(n,edges,required={}): 無向辺を指定した出次数の偶奇になるように向き付ける。
  edgesは0始まりの端点の組、required[v]は偶数なら0、奇数なら1。空なら全頂点が偶数。
  辺の入力順に(from,to)を返す。条件を満たせなければnullopt。O(n+m)時間・領域。
  非連結、自己ループ、多重辺も可。自己ループは出次数に1を加える。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

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

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

ローカル・CI検査

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

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

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

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(木の辺を決める順序): https://img.atcoder.jp/agc035/editorial.pdf
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/parity_orientation.md
//
// API一覧
// orient_with_outdegree_parity(n,edges,required={}): 無向辺を指定した出次数の偶奇になるように向き付ける。
//   edgesは0始まりの端点の組、required[v]は偶数なら0、奇数なら1。空なら全頂点が偶数。
//   辺の入力順に(from,to)を返す。条件を満たせなければnullopt。O(n+m)時間・領域。
//   非連結、自己ループ、多重辺も可。自己ループは出次数に1を加える。


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


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

inline std::optional<std::vector<std::pair<int, int>>> orient_with_outdegree_parity(
    int n, std::span<const std::pair<int, int>> edges, std::span<const std::uint8_t> required = {})
{
#ifndef SKIP_RUNTIME_CHECKS
    if (n < 0)
    {
        throw std::invalid_argument("parity orientation: negative vertex count");
    }
#endif
    std::vector<std::uint8_t> need(n);
    if (!required.empty())
    {
#ifndef SKIP_RUNTIME_CHECKS
        if (required.size() != static_cast<std::size_t>(n))
        {
            throw std::invalid_argument("parity orientation: parity count");
        }
        if (std::ranges::any_of(required, [](std::uint8_t bit)
                                {
                                    return bit > 1;
                                }))
        {
            throw std::invalid_argument("parity orientation: parity must be zero or one");
        }
#endif
        std::ranges::copy(required, need.begin());
    }

    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("parity 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.begin(), edges.end());
    std::vector<bool> entered(n), used(edges.size());
    std::vector<std::size_t> parent_edge(n);
    std::vector<int> order;
    order.reserve(n);
    for (int root = 0; root < n; ++root)
    {
        if (entered[root])
        {
            continue;
        }
        const auto begin = order.size();
        order.push_back(root);
        entered[root] = true;
        for (auto i = begin; i < order.size(); ++i)
        {
            const int u = order[i];
            for (const auto id : adjacent[u])
            {
                if (used[id])
                {
                    continue;
                }
                used[id] = true;
                const auto [a, b] = edges[id];
                const int v = a == u ? b : a;
                if (!entered[v])
                {
                    entered[v] = true;
                    parent_edge[v] = id;
                    order.push_back(v);
                }
                else
                {
                    need[a] ^= 1;
                }
            }
        }

        // 根以外は、親への辺の向きで残りの偶奇を合わせられる。
        for (auto i = order.size(); i > begin + 1;)
        {
            const int u = order[--i];
            auto &[from, to] = oriented[parent_edge[u]];
            if ((from == u) != (need[u] != 0))
            {
                std::swap(from, to);
            }
            need[from] ^= 1;
        }
        if (need[root] != 0)
        {
            return std::nullopt;
        }
    }
    return oriented;
}

#endif

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