【ZJOI2016】小星星

限制与约定

1$=10$$\leq \frac {n(n-1)} {2}$
2
3$=17$保证新的饰品中每颗小星星与至多两颗小星星相连
4
5$=14$
6
7$=16$保证答案不超过 $10^5$
8
9$=17$
10

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define LL long long

using namespace std;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

char c=nc();int b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

char c=nc();LL b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

{
char c=nc();int len=0;
for(;!(c>='A' && c<='Z');c=nc()) if (c==EOF) return 0;
for(;(c>='A' && c<='Z');s[len++]=c,c=nc());
s[len++]='\0';
return len;
}

for (x=nc();!(x>='A' && x<='Z');x=nc());
}

int wt,ss[19];
inline void print(int x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {
for (wt=0;x;ss[++wt]=x%10,x/=10);
for (;wt;putchar(ss[wt]+48),wt--);}
}
inline void print(LL x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}
}

int n,m,a[20][20],b[20],fa[20];
LL ans,f[20][20],p[20];
vector<int> c[20],d[20];

void dp(int x)
{
for (int i=1;i<=n;i++)
if (b[i]) f[x][i]=1LL;
for (int i=0;i<d[x].size();i++)
dp(d[x][i]);
if (x==1) return ;
for (int j=1;j<=n;j++)
p[j]=0;
for (int j=1;j<n;j++)
for (int k=j+1;k<=n;k++)
if (a[j][k] && b[j] && b[k]) p[j]+=f[x][k],p[k]+=f[x][j];
for (int j=1;j<=n;j++)
f[fa[x]][j]*=p[j];
}

LL calc()
{
memset(f,0,sizeof(f));
dp(1);
LL res=0;
for (int i=1;i<=n;i++)
res+=f[1][i];
return res;
}

void work(int x)
{
if (x==n+1)
{
int s=0;LL f;
for (int i=1;i<=n;i++)
s+=b[i];
if (s%2==n%2) f=1LL;else f=-1LL;
ans+=f*calc();
return ;
}
for (int i=0;i<=1;i++)
b[x]=i,work(x+1);
}

void dfs(int x)
{
for (int i=0;i<c[x].size();i++)
if (fa[c[x][i]]==-1)
{
fa[c[x][i]]=x;
d[x].push_back(c[x][i]);
dfs(c[x][i]);
}
}

int main()
{
int x,y;
for(int i=1;i<=m;i++)
memset(b,0,sizeof(b));
for (int i=1;i<n;i++)