競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// minimum_set_partition を、集合の分け方をすべて試す愚直な実装と比べる
// 使ってよい集合族が部分集合について閉じている場合と、閉じていない場合の両方を試す
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/dp/set-partition.hpp"
#include <iostream>
#include <random>
#include <vector>
int main() {
std::mt19937_64 rng(314);
bool ok = true;
for (int rep = 0; rep < 2000 && ok; rep++) {
int n = rng() % 8;
std::vector<bool> feasible(1 << n);
for (int S = 1; S < (1 << n); S++) feasible[S] = rng() % 3 == 0;
if (rep % 2 == 0) {
// 部分集合について閉じさせる (使ってよい集合の部分集合も使ってよい)
for (int S = (1 << n) - 1; S > 0; S--)
if (feasible[S])
for (int T = S; T > 0; T = (T - 1) & S) feasible[T] = true;
}
// 愚直: 要素 i に「何番目の集合か」を割り当てる分け方をすべて試す
long long expect = n == 0 ? 0 : -1;
std::vector<int> b(n, 0);
while (n > 0) {
int k = 0;
for (int x : b) k = std::max(k, x + 1);
std::vector<int> parts(k, 0);
for (int i = 0; i < n; i++) parts[b[i]] |= 1 << i;
bool good = true;
for (int p : parts) good &= p != 0 && feasible[p];
if (good && (expect == -1 || k < expect)) expect = k;
int i = 0;
while (i < n && ++b[i] == n) b[i++] = 0;
if (i == n) break;
}
if (minimum_set_partition(n, feasible) != expect) ok = false, std::cerr << "minimum_set_partition" << std::endl;
}
if (ok) std::cout << "Hello World" << std::endl;
}#line 1 "test/unittest/dp/set-partition.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// minimum_set_partition を、集合の分け方をすべて試す愚直な実装と比べる
// 使ってよい集合族が部分集合について閉じている場合と、閉じていない場合の両方を試す
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/dp/set-partition.hpp"
/**
* @file set-partition.hpp
* @brief 集合を「使ってよい集合」に分けるときの最小個数 (要素が 16 個程度まで)
*/
#include <cassert>
#include <vector>
/**
* @brief {0, ..., n-1} を、feasible[T] が true の集合 T いくつかに重ならないように分けるときの最小個数 (分けられなければ -1) O(3^n)
* feasible の大きさは 2^n。dp[S] = min (dp[S \ T] + 1) を、T を「S の最小の要素を含む S の部分集合」に限って求める
* 使ってよい集合族が部分集合について閉じているなら、重なりを許して覆うときの最小個数とも一致する
*/
inline long long minimum_set_partition(int n, const std::vector<bool>& feasible) {
assert(static_cast<long long>(feasible.size()) == (1LL << n));
const int INF = n + 1;
std::vector<int> dp(1 << n, INF);
dp[0] = 0;
for (int S = 1; S < (1 << n); S++) {
const int low = S & -S, rest = S ^ low;
// rest の部分集合 U について T = U | low を試す
for (int U = rest;; U = (U - 1) & rest) {
const int T = U | low;
if (feasible[T] && dp[S ^ T] + 1 < dp[S]) dp[S] = dp[S ^ T] + 1;
if (U == 0) break;
}
}
return dp[(1 << n) - 1] >= INF ? -1 : dp[(1 << n) - 1];
}
#line 8 "test/unittest/dp/set-partition.test.cpp"
#include <iostream>
#include <random>
#line 12 "test/unittest/dp/set-partition.test.cpp"
int main() {
std::mt19937_64 rng(314);
bool ok = true;
for (int rep = 0; rep < 2000 && ok; rep++) {
int n = rng() % 8;
std::vector<bool> feasible(1 << n);
for (int S = 1; S < (1 << n); S++) feasible[S] = rng() % 3 == 0;
if (rep % 2 == 0) {
// 部分集合について閉じさせる (使ってよい集合の部分集合も使ってよい)
for (int S = (1 << n) - 1; S > 0; S--)
if (feasible[S])
for (int T = S; T > 0; T = (T - 1) & S) feasible[T] = true;
}
// 愚直: 要素 i に「何番目の集合か」を割り当てる分け方をすべて試す
long long expect = n == 0 ? 0 : -1;
std::vector<int> b(n, 0);
while (n > 0) {
int k = 0;
for (int x : b) k = std::max(k, x + 1);
std::vector<int> parts(k, 0);
for (int i = 0; i < n; i++) parts[b[i]] |= 1 << i;
bool good = true;
for (int p : parts) good &= p != 0 && feasible[p];
if (good && (expect == -1 || k < expect)) expect = k;
int i = 0;
while (i < n && ++b[i] == n) b[i++] = 0;
if (i == n) break;
}
if (minimum_set_partition(n, feasible) != expect) ok = false, std::cerr << "minimum_set_partition" << std::endl;
}
if (ok) std::cout << "Hello World" << std::endl;
}