传送门
Description
Solution
容斥,考虑有多少个节点不被匹配到,求出的方案,多个点可以同时不被匹配到
状态压缩+树形dp
Code
#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define reg register
#define int ll
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
struct edge{int to,nex;}e[540];int en,hr[20],Hr[20];
void ins(int x,int y,int *h)
{
e[++en]=(edge){y,h[x]};h[x]=en;
e[++en]=(edge){x,h[y]};h[y]=en;
}
int Siz[1<<17],now,ans,f[20][20],g[20];
void dfs(int x,int fa)
{
reg int i,j;
for(i=0;i<n;++i)
{
if(now>>i&1)f[x][i]=1;
else f[x][i]=0;
}
for(i=hr[x];i;i=e[i].nex)if(fa^e[i].to)dfs(e[i].to,x);
memset(g,0,sizeof(int[n]));
if(~fa)for(i=0;i<n;++i)if(now>>i&1)for(j=Hr[i];j;j=e[j].nex)if((now>>e[j].to)&1)g[e[j].to]+=f[x][i];
for(i=0;i<n;++i)f[fa][i]*=g[i];
}
signed main()
{
n=read();m=read();
reg int i,x,y;
for(i=1;i<=m;++i)
{
x=read()-1;y=read()-1;
ins(x,y,Hr);
}
for(i=1;i<n;++i) x=read()-1,y=read()-1,ins(x,y,hr);
int S=1<<n;
for(x=1;x<S;++x)
{
Siz[x]=Siz[(x-1)&x]+1;now=x;dfs(0,-1);
for(i=0;i<n;++i)if(x>>i&1)ans+=((n-Siz[x])&1?-1:1)*f[0][i];
}
printf("%lld
",ans);
return 0;
}
Blog来自PaperCloud,未经允许,请勿转载,TKS!