shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: 集合の被覆 (使ってよい集合で覆う最小個数、包除原理)
(src/dp/set-cover.hpp)

$\lbrace 0, \dots, n - 1 \rbrace$ を、「使ってよい集合」いくつかで (重なりを許して) 覆うときの最小個数を、包除原理で $O(2^n n)$ で求めます。$n$ は 20 程度まで使えます。数を素数の mod で数えるので、まれに誤ります (下を参照)。

// 彩色数: 独立集合で頂点全体を覆う最小個数
std::vector<bool> indep(1 << n);
indep[0] = true;
for (int S = 1; S < (1 << n); S++) {
    int v = __builtin_ctz(S), rest = S & (S - 1);
    indep[S] = indep[rest] && !(adj[v] & rest);  // adj[v] は v と隣り合う頂点の集合
}
long long k = minimum_set_cover(n, indep);

関数

関数 説明 計算量
minimum_set_cover(n, feasible) feasible[T] (大きさ $2^n$ の std::vector<bool>) が true の集合 $T$ いくつかで、全体を重なりを許して覆うときの最小個数。覆えなければ -1、$n = 0$ なら 0 $O(2^n n)$

関連

Verified with

Code

#pragma once

/**
 * @file set-cover.hpp
 * @brief 集合を「使ってよい集合」で覆うときの最小個数 (包除原理、要素が 20 個程度まで)
 */

#include <cassert>
#include <vector>

/**
 * @brief {0, ..., n-1} を、feasible[T] が true の集合 T いくつかで (重なりを許して) 覆うときの最小個数 (覆えなければ -1) O(2^n n)
 * feasible の大きさは 2^n。f(S) を S に含まれる使ってよい集合の数とすると、全体を覆う k 個組の数は Σ_S (-1)^{n - |S|} f(S)^k。
 * これが 0 でない最小の k を求める。数は 2 つの素数の mod で数えるので、0 でないのに 0 と誤る確率がわずかにある
 */
inline long long minimum_set_cover(int n, const std::vector<bool>& feasible) {
    assert(0 <= n && n < 31);
    assert(static_cast<long long>(feasible.size()) == (1LL << n));
    if (n == 0) return 0;
    const int N = 1 << n;
    // f[S] = S に含まれる使ってよい集合の数 (部分集合についてのゼータ変換)。2^n 以下なので mod を取らずに持てる
    std::vector<unsigned> f(N);
    for (int S = 0; S < N; S++) f[S] = feasible[S];
    for (int i = 0; i < n; i++) {
        for (int S = 0; S < N; S++) {
            if (S >> i & 1) f[S] += f[S ^ (1 << i)];
        }
    }
    if (f[N - 1] == 0) return -1;
    static constexpr unsigned long long P[2] = {998244353, 1000000007};
    // pw[j][S] = f[S]^k mod P[j]
    std::vector<unsigned> pw[2] = {std::vector<unsigned>(N, 1), std::vector<unsigned>(N, 1)};
    // 覆えるなら、各要素を含む集合を 1 つずつ選べば n 個以下で覆える
    for (int k = 1; k <= n; k++) {
        bool nonzero = false;
        for (int j = 0; j < 2; j++) {
            unsigned long long sum = 0;
            for (int S = 0; S < N; S++) {
                pw[j][S] = static_cast<unsigned>(pw[j][S] * static_cast<unsigned long long>(f[S]) % P[j]);
                // n - |S| が奇数なら引く
                sum += (__builtin_parity(S) ^ (n & 1)) ? P[j] - pw[j][S] : pw[j][S];
            }
            nonzero |= sum % P[j] != 0;
        }
        if (nonzero) return k;
    }
    return -1;
}
#line 2 "src/dp/set-cover.hpp"

/**
 * @file set-cover.hpp
 * @brief 集合を「使ってよい集合」で覆うときの最小個数 (包除原理、要素が 20 個程度まで)
 */

#include <cassert>
#include <vector>

/**
 * @brief {0, ..., n-1} を、feasible[T] が true の集合 T いくつかで (重なりを許して) 覆うときの最小個数 (覆えなければ -1) O(2^n n)
 * feasible の大きさは 2^n。f(S) を S に含まれる使ってよい集合の数とすると、全体を覆う k 個組の数は Σ_S (-1)^{n - |S|} f(S)^k。
 * これが 0 でない最小の k を求める。数は 2 つの素数の mod で数えるので、0 でないのに 0 と誤る確率がわずかにある
 */
inline long long minimum_set_cover(int n, const std::vector<bool>& feasible) {
    assert(0 <= n && n < 31);
    assert(static_cast<long long>(feasible.size()) == (1LL << n));
    if (n == 0) return 0;
    const int N = 1 << n;
    // f[S] = S に含まれる使ってよい集合の数 (部分集合についてのゼータ変換)。2^n 以下なので mod を取らずに持てる
    std::vector<unsigned> f(N);
    for (int S = 0; S < N; S++) f[S] = feasible[S];
    for (int i = 0; i < n; i++) {
        for (int S = 0; S < N; S++) {
            if (S >> i & 1) f[S] += f[S ^ (1 << i)];
        }
    }
    if (f[N - 1] == 0) return -1;
    static constexpr unsigned long long P[2] = {998244353, 1000000007};
    // pw[j][S] = f[S]^k mod P[j]
    std::vector<unsigned> pw[2] = {std::vector<unsigned>(N, 1), std::vector<unsigned>(N, 1)};
    // 覆えるなら、各要素を含む集合を 1 つずつ選べば n 個以下で覆える
    for (int k = 1; k <= n; k++) {
        bool nonzero = false;
        for (int j = 0; j < 2; j++) {
            unsigned long long sum = 0;
            for (int S = 0; S < N; S++) {
                pw[j][S] = static_cast<unsigned>(pw[j][S] * static_cast<unsigned long long>(f[S]) % P[j]);
                // n - |S| が奇数なら引く
                sum += (__builtin_parity(S) ^ (n & 1)) ? P[j] - pw[j][S] : pw[j][S];
            }
            nonzero |= sum % P[j] != 0;
        }
        if (nonzero) return k;
    }
    return -1;
}
Back to top page