洛谷传送门
BZOJ传送门
题解:
很显然我们将恰好包含所有 n − 1 n-1 n−1种转化为,最多包含 n − 1 n-1 n−1种-最多包含 n − 2 n-2 n−2种+最多包含 n − 3 n-3 n−3种 ⋯ \cdots ⋯。
然后矩阵树随便做吧。
太久没写矩阵树了,差点忘记行列式交换两行需要乘
−
1
-1
−1,虽然矩阵树根本不用考虑这个,因为我这种写法种需要交换两行的情况答案一定是0
代码:
#include<bits/stdc++.h>
#define ll long long
#define re register
#define gc get_char
#define cs const
namespace IO{
inline char get_char(){
static cs int Rlen=1<<22|1;
static char buf[Rlen],*p1,*p2;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>
inline T get(){
char c;
while(!isdigit(c=gc()));T num=c^48;
while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48);
return num;
}
inline int getint(){return get<int>();}
}
using namespace IO;
using std::cerr;
using std::cout;
#define pii std::pair<int,int>
#define fi first
#define se second
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){ll r=(ll)a*b;return r>=mod?r%mod:r;}
inline int power(int a,int b,int res=1){
for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));
return res;
}
inline int Inc(int &a,int b){return (a+=b)>=mod&&(a-=mod),a;}
inline int Dec(int &a,int b){return (a-=b)<0&&(a+=mod),a;}
cs int N=20;
int n;
int a[N][N],b[N][N];
inline int det(){
for(int re i=1;i<n;++i)
for(int re j=1;j<n;++j)
b[i][j]=a[i][j]<0?a[i][j]+mod:a[i][j];
int res=1;
for(int re i=1;i<n;++i){
int p=-1;
for(int re j=i;j<n&&p==-1;++j)b[j][i]&&(p=j);
if(p==-1)return 0;
if(p!=i){
for(int re j=i;j<n;++j)std::swap(b[i][j],b[p][j]);
res=dec(0,res);
}
res=mul(res,b[i][i]);
p=power(b[i][i],mod-2);
for(int re j=i;j<n;++j)b[i][j]=mul(b[i][j],p);
for(int re j=i+1;j<n;++j)if(b[j][i]){
int tmp=b[j][i];
for(int re k=i;k<n;++k)Dec(b[j][k],mul(tmp,b[i][k]));
}
}
return res;
}
std::vector<pii> vec[N];
int dfs(int cur,int coef){
if(cur==n)return mul(coef,det());
int tmp=dfs(cur+1,coef);
for(int re i=0,lim=vec[cur].size();i<vec[cur].size();++i){
pii &t=vec[cur][i];
++a[t.fi][t.fi];
++a[t.se][t.se];
--a[t.fi][t.se];
--a[t.se][t.fi];
}
Inc(tmp,dfs(cur+1,mod-coef));
for(int re i=0,lim=vec[cur].size();i<vec[cur].size();++i){
pii &t=vec[cur][i];
--a[t.fi][t.fi];
--a[t.se][t.se];
++a[t.fi][t.se];
++a[t.se][t.fi];
}
return tmp;
}
signed main(){
// freopen("dark.in","r",stdin);
scanf("%d",&n);
for(int re i=1;i<n;++i){
int m,u,v;scanf("%d",&m);
while(m--)scanf("%d%d",&u,&v),vec[i].push_back(pii(u,v));
}
cout<<dfs(1,(n&1)?1:(mod-1));
return 0;
}