This documentation is automatically generated by online-judge-tools/verification-helper
#include "other/connected_dp.hpp"
#include "ds/hashmap.hpp"
#include "random/hash_vector.hpp"
// dot case:
// https://atcoder.jp/contests/ttpc2023/submissions/57905985
// https://qoj.ac/problem/9
namespace connected_dp_squares {
// pair<新しい状態、今の成分 → 新しい成分>
vc<pair<vc<int>, vc<int>>> next_states(const vc<int>& now) {
int N = len(now);
vc<pair<vc<int>, vc<int>>> res;
FOR(s, 1 << N) {
vc<int> par(N + N);
FOR(i, N) par[i] = (s & 1 << i ? i : -1);
FOR(i, N) par[N + i] = (now[i] == -1 ? -1 : now[i] + N);
auto find = [&](int x) -> int {
while (par[x] != x) { x = par[x] = par[par[x]]; }
return x;
};
auto merge = [&](int a, int b) -> void {
a = find(a), b = find(b);
if (a == b) return;
if (a > b) swap(a, b);
par[b] = a;
};
FOR(i, N - 1) if (par[i] != -1 && par[i + 1] != -1) merge(i, i + 1);
FOR(i, N) if (par[i] != -1 && par[N + i] != -1) merge(i, N + i);
FOR(i, N + N) if (par[i] != -1) par[i] = find(i);
FOR(i, N, N + N) if (par[i] >= N) par[i] = -1;
res.eb(vc<int>(par.begin(), par.begin() + N), vc<int>(par.begin() + N, par.end()));
}
return res;
}
vc<int> reverse_state(const vc<int>& now) {
int N = len(now);
vc<int> max_i(N, -1);
FOR(i, N) if (now[i] != -1) max_i[now[i]] = i;
vc<int> rev(N, -1);
FOR(i, N) {
if (now[i] == -1) continue;
int x = max_i[now[i]];
rev[N - 1 - i] = N - 1 - x;
}
return rev;
}
// 0, 1 :空の列、領域の手前、後ろ
// 連結領域をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> connedted_dp_graph(int N, bool merge_reverse) {
HashMap<int> MP;
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
u64 h = hash_vector<int>(nxt);
if (merge_reverse) { chmin(h, hash_vector<int>(reverse_state(nxt))); }
if (!MP.count(h)) { MP[h] = len(states), states.eb(nxt); }
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
// 0, 1 :空の列、領域の手前、後ろ
// 多角形(空洞なし)をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> polygon_dp_graph(int N) {
HashMap<int> MP;
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
bool ok = [&](vc<int>& now, vc<int>& nxt, vc<int>& convert) -> bool {
// 頂点のみで接するのはダメ
FOR(i, N - 1) {
bool a1 = now[i] != -1, a2 = now[i + 1] != -1;
bool b1 = nxt[i] != -1, b2 = nxt[i + 1] != -1;
if (a1 && !a2 && !b1 && b2) return false;
if (!a1 && a2 && b1 && !b2) return false;
}
// empty region を閉じることと、異なる連結成分がマージされることが同値
int close = 0;
int after = 0;
vc<bool> is_new(N, 1);
FOR(i, N) if (convert[i] != -1) is_new[convert[i]] = 0;
FOR(i, N) if (nxt[i] == i && !is_new[i])++ after;
vc<int> I;
FOR(i, N) if (now[i] != -1) I.eb(i);
FOR(k, len(I) - 1) {
int i = I[k], j = I[k + 1];
if (j == i + 1) continue;
bool cl = 1;
FOR(p, i + 1, j) if (nxt[p] == -1) cl = 0;
if (cl) close++;
}
return a - close == after;
}(now, nxt, convert);
if (!ok) continue;
u64 h = hash_vector<int>(nxt);
if (!MP.count(h)) {
MP[h] = len(states);
states.eb(nxt);
}
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
} // namespace connected_dp_squares
#line 2 "ds/hashmap.hpp"
// u64 -> Val
template <typename Val>
struct HashMap {
// n は入れたいものの個数で ok
HashMap(u32 n = 0) { build(n); }
void build(u32 n) {
u32 k = 8;
while (k < n * 2) k *= 2;
cap = k / 2, mask = k - 1;
key.resize(k), val.resize(k), used.assign(k, 0);
}
// size を保ったまま. size=0 にするときは build すること.
void clear() {
used.assign(len(used), 0);
cap = (mask + 1) / 2;
}
int size() { return len(used) / 2 - cap; }
int index(const u64& k) {
int i = 0;
for (i = hash(k); used[i] && key[i] != k; i = (i + 1) & mask) {}
return i;
}
Val& operator[](const u64& k) {
if (cap == 0) extend();
int i = index(k);
if (!used[i]) { used[i] = 1, key[i] = k, val[i] = Val{}, --cap; }
return val[i];
}
Val get(const u64& k, Val default_value) {
int i = index(k);
return (used[i] ? val[i] : default_value);
}
bool count(const u64& k) {
int i = index(k);
return used[i] && key[i] == k;
}
// f(key, val)
template <typename F>
void enumerate_all(F f) {
FOR(i, len(used)) if (used[i]) f(key[i], val[i]);
}
private:
u32 cap, mask;
vc<u64> key;
vc<Val> val;
vc<bool> used;
u64 hash(u64 x) {
static const u64 FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
x += FIXED_RANDOM;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return (x ^ (x >> 31)) & mask;
}
void extend() {
vc<pair<u64, Val>> dat;
dat.reserve(len(used) / 2 - cap);
FOR(i, len(used)) {
if (used[i]) dat.eb(key[i], val[i]);
}
build(2 * len(dat));
for (auto& [a, b]: dat) (*this)[a] = b;
}
};
#line 2 "random/hash_vector.hpp"
#line 2 "random/base.hpp"
u64 RNG_64() {
static u64 x_ = u64(chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count()) * 10150724397891781847ULL;
x_ ^= x_ << 7;
return x_ ^= x_ >> 9;
}
u64 RNG(u64 lim) { return RNG_64() % lim; }
ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 2 "mod/modint61.hpp"
struct modint61 {
static constexpr u64 mod = (1ULL << 61) - 1;
u64 val;
constexpr modint61() : val(0ULL) {}
constexpr modint61(u32 x) : val(x) {}
constexpr modint61(u64 x) : val(x % mod) {}
constexpr modint61(int x) : val((x < 0) ? (x + static_cast<ll>(mod)) : x) {}
constexpr modint61(ll x) : val(((x %= static_cast<ll>(mod)) < 0) ? (x + static_cast<ll>(mod)) : x) {}
static constexpr u64 get_mod() { return mod; }
modint61 &operator+=(const modint61 &a) {
val = ((val += a.val) >= mod) ? (val - mod) : val;
return *this;
}
modint61 &operator-=(const modint61 &a) {
val = ((val -= a.val) >= mod) ? (val + mod) : val;
return *this;
}
modint61 &operator*=(const modint61 &a) {
const unsigned __int128 y = static_cast<unsigned __int128>(val) * a.val;
val = (y >> 61) + (y & mod);
val = (val >= mod) ? (val - mod) : val;
return *this;
}
modint61 operator-() const { return modint61(val ? mod - val : u64(0)); }
modint61 &operator/=(const modint61 &a) { return (*this *= a.inverse()); }
modint61 operator+(const modint61 &p) const { return modint61(*this) += p; }
modint61 operator-(const modint61 &p) const { return modint61(*this) -= p; }
modint61 operator*(const modint61 &p) const { return modint61(*this) *= p; }
modint61 operator/(const modint61 &p) const { return modint61(*this) /= p; }
bool operator<(const modint61 &other) const { return val < other.val; }
bool operator==(const modint61 &p) const { return val == p.val; }
bool operator!=(const modint61 &p) const { return val != p.val; }
modint61 inverse() const {
ll a = val, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b), swap(u -= t * v, v);
}
return modint61(u);
}
modint61 pow(ll n) const {
assert(n >= 0);
modint61 ret(1), mul(val);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul, n >>= 1;
}
return ret;
}
};
#ifdef FASTIO
void rd(modint61 &x) {
fastio::rd(x.val);
assert(0 <= x.val && x.val < modint61::mod);
}
void wt(modint61 x) { fastio::wt(x.val); }
#endif
#line 5 "random/hash_vector.hpp"
template <typename T>
u64 hash_vector(vc<T> X) {
using mint = modint61;
static vc<mint> hash_base;
int n = len(X);
while (len(hash_base) <= n) { hash_base.eb(RNG(mint::get_mod())); }
mint H = 0;
FOR(i, n) H += hash_base[i] * mint(X[i]);
H += hash_base[n];
return H.val;
}
template <typename T, int K>
u64 hash_array(array<T, K> X) {
using mint = modint61;
static array<mint, K> hash_base{};
if (hash_base[0] == mint(0)) FOR(i, K) hash_base[i] = RNG_64();
mint H = 0;
FOR(i, K) H += hash_base[i] * mint(X[i]);
return H.val;
}
#line 3 "other/connected_dp.hpp"
// dot case:
// https://atcoder.jp/contests/ttpc2023/submissions/57905985
// https://qoj.ac/problem/9
namespace connected_dp_squares {
// pair<新しい状態、今の成分 → 新しい成分>
vc<pair<vc<int>, vc<int>>> next_states(const vc<int>& now) {
int N = len(now);
vc<pair<vc<int>, vc<int>>> res;
FOR(s, 1 << N) {
vc<int> par(N + N);
FOR(i, N) par[i] = (s & 1 << i ? i : -1);
FOR(i, N) par[N + i] = (now[i] == -1 ? -1 : now[i] + N);
auto find = [&](int x) -> int {
while (par[x] != x) { x = par[x] = par[par[x]]; }
return x;
};
auto merge = [&](int a, int b) -> void {
a = find(a), b = find(b);
if (a == b) return;
if (a > b) swap(a, b);
par[b] = a;
};
FOR(i, N - 1) if (par[i] != -1 && par[i + 1] != -1) merge(i, i + 1);
FOR(i, N) if (par[i] != -1 && par[N + i] != -1) merge(i, N + i);
FOR(i, N + N) if (par[i] != -1) par[i] = find(i);
FOR(i, N, N + N) if (par[i] >= N) par[i] = -1;
res.eb(vc<int>(par.begin(), par.begin() + N), vc<int>(par.begin() + N, par.end()));
}
return res;
}
vc<int> reverse_state(const vc<int>& now) {
int N = len(now);
vc<int> max_i(N, -1);
FOR(i, N) if (now[i] != -1) max_i[now[i]] = i;
vc<int> rev(N, -1);
FOR(i, N) {
if (now[i] == -1) continue;
int x = max_i[now[i]];
rev[N - 1 - i] = N - 1 - x;
}
return rev;
}
// 0, 1 :空の列、領域の手前、後ろ
// 連結領域をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> connedted_dp_graph(int N, bool merge_reverse) {
HashMap<int> MP;
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
u64 h = hash_vector<int>(nxt);
if (merge_reverse) { chmin(h, hash_vector<int>(reverse_state(nxt))); }
if (!MP.count(h)) { MP[h] = len(states), states.eb(nxt); }
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
// 0, 1 :空の列、領域の手前、後ろ
// 多角形(空洞なし)をひとつ作る。
// 状態:-1 が選んでいない。0,1,2,3 等は同じ成分には同じ値が入る。
// [states, edges]
pair<vvc<int>, vc<pair<int, int>>> polygon_dp_graph(int N) {
HashMap<int> MP;
vvc<int> states;
vc<pair<int, int>> edges;
states.eb(vc<int>(N, -1));
states.eb(vc<int>(N, -1));
MP[hash_vector<int>(states[0])] = 0;
int p = -1;
while (1) {
if (++p == len(states)) break;
if (p == 1) {
edges.eb(1, 1);
continue;
}
vc<int> now = states[p];
for (auto&& [nxt, convert]: next_states(now)) {
// 今の成分数、消える成分数
int a = 0, b = 0;
FOR(v, N) if (now[v] == v) {
++a;
if (convert[v] == -1) ++b;
}
// 消える成分があってよいのは、終状態にいくときのみ
if (b >= 2) continue;
if (b == 1) {
if (MAX(nxt) != -1) continue;
edges.eb(p, 1);
continue;
}
bool ok = [&](vc<int>& now, vc<int>& nxt, vc<int>& convert) -> bool {
// 頂点のみで接するのはダメ
FOR(i, N - 1) {
bool a1 = now[i] != -1, a2 = now[i + 1] != -1;
bool b1 = nxt[i] != -1, b2 = nxt[i + 1] != -1;
if (a1 && !a2 && !b1 && b2) return false;
if (!a1 && a2 && b1 && !b2) return false;
}
// empty region を閉じることと、異なる連結成分がマージされることが同値
int close = 0;
int after = 0;
vc<bool> is_new(N, 1);
FOR(i, N) if (convert[i] != -1) is_new[convert[i]] = 0;
FOR(i, N) if (nxt[i] == i && !is_new[i])++ after;
vc<int> I;
FOR(i, N) if (now[i] != -1) I.eb(i);
FOR(k, len(I) - 1) {
int i = I[k], j = I[k + 1];
if (j == i + 1) continue;
bool cl = 1;
FOR(p, i + 1, j) if (nxt[p] == -1) cl = 0;
if (cl) close++;
}
return a - close == after;
}(now, nxt, convert);
if (!ok) continue;
u64 h = hash_vector<int>(nxt);
if (!MP.count(h)) {
MP[h] = len(states);
states.eb(nxt);
}
edges.eb(p, MP[h]);
}
}
return {states, edges};
}
} // namespace connected_dp_squares