PKUWC2018 随机游走
又一道咕了很久的神仙题
题目链接
看了好久题解才会做
\(Min-Max\)容斥
见我的这篇文章
解析
声明:\(u\)代表当前节点,\(v\)代表\(u\)的子节点,\(fa\)代表\(u\)的父亲节点,\(d[u]\)代表\(u\)的度数.
我们枚举一个\(S\),然后算\(Min(S)\),然后就可以用\(Min-Max\)容斥算\(Max(S)\)了.
首先,我们列出期望的转移方程\(f[u]=\frac{1}{d[u]}(f[fa]+1+\sum(f[v]+1))\)
当\(u\in S\)时,\(f[u]\)显然为\(0\).
然后,一个点的期望\(f[u]\)可以写成\(kf[fa]+b\)的形式.
那么,刚才那个方程可以写成\(f[u]=\frac{1}{d[u]}(f[fa]+1+\sum(k_vf[v]+b_v+1))\)
因为总共的\(1\)的数量等于\(d[u]\),我们把\(1\)拉到外面来得
\(f[u]=\frac{1}{d[u]}(f[fa]+\sum(k_vf[u]+b_v))+1\)
把\(\sum\)拆掉得\(f[u]=\frac{1}{d[u]}f[fa]+\frac{f[u]*\sum k}{d[u]}+\frac{\sum b}{d[u]}+1\)
两边乘\(d[u]\)得(终于不用打该死的\(frac\)了)
\(d[u]f[u]=f[fa]+f[u]*\sum k+\sum b+d[u]\)
简单移个项得\((d[u]-\sum k)f[u]=f[fa]+\sum b+d[u]\)
两边同时除以\((d[u]-\sum k)\)得
\(f[u]=\frac{1}{(d[u]-\sum k)}f[fa]+\frac{\sum b+d[u]}{(d[u]-\sum k)}\)
那么,\(k_u=\frac{1}{(d[u]-\sum k)},k_b=\frac{\sum b+d[u]}{(d[u]-\sum k)}\),然后我们向上做就好了.
求完这个\(Min(S)\)之后,我们直接\(FWT/FMT\)即可.
代码如下
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (21)
#define M (101)
#define P (998244353)
#define SI (1<<N)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
inline char read(){
static const int IN_LEN=1000000;
static char buf[IN_LEN],*s,*t;
return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
static bool iosig;
static char c;
for(iosig=false,c=read();!isdigit(c);c=read()){
if(c=='-')iosig=true;
if(c==-1)return;
}
for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
if(iosig)x=-x;
}
inline char readchar(){
static char c;
for(c=read();!isalpha(c);c=read())
if(c==-1)return 0;
return c;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
*ooh++=c;
}
template<class T>
inline void print(T x){
static int buf[30],cnt;
if(x==0)print('0');
else{
if(x<0)print('-'),x=-x;
for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
while(cnt)print((char)buf[cnt--]);
}
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
struct fc{
LL k,b;
fc operator +(const fc a)const{
return {(k+a.k)%P,(b+a.b)%P};
}
fc operator *(const LL x)const{
return {k*x%P,b*x%P};
}
};
LL ksm(LL a,int p){
LL res=1;
while(p){
if(p&1)res=(res*a)%P;
a=(a*a)%P,p>>=1;
}
return res;
}
int n,fi[N],ne[M],b[M],Q,rt,E,ru[N],Lim,pc[SI]; LL f[1<<N],inv[N];
void add(int x,int y){ne[++E]=fi[x],fi[x]=E,b[E]=y,ru[y]++;}
fc dfs(int u,int pre,int S){
if(S&(1<<u-1))return {0,0};
fc ans={0,0};
for(int i=fi[u];i;i=ne[i]){
int v=b[i];
if(v==pre)continue;
ans=ans+dfs(v,u,S);
}
LL iv=ksm((ru[u]-ans.k+P)%P,P-2);
return {iv,iv*(ans.b+ru[u])%P};
}
int main(){
read(n),read(Q),read(rt),Lim=(1<<n),inv[0]=1;
for(int i=1;i<=n;i++)inv[i]=ksm(i,P-2)%P;
for(int i=1,x,y;i<n;i++)
read(x),read(y),add(x,y),add(y,x);
for(int i=1;i<Lim;i++)pc[i]=pc[i>>1]+(i&1);
for(int i=1;i<Lim;i++){
fc ans=dfs(rt,0,i);
f[i]=ans.b; if(!(pc[i]&1))f[i]=P-f[i];
}
for(int i=1;i<Lim;i<<=1)
for(int R=i<<1,j=0;j<Lim;j+=R)
for(int k=j;k<j+i;k++)(f[i+k]+=f[k])%=P;
while(Q--){
int cnt,S=0;read(cnt);
for(int i=1,x;i<=cnt;i++)
read(x),S|=(1<<x-1);
printf("%lld\n",(f[S]+P)%P);
}
}