sirkit-cpp-library

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

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

森の彩色

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

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

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

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

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

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

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

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

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

ForestColoring(n, edges) に 0 始まりの無向辺を渡すと、同じ頂点に接する辺の彩色と、距離が 1 または 2 の頂点が異なる色になる彩色を求められます。孤立頂点や非連結の森も扱えます。

  • edge_coloring() は入力順に各辺の色を返します。必要な色数は最大次数 Δ です。
  • distance_two_coloring() は頂点番号順に各頂点の色を返します。頂点があれば Δ+1 色で彩色でき、頂点がなければ 0 色です。
  • count_distance_two<T>(k) は区別できる色が k 個あるときの頂点彩色を数えます。法付きの型 T を渡すと、個数をその法で求められます。

彩色の結果の color_count が色数で、colors が 0 始まりの色の列です。単純な道では辺を 2 色で交互に塗り、頂点は 3 色を繰り返して塗れます。

構築は O(n+m+1) 時間・領域で、森では m < n です。彩色と数え上げは 1 回 O(n+1) 時間で、数え上げの追加領域は O(1) です。

使用例 · 辺の彩色 · 頂点彩色の個数

ソースのAPI注記
ForestColoring(n,edges): 0始まりの無向森。閉路、多重辺、自己辺があれば例外。構築 O(n+m+1) 時間・領域。
edge_coloring(): 接する辺はすべて異なる色。最小色数は最大次数Δ。
distance_two_coloring(): 距離が1または2の頂点を異なる色にする。空でない森の最小色数はΔ+1。
  ForestColoringResult{color_count,colors}。色は0始まり。辺は入力順、頂点は番号順。空なら0色。
count_distance_two<T>(palette): 使用可能な色をpalette個とした頂点彩色の個数。空の森は1通り。
  T{0},T{1}とpalette以下の整数から構築でき、乗算可能。各操作 O(n+1) 時間。数え上げの追加領域は O(1)。非再帰。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
E - Virus Tree 2 forest_distance_two_coloring.cpp 未提出 —
D - Coloring Edges on Tree forest_edge_coloring.cpp 未提出 —

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

ローカル・CI検査

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

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

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

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/forest_coloring.md
//
// API一覧
// ForestColoring(n,edges): 0始まりの無向森。閉路、多重辺、自己辺があれば例外。構築 O(n+m+1) 時間・領域。
// edge_coloring(): 接する辺はすべて異なる色。最小色数は最大次数Δ。
// distance_two_coloring(): 距離が1または2の頂点を異なる色にする。空でない森の最小色数はΔ+1。
//   ForestColoringResult{color_count,colors}。色は0始まり。辺は入力順、頂点は番号順。空なら0色。
// count_distance_two<T>(palette): 使用可能な色をpalette個とした頂点彩色の個数。空の森は1通り。
//   T{0},T{1}とpalette以下の整数から構築でき、乗算可能。各操作 O(n+1) 時間。数え上げの追加領域は O(1)。非再帰。


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


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

struct ForestColoringResult
{
    int color_count;
    std::vector<int> colors;
};

class ForestColoring
{
public:
    ForestColoring(int n, std::span<const std::pair<int, int>> edges) : edge_count_(edges.size())
    {
        if (n < 0)
        {
            throw std::length_error("ForestColoring: negative vertex count");
        }
        adjacent_.resize(n);
        for (std::size_t i = 0; i < edges.size(); ++i)
        {
            const auto [u, v] = edges[i];
            if (u < 0 || u >= n || v < 0 || v >= n)
            {
                throw std::out_of_range("ForestColoring: edge endpoint");
            }
            adjacent_[u].push_back({v, i});
            adjacent_[v].push_back({u, i});
        }

        constexpr int kUnvisited = -2;
        parent_.assign(n, kUnvisited);
        parent_edge_.assign(n, edge_count_);
        order_.reserve(n);
        for (int root = 0; root < n; ++root)
        {
            if (parent_[root] != kUnvisited)
            {
                continue;
            }
            parent_[root] = -1;
            const auto first = order_.size();
            order_.push_back(root);
            for (std::size_t i = first; i < order_.size(); ++i)
            {
                const int v = order_[i];
                for (const auto &[next, edge] : adjacent_[v])
                {
                    if (edge == parent_edge_[v])
                    {
                        continue;
                    }
                    if (parent_[next] != kUnvisited)
                    {
                        throw std::invalid_argument("ForestColoring: graph contains a cycle");
                    }
                    parent_[next] = v;
                    parent_edge_[next] = edge;
                    order_.push_back(next);
                }
                max_degree_ = std::max(max_degree_, static_cast<int>(adjacent_[v].size()));
            }
        }
    }

    [[nodiscard]] ForestColoringResult edge_coloring() const
    {
        if (order_.empty())
        {
            return {0, {}};
        }
        ForestColoringResult result{max_degree_, std::vector<int>(edge_count_)};
        for (const int v : order_)
        {
            const auto upward = parent_edge_[v];
            const int forbidden = upward == edge_count_ ? -1 : result.colors[upward];
            int next_color = 0;
            for (const auto &[child, edge] : adjacent_[v])
            {
                if (edge != upward)
                {
                    if (next_color == forbidden)
                    {
                        ++next_color;
                    }
                    result.colors[edge] = next_color++;
                }
            }
        }
        return result;
    }

    [[nodiscard]] ForestColoringResult distance_two_coloring() const
    {
        if (order_.empty())
        {
            return {0, {}};
        }
        ForestColoringResult result{max_degree_ + 1, std::vector<int>(adjacent_.size())};
        for (const int v : order_)
        {
            const int parent = parent_[v];
            const int parent_color = parent == -1 ? -1 : result.colors[parent];
            int next_color = 0;
            for (const auto &[child, edge] : adjacent_[v])
            {
                if (child != parent)
                {
                    while (next_color == result.colors[v] || next_color == parent_color)
                    {
                        ++next_color;
                    }
                    result.colors[child] = next_color++;
                }
            }
        }
        return result;
    }

    template <class T>
    [[nodiscard]] T count_distance_two(std::uint64_t palette) const
    {
        if (order_.empty())
        {
            return T{1};
        }
        if (palette < static_cast<std::uint64_t>(max_degree_) + 1)
        {
            return T{0};
        }

        T total{1};
        for (const int v : order_)
        {
            const bool root = parent_[v] == -1;
            if (root)
            {
                total = total * T(palette);
            }
            const auto children = adjacent_[v].size() - static_cast<std::size_t>(!root);
            const auto available = palette - (root ? 1U : 2U);
            for (std::size_t position = 0; position < children; ++position)
            {
                total = total * T(available - position);
            }
        }
        return total;
    }

private:
    std::size_t edge_count_;
    int max_degree_ = 0;
    std::vector<std::vector<std::pair<int, std::size_t>>> adjacent_;
    std::vector<int> parent_, order_;
    std::vector<std::size_t> parent_edge_;
};

#endif

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