令 f ( s ) f(s) f(s)表示状态为 s s s的人口之和, g ( s ) g(s) g(s)为状态 s s s的所有州的划分的满意度之和
则
g
(
s
)
=
∑
i
⊆
s
f
(
i
)
f
(
s
)
g
(
s
⨁
i
)
g(s)=\sum_{i\subseteq s}\frac{f(i)}{f(s)}g(s\bigoplus i)
g(s)=∑i⊆sf(s)f(i)g(s⨁i)
=
1
f
(
s
)
∑
i
⊆
s
f
(
i
)
g
(
s
⨁
i
)
\ \ \ \ \ \ \ \ =\frac{1}{f(s)}\sum_{i\subseteq s}f(i)g(s\bigoplus i)
=f(s)1∑i⊆sf(i)g(s⨁i)
后面一坨是一个子集卷积的形式
直接枚举子集是
3
n
3^n
3n无法接受
用子集卷积做到
O
(
n
2
2
n
)
O(n^22^n)
O(n22n)
欧拉回路用并查集判一下就可以了
#include<bits/stdc++.h>
using namespace std;
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define re register
#define pb push_back
#define cs const
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?(a+=mod):0;}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){
for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
}
inline void chemx(ll &a,ll b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=22,C=(1<<N)+5,M=N*N;
int n,m,p;
struct edge{
int u,v;
}e[M];
inline void FMT(int *f,int n,int kd){
for(int i=0;i<n;i++)
for(int j=0;j<(1<<n);j++)
if(j&(1<<i))(kd==1)?(Add(f[j],f[j^(1<<i)])):(Dec(f[j],f[j^(1<<i)]));
}
int fa[N],in[N];
inline int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
inline bool check(int i){
int root=0;
for(int j=1;j<=n;j++)
if(i&(1<<(j-1))){
if(in[j]&1)return true;
else{
int rt=find(j);
if(!root)root=rt;
if(root!=rt)return true;
}
}
return false;
}
int f[N][C],g[N][C],w[N],s[C],cnt[C],inv[C];
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
n=read(),m=read(),p=read();
for(int i=1;i<=m;i++)
e[i].u=read(),e[i].v=read();
for(int i=1;i<=n;i++)w[i]=read();
int sta=1<<n;
for(int i=1;i<sta;i++)
for(int j=1;j<=n;j++)
if(i&(1<<(j-1)))s[i]+=w[j],cnt[i]++;
for(int i=0;i<sta;i++){
for(int j=1;j<=n;j++)in[j]=0,fa[j]=j;
for(int j=1;j<=m;j++){
int u=e[j].u,v=e[j].v;
if((i&(1<<(u-1)))&&(i&(1<<(v-1)))){
in[u]++,in[v]++,fa[find(u)]=find(v);
}
}
s[i]=ksm(s[i],p);
if(check(i))f[cnt[i]][i]=s[i];
inv[i]=ksm(s[i],mod-2);
}
for(int i=0;i<=n;i++)FMT(f[i],n,1);
g[0][0]=1;
FMT(g[0],n,1);
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++)
for(int k=0;k<sta;k++)
Add(g[i][k],mul(g[j][k],f[i-j][k]));
FMT(g[i],n,-1);
for(int k=0;k<sta;k++)
if(cnt[k]!=i)g[i][k]=0;
else g[i][k]=mul(g[i][k],inv[k]);
FMT(g[i],n,1);
}
FMT(g[n],n,-1);
cout<<g[n][sta-1];
}