题目链接
题解
设(f[i][S])表示从(i)节点出发,走完(S)集合中的点的期望步数
记(de[i])为(i)的度数,(E)为边集,我们很容易写出状态转移方程
①若(i
otin S)
[f[i][S] = frac{1}{de[i]}sumlimits_{(i,j) in E}(f[j][S] + 1)
]
②若(i in S)
除非({i} = S),(f[i][S] = 0)
否则
[f[i][S] = frac{1}{de[i]}sumlimits_{(i,j) in E}(f[j][S - {i}] + 1)
]
容易发现转移到的集合(S')要么是(S),要么是更小的集合(S - {i})
状态数是(O(n2^{n})),如果我们按(S)逐一从小计算,计算当前(S)时,转移到的(S - {i})则可以直接算出
而如果转移到当前的(S),这个方程则有了后效性
直接高斯消元是(O(n^{3}2^{n}))的,我们考虑如hdu Maze那题一样解出式子
在集合(S)意义下【为了方便我们就省去S这维】,记(fa[i])为(i)的父节点,我们不妨设
[f[i] = A_if[fa[i]] + B_i
]
①若(i
otin S)
如果(i)为叶节点,那么(A_i = B_i = 1)
否则有
[egin{aligned}
f[i] &= frac{1}{de[i]}sumlimits_{(i,j) in E}(f[j][S] + 1) \
&= frac{1}{d[i]}f[fa[i]] + frac{1}{de[i]}sum_{i = fa[j]}(A_jf[i] + B_j + 1) \
&= frac{1}{d[i] - sum_{i = fa[j]}A_j}f[fa[i]] + frac{sum_{i = fa[j]}(B_j + 1) + 1}{d[i] - sum_{i = fa[j]}A_j}
end{aligned}
]
所以
[left{
egin{aligned}
A_i &= frac{1}{d[i] - sum_{i = fa[j]}A_j} \
B_i &= frac{sum_{i = fa[j]}(B_j + 1) + 1}{d[u] - sum_{i = fa[j]}A_j}
end{aligned}
ight.
]
可以由儿子递推
②若(i in S)
除非({i} = S),此时(A_i = B_i = 0)
否则(A_i = 0),(B_i = frac{1}{de[i]}sumlimits_{(i,j) in E}(f[j][S - {i}] + 1))
计算出所有(A_i)和(B_i)后回代可得到(f[i][S])
至此可以(O(n2^n))预处理所有(f[i][S])
然后做到(O(1))回答询问
根本不需要什么minmax容斥,(O(3^n))子集枚举
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 19,maxm = (1 << 19),INF = 1000000000,P = 998244353;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne = 1,de[maxn];
struct EDGE{int to,nxt;}ed[maxn << 1];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
ed[++ne] = (EDGE){u,h[v]}; h[v] = ne;
de[u]++; de[v]++;
}
inline int qpow(int a,int b){
int re = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
int in[maxn],f[maxn][maxm],nowS,n,Q,rt,maxv;
int A[maxn],B[maxn],fa[maxn];
void dfs(int u){
if (de[u] == 1 && u != rt){
if (in[u]){
A[u] = 0;
B[u] = (nowS ^ (1 << u - 1)) ? (f[fa[u]][nowS ^ (1 << u - 1)] + 1) % P : 0;
}
else A[u] = 1,B[u] = 1;
return;
}
if (in[u]){
A[u] = 0;
if (!(nowS ^ (1 << u - 1))) B[u] = 0;
else{
B[u] = 0;
int dv = qpow(de[u],P - 2),e = (nowS ^ (1 << u - 1));
Redge(u) B[u] = (B[u] + 1ll * dv * (f[to = ed[k].to][e] + 1) % P) % P;
}
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; dfs(to);
}
return;
}
int sa = 0,sb = 0;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; dfs(to);
sa = (sa + A[to]) % P;
sb = (sb + B[to] + 1) % P;
}
int d = qpow(((de[u] - sa) % P + P) % P,P - 2);
if (u == rt) A[u] = 0,B[u] = 1ll * sb * d % P;
else A[u] = d,B[u] = 1ll * (sb + 1) % P * d % P;
}
void dfs2(int u){
if (u == rt) f[u][nowS] = B[u];
else f[u][nowS] = (1ll * A[u] * f[fa[u]][nowS] % P + B[u]) % P;
Redge(u) if ((to = ed[k].to) != fa[u]) dfs2(to);
}
int main(){
n = read(); Q = read(); rt = read(); maxv = (1 << n) - 1;
for (int i = 1; i < n; i++) build(read(),read());
REP(i,n) f[i][0] = 0;
for (nowS = 1; nowS <= maxv; nowS++){
REP(i,n) in[i] = ((nowS & (1 << i - 1)) > 0);
dfs(rt); dfs2(rt);
}
while (Q--){
int k = read(),S = 0;
while (k--) S |= (1 << (read() - 1));
printf("%d
",f[rt][S]);
}
return 0;
}