This documentation is automatically generated by online-judge-tools/verification-helper
#include "flow/bipartite_dense.hpp"
#include "ds/my_bitset.hpp"
// vc<bitset> で biadj matrix を渡す
// N_1^2N_2/w.
// (5000,5000) で 300ms 程度で動く場合がある
// https://qoj.ac/problem/6308
// (10000?, 5000) 655ms
// https://codeforces.com/contest/1045/problem/A
// (10000, 20000) で 3837ms
// https://codeforces.com/contest/786/problem/E
// (10000, 10000) 6500ms TLE. row, col とも shuffle で 3000ms AC.
// bitset の方が My_Bitset より高速?(2024/05/27)
template <typename BS>
struct BipartiteMatching_Dense {
int N1, N2;
vc<BS>& adj;
vc<int> match_1, match_2;
vc<int> que;
vc<int> prev;
BS vis;
BipartiteMatching_Dense(vc<BS>& adj, int N1, int N2) : N1(N1), N2(N2), adj(adj), match_1(N1, -1), match_2(N2, -1) {
if constexpr (is_same_v<BS, My_Bitset>) vis.resize(N2);
FOR(s, N1) bfs(s);
}
void bfs(int s) {
if (match_1[s] != -1) return;
que.resize(N1), prev.resize(N1);
int l = 0, r = 0;
prev[s] = -1;
vis.set();
que[r++] = s;
while (l < r) {
int u = que[l++];
BS cand = vis & adj[u];
for (int v = cand._Find_first(); v < N2; v = cand._Find_next(v)) {
vis[v] = 0;
if (match_2[v] != -1) {
que[r++] = match_2[v];
prev[match_2[v]] = u;
continue;
}
int a = u, b = v;
while (a != -1) {
int t = match_1[a];
match_1[a] = b, match_2[b] = a, a = prev[a], b = t;
}
return;
}
}
return;
}
vc<pair<int, int>> matching() {
vc<pair<int, int>> res;
FOR(v, N1) if (match_1[v] != -1) res.eb(v, match_1[v]);
return res;
}
pair<vc<int>, vc<int>> vertex_cover() {
vc<int> que(N1);
int l = 0, r = 0;
vis.set();
vc<bool> done(N1);
FOR(i, N1) {
if (match_1[i] == -1) done[i] = 1, que[r++] = i;
}
while (l < r) {
int a = que[l++];
BS cand = adj[a] & vis;
for (int b = cand._Find_first(); b < N2; b = cand._Find_next(b)) {
vis[b] = 0;
int to = match_2[b];
assert(to != -1);
if (!done[to]) done[to] = 1, que[r++] = to;
}
}
vc<int> left, right;
FOR(i, N1) if (!done[i]) left.eb(i);
FOR(i, N2) if (!vis[i]) right.eb(i);
return {left, right};
}
};
#line 2 "ds/my_bitset.hpp"
// https://codeforces.com/contest/914/problem/F
// https://yukicoder.me/problems/no/142
// わずかに普通の bitset より遅いときもあるようだが,
// 固定長にしたくないときや slice 操作が必要なときに使う
struct My_Bitset {
using T = My_Bitset;
int N;
vc<u64> dat;
// x で埋める
My_Bitset(int N = 0, int x = 0) : N(N) {
assert(x == 0 || x == 1);
u64 v = (x == 0 ? 0 : -1);
dat.assign((N + 63) >> 6, v);
if (N) dat.back() >>= (64 * len(dat) - N);
}
int size() { return N; }
void resize(int size) {
dat.resize((size + 63) >> 6);
int remainingBits = size & 63;
if (remainingBits != 0) {
u64 mask = (u64(1) << remainingBits) - 1;
dat.back() &= mask;
}
N = size;
}
void append(int idx, bool b) {
assert(N == idx);
resize(idx + 1), (*this)[idx] = b;
}
static T from_string(string &S) {
int N = len(S);
T ANS(N);
FOR(i, N) ANS[i] = (S[i] == '1');
return ANS;
}
// thanks to chatgpt!
class Proxy {
public:
Proxy(vc<u64> &d, int i) : dat(d), index(i) {}
operator bool() const { return (dat[index >> 6] >> (index & 63)) & 1; }
Proxy &operator=(u64 value) {
dat[index >> 6] &= ~(u64(1) << (index & 63));
dat[index >> 6] |= (value & 1) << (index & 63);
return *this;
}
void flip() {
dat[index >> 6] ^= (u64(1) << (index & 63)); // XOR to flip the bit
}
private:
vc<u64> &dat;
int index;
};
Proxy operator[](int i) { return Proxy(dat, i); }
bool operator==(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) if (dat[i] != p.dat[i]) return false;
return true;
}
T &operator&=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] &= p.dat[i];
return *this;
}
T &operator|=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] |= p.dat[i];
return *this;
}
T &operator^=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] ^= p.dat[i];
return *this;
}
T operator&(const T &p) const { return T(*this) &= p; }
T operator|(const T &p) const { return T(*this) |= p; }
T operator^(const T &p) const { return T(*this) ^= p; }
T operator~() const {
T p = (*this);
p.flip_range(0, N);
return p;
}
void set_minus_inplace(T &other) {
assert(N == other.N);
FOR(i, len(dat)) dat[i] = dat[i] & (~other.dat[i]);
}
T set_minus(T other) {
assert(N == other.N);
FOR(i, len(dat)) other.dat[i] = dat[i] & (~other.dat[i]);
return other;
}
int count() {
int ans = 0;
for (u64 val: dat) ans += popcnt(val);
return ans;
}
int dot(T &p) {
assert(N == p.N);
int ans = 0;
FOR(i, len(dat)) ans += popcnt(dat[i] & p.dat[i]);
return ans;
}
int next(int i) {
chmax(i, 0);
if (i >= N) return N;
int k = i >> 6;
{
u64 x = dat[k];
int s = i & 63;
x = (x >> s) << s;
if (x) return (k << 6) | lowbit(x);
}
FOR(idx, k + 1, len(dat)) {
if (dat[idx] == 0) continue;
return (idx << 6) | lowbit(dat[idx]);
}
return N;
}
int prev(int i) {
chmin(i, N - 1);
if (i <= -1) return -1;
int k = i >> 6;
if ((i & 63) < 63) {
u64 x = dat[k];
x &= (u64(1) << ((i & 63) + 1)) - 1;
if (x) return (k << 6) | topbit(x);
--k;
}
FOR_R(idx, k + 1) {
if (dat[idx] == 0) continue;
return (idx << 6) | topbit(dat[idx]);
}
return -1;
}
My_Bitset range(int L, int R) {
assert(L <= R);
My_Bitset p(R - L);
int rm = (R - L) & 63;
FOR(rm) {
p[R - L - 1] = bool((*this)[R - 1]);
--R;
}
int n = (R - L) >> 6;
int hi = L & 63;
int lo = 64 - hi;
int s = L >> 6;
if (hi == 0) {
FOR(i, n) { p.dat[i] ^= dat[s + i]; }
} else {
FOR(i, n) { p.dat[i] ^= (dat[s + i] >> hi) ^ (dat[s + i + 1] << lo); }
}
return p;
}
My_Bitset slice(int L, int R) { return range(L, R); }
int count_range(int L, int R) {
assert(L <= R);
int cnt = 0;
while ((L < R) && (L & 63)) cnt += (*this)[L++];
while ((L < R) && (R & 63)) cnt += (*this)[--R];
int l = L >> 6, r = R >> 6;
FOR(i, l, r) cnt += popcnt(dat[i]);
return cnt;
}
// [L,R) に p を代入
void assign_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) { (*this)[L++] = bool(p[a++]); }
while (L < R && (R & 63)) { (*this)[--R] = bool(p[--b]); }
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] = p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] = (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// [L,R) に p を xor
void xor_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
dat[L >> 6] ^= u64(p[a]) << (L & 63);
++a, ++L;
}
while (L < R && (R & 63)) {
--b, --R;
dat[R >> 6] ^= u64(p[b]) << (R & 63);
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] ^= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] ^= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// 行列基本変形で使うやつ
// p は [i:N) にしかないとして p を xor する
void xor_suffix(int i, My_Bitset &p) {
assert(N == p.N && 0 <= i && i < N);
FOR(k, i / 64, len(dat)) { dat[k] ^= p.dat[k]; }
}
// [L,R) に p を and
void and_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
if (!p[a]) (*this)[L] = 0;
a++, L++;
}
while (L < R && (R & 63)) {
--b, --R;
if (!p[b]) (*this)[R] = 0;
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] &= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] &= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// [L,R) に p を or
void or_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
dat[L >> 6] |= u64(p[a]) << (L & 63);
++a, ++L;
}
while (L < R && (R & 63)) {
--b, --R;
dat[R >> 6] |= u64(p[b]) << (R & 63);
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] |= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] |= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// 行列基本変形で使うやつ
// p は [i:N) にしかないとして p を or する
void or_suffix(int i, My_Bitset &p) {
assert(N == p.N && 0 <= i && i < N);
FOR(k, i / 64, len(dat)) { dat[k] |= p.dat[k]; }
}
// [L,R) を 1 に変更
void set_range(int L, int R) {
while (L < R && (L & 63)) { set(L++); }
while (L < R && (R & 63)) { set(--R); }
FOR(i, L >> 6, R >> 6) dat[i] = u64(-1);
}
// [L,R) を 1 に変更
void reset_range(int L, int R) {
while (L < R && (L & 63)) { reset(L++); }
while (L < R && (R & 63)) { reset(--R); }
FOR(i, L >> 6, R >> 6) dat[i] = u64(0);
}
// [L,R) を flip
void flip_range(int L, int R) {
while (L < R && (L & 63)) { flip(L++); }
while (L < R && (R & 63)) { flip(--R); }
FOR(i, L >> 6, R >> 6) dat[i] ^= u64(-1);
}
// bitset に仕様を合わせる
void set(int i) { (*this)[i] = 1; }
void reset(int i) { (*this)[i] = 0; }
void flip(int i) { (*this)[i].flip(); }
void set() {
fill(all(dat), u64(-1));
resize(N);
}
void reset() { fill(all(dat), 0); }
void flip() {
FOR(i, len(dat) - 1) { dat[i] = u64(-1) ^ dat[i]; }
int i = len(dat) - 1;
FOR(k, 64) {
if (64 * i + k >= size()) break;
flip(64 * i + k);
}
}
bool any() {
FOR(i, len(dat)) {
if (dat[i]) return true;
}
return false;
}
bool ALL() {
dat.resize((N + 63) >> 6);
int r = N & 63;
if (r != 0) {
u64 mask = (u64(1) << r) - 1;
if (dat.back() != mask) return 0;
}
for (int i = 0; i < N / 64; ++i)
if (dat[i] != u64(-1)) return false;
return true;
}
// bs[i]==true であるような i 全体
vc<int> collect_idx() {
vc<int> I;
FOR(i, N) if ((*this)[i]) I.eb(i);
return I;
}
bool is_subset(T &other) {
assert(len(other) == N);
FOR(i, len(dat)) {
u64 a = dat[i], b = other.dat[i];
if ((a & b) != a) return false;
}
return true;
}
int _Find_first() { return next(0); }
int _Find_next(int p) { return next(p + 1); }
template <typename F>
void enumerate(int L, int R, F f) {
if (L >= size()) return;
int p = ((*this)[L] ? L : _Find_next(L));
while (p < R) {
f(p);
p = _Find_next(p);
}
}
static string TO_STR[256];
string to_string() const {
if (TO_STR[0].empty()) precompute();
string S;
for (auto &x: dat) { FOR(i, 8) S += TO_STR[(x >> (8 * i) & 255)]; }
S.resize(N);
return S;
}
static void precompute() {
FOR(s, 256) {
string x;
FOR(i, 8) x += '0' + (s >> i & 1);
TO_STR[s] = x;
}
}
};
string My_Bitset::TO_STR[256];
#line 2 "flow/bipartite_dense.hpp"
// vc<bitset> で biadj matrix を渡す
// N_1^2N_2/w.
// (5000,5000) で 300ms 程度で動く場合がある
// https://qoj.ac/problem/6308
// (10000?, 5000) 655ms
// https://codeforces.com/contest/1045/problem/A
// (10000, 20000) で 3837ms
// https://codeforces.com/contest/786/problem/E
// (10000, 10000) 6500ms TLE. row, col とも shuffle で 3000ms AC.
// bitset の方が My_Bitset より高速?(2024/05/27)
template <typename BS>
struct BipartiteMatching_Dense {
int N1, N2;
vc<BS>& adj;
vc<int> match_1, match_2;
vc<int> que;
vc<int> prev;
BS vis;
BipartiteMatching_Dense(vc<BS>& adj, int N1, int N2) : N1(N1), N2(N2), adj(adj), match_1(N1, -1), match_2(N2, -1) {
if constexpr (is_same_v<BS, My_Bitset>) vis.resize(N2);
FOR(s, N1) bfs(s);
}
void bfs(int s) {
if (match_1[s] != -1) return;
que.resize(N1), prev.resize(N1);
int l = 0, r = 0;
prev[s] = -1;
vis.set();
que[r++] = s;
while (l < r) {
int u = que[l++];
BS cand = vis & adj[u];
for (int v = cand._Find_first(); v < N2; v = cand._Find_next(v)) {
vis[v] = 0;
if (match_2[v] != -1) {
que[r++] = match_2[v];
prev[match_2[v]] = u;
continue;
}
int a = u, b = v;
while (a != -1) {
int t = match_1[a];
match_1[a] = b, match_2[b] = a, a = prev[a], b = t;
}
return;
}
}
return;
}
vc<pair<int, int>> matching() {
vc<pair<int, int>> res;
FOR(v, N1) if (match_1[v] != -1) res.eb(v, match_1[v]);
return res;
}
pair<vc<int>, vc<int>> vertex_cover() {
vc<int> que(N1);
int l = 0, r = 0;
vis.set();
vc<bool> done(N1);
FOR(i, N1) {
if (match_1[i] == -1) done[i] = 1, que[r++] = i;
}
while (l < r) {
int a = que[l++];
BS cand = adj[a] & vis;
for (int b = cand._Find_first(); b < N2; b = cand._Find_next(b)) {
vis[b] = 0;
int to = match_2[b];
assert(to != -1);
if (!done[to]) done[to] = 1, que[r++] = to;
}
}
vc<int> left, right;
FOR(i, N1) if (!done[i]) left.eb(i);
FOR(i, N2) if (!vis[i]) right.eb(i);
return {left, right};
}
};