P5492 [PKUWC2018] 随机算法 题解
Super_Cube · · 题解
Solution
观察到
设
顺推方便一点。假设找到了一个点
记录一下这样能加入的点,假设一共有
最后设最大独立集的大小为
时间复杂度:
Code
#include <bits/stdc++.h>
const int mod = 998244353;
inline int qpow(int x, int y) {
static int res;
res = 1;
while (y) {
if (y & 1)
res = 1ll * res * x % mod;
y >>= 1;
x = 1ll * x * x % mod;
}
return res;
}
int dp[1048576];
int v[20];
int n, m, res, ans;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) v[i] |= 1 << i;
for (int x, y; m--;) scanf("%d%d", &x, &y), --x, --y, v[x] |= 1 << y, v[y] |= 1 << x;
dp[0] = 1;
for (int i = 0, cnt; i < (1 << n); ++i) {
if (!dp[i])
continue;
res = std::max(res, __builtin_popcount(i));//最大独立集的大小
cnt = 0;
for (int j = 0; j < n; ++j)
if (!(i & v[j]))
++cnt;
cnt = qpow(cnt, mod - 2);//逆元
for (int j = 0; j < n; ++j)
if (!(i & v[j]))
dp[i | (1 << j)] = (dp[i | (1 << j)] + 1ll * dp[i] * cnt % mod) % mod;
}
for (int i = 0; i < (1 << n); ++i)
if (__builtin_popcount(i) == res)
ans = (ans + dp[i]) % mod;
printf("%d", ans);
return 0;
}