「PKUWC2018」随机算法 - 状压dp

给定一张nn个点mm条边的图,按照以下算法求最大独立集

  1. 等概率随机一个 1n1\ldots n的排列 p[1n]p[1\ldots n]
  2. 维护答案集合 SS ,一开始 SS 为空集,之后按照 i=1ni=1\ldots n的顺序,检查 {p[i]}S\{p[i]\}\cup S是否是一个独立集,如果是的话就令 S={p[i]}SS=\{p[i]\}\cup S
  3. 最后得到一个独立集 SS 作为答案。

求这个算法的正确率,答案对998244353998244353取模

n20n\le 20

LOJ2540

Solution

又是一个假概率题,先算出方案数,最后除以n!n!

首先可以O(n3n)O(n3^n)暴力dp,每个点有三种状态:考虑过且在独立集中,考虑过且不在独立集中,没考虑过

考虑优化这个dp,把前两种状态合并起来,设dp[i][state]dp[i][state]表示当前独立集大小为ii,考虑/没考虑的状态为statestate的方案数

因为对于每个接下来要加入独立集中的点k(kstate)k(k\notin state),显然与它有连边的点都不能加入独立集中,也就是必须要和这个点一起考虑,才能保证无后效性

因此选完kk后就必须紧接着选择wkw_k,但wkw_k的内部排列方式可能不同,于是要乘上一个排列数

wkw_k为与kk相邻的所有点(包括它自己)的集合,那么有

dp[i][state]Ans1wkwkstate1dp[i+1][statewk]dp[i][state] * A_{n-|s|-1}^{|w_k-w_k\cap state| - 1} \rightarrow dp[i + 1][state \cup w_k]

时间复杂度O(2nn2)O(2^nn^2)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void proc_status()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl;
}

template <typename T> T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int Maxn = 21 + 10, Maxs = (1 << 20) + 10000, Mod = 998244353;

int N, M;
int e, Begin[Maxn], To[Maxn << 2], Next[Maxn << 2];
int W[Maxn], Dp[Maxn][Maxs], fac[Maxn], ifac[Maxn];
int Cnt[Maxs];

inline int Pow (int a, int b)
{
int ans = 1;
while (b) { if (b & 1) ans = 1ll * ans * a % Mod; a = 1ll * a * a % Mod; b >>= 1; }
return ans;
}

inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }

inline int Calc_P (int n, int m) { return 1ll * fac[n] * ifac[n - m] % Mod; }

inline void Add (int &a, int b) { a += b; if (a >= Mod) a -= Mod; }

inline void Solve ()
{
int ALL = (1 << N) - 1;
for (int i = 1; i <= N; ++i)
{
W[i] = 1 << (i - 1);
for (int j = Begin[i]; j; j = Next[j])
W[i] |= (1 << (To[j] - 1));
}
for (int i = 0; i <= ALL; ++i) Cnt[i] = __builtin_popcount(i);

Dp[0][0] = 1;
for (int i = 0; i <= N; ++i)
{
for (int state = 0; state <= ALL; ++state)
{
if (!Dp[i][state]) continue;
for (int k = 1; k <= N; ++k)
{
if ((1 << (k - 1)) & state) continue;
int cnt = Cnt[W[k] ^ (W[k] & state)];
Add (Dp[i + 1][state | W[k]], 1ll * Dp[i][state] * Calc_P(N - Cnt[state] - 1, cnt - 1) % Mod);
// cout<<state<<" "<<(state | W[k])<<" "<<Dp[i][state]<<" "<<Calc_P(N - Cnt[state] - 1, cnt)<<endl;
}
}
}
for (int i = N; i >= 0; --i)
if (Dp[i][ALL])
{
cout<<1ll * Dp[i][ALL] * ifac[N] % Mod<<endl;
return ;
}
}

inline void Input ()
{
N = read<int>(), M = read<int>();
while (M--)
{
int x = read<int>(), y = read<int>();
add_edge (x, y);
add_edge (y, x);
}
}

inline void Init (int maxn)
{
fac[0] = 1;
for (int i = 1; i <= maxn; ++i) fac[i] = 1ll * fac[i - 1] * i % Mod;
ifac[maxn] = Pow(fac[maxn], Mod - 2);
for (int i = maxn - 1; i >= 0; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % Mod;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Init(20);
Input();
Solve();
return 0;
}
感谢你的赞赏!