sirkit-cpp-library

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

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

総当たり戦の組み合わせ

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

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

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

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

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

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

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

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

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

  • RoundRobinSchedule(n) は、すべての2人組が1回ずつ対戦する日程を作ります。1ラウンドで各選手が行う試合は1つまでです。
  • round_count() は必要なラウンド数を返します。$n\le1$ なら0、偶数人なら $n-1$、奇数人なら $n$ です。
  • partner(r,u) はラウンド $r$ の相手を返します。休みなら -1 です。
  • round_of(u,v) は2人の対戦ラウンドを返します。同じ選手を渡すと -1 です。

size() は選手の人数を返します。 選手とラウンドはどちらも0始まりです。すべての操作は $O(1)$ 時間で、日程表を保存する領域は不要です。

使用例 / Verify

ソースのAPI注記
[RoundRobinSchedule]
RoundRobinSchedule(n): n人の総当たり戦を最小のラウンド数で配置。1ラウンドにつき1人1試合まで。
size()/round_count(): 選手数/ラウンド数。n<=1なら0ラウンド。
partner(r,u): ラウンドrの選手uの対戦相手。試合がなければ-1。
round_of(u,v): 選手u,vの試合のラウンド。同じ選手なら-1。
番号はすべて0始まり。負のnはinvalid_argument、範囲外の照会はout_of_range。
全操作O(1)時間、保持する領域もO(1)。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Symmetric Matrix round_robin_schedule_arc208_d.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/round_robin_schedule.md
// 参考(完全グラフの辺彩色): https://info.atcoder.jp/entry/algorithm_lectures/complete_graph_edge_coloring
//
// API一覧
// [RoundRobinSchedule]
// RoundRobinSchedule(n): n人の総当たり戦を最小のラウンド数で配置。1ラウンドにつき1人1試合まで。
// size()/round_count(): 選手数/ラウンド数。n<=1なら0ラウンド。
// partner(r,u): ラウンドrの選手uの対戦相手。試合がなければ-1。
// round_of(u,v): 選手u,vの試合のラウンド。同じ選手なら-1。
// 番号はすべて0始まり。負のnはinvalid_argument、範囲外の照会はout_of_range。
// 全操作O(1)時間、保持する領域もO(1)。


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


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

class RoundRobinSchedule
{
    int players_;

    void check_player(int player) const
    {
        if (player < 0 || player >= players_)
        {
            throw std::out_of_range("round-robin schedule: player out of range");
        }
    }

public:
    explicit RoundRobinSchedule(int players) : players_(players)
    {
        if (players < 0)
        {
            throw std::invalid_argument("round-robin schedule: negative number of players");
        }
    }

    [[nodiscard]] int size() const noexcept
    {
        return players_;
    }

    [[nodiscard]] int round_count() const noexcept
    {
        if (players_ <= 1)
        {
            return 0;
        }
        return players_ - (players_ % 2 == 0);
    }

    [[nodiscard]] int partner(int round, int player) const
    {
        check_player(player);
        const int circle = round_count();
        if (round < 0 || round >= circle)
        {
            throw std::out_of_range("round-robin schedule: round out of range");
        }
        if (player == round)
        {
            return circle < players_ ? circle : -1;
        }
        if (player == circle)
        {
            return round;
        }
        // 円周上の対称位置。偶数人のとき、円外の1人はそのラウンドの中心と対戦する。
        return static_cast<int>((2 * std::int64_t{round} + circle - player) % circle);
    }

    [[nodiscard]] int round_of(int u, int v) const
    {
        check_player(u);
        check_player(v);
        if (u == v)
        {
            return -1;
        }
        const int circle = round_count();
        if (u == circle)
        {
            return v;
        }
        if (v == circle)
        {
            return u;
        }
        const auto sum = (std::int64_t{u} + v) % circle;
        return static_cast<int>((sum + (sum % 2 == 0 ? 0 : circle)) / 2);
    }
};

#endif

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