DPO Matching
题意
给定一张大小为 \(2n\) 的图,求该图二分图匹配的方案数。
\(n \le 21\)。
Sol
状压板题。
设 \(f_T\) 表示 \(T\) 集合内的点被匹配。
直接转移即可。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 22, mod = 1e9 + 7;
array <array <int, N>, N> G;
array <int, 1 << N> f;
void Mod(int &x) {
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
int main() {
int n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
G[i][j] = read();
f[0] = 1;
for (int T = 0; T < 1 << n; T++) {
int x = T, y = 0;
while (x) {
if (x & 1) y++;
x >>= 1;
}
for (int i = 1; i <= n; i++) {
if (!G[y][i] || !(T & (1 << (i - 1)))) continue;
f[T] += f[T - (1 << (i - 1))], Mod(f[T]);
}
}
write(f[(1 << n) - 1]), puts("");
return 0;
}