【51nod1806】—wangyurzee的树(purfer序列+容斥)

传送门


发现mm很小,显然可以考虑容斥
则不满足要求就可以变成强制为一个度数的方案
转化到树的purferpurfer序列
问题就变成了用nn个数去填n2n-2个格子,有些数指定出现次数求方案
假设限制了tt个数,分别为d1,d2,,,dtd_1,d_2,,,d_t
s=dis=\sum d_i
则方案数就是
ans=(n2s)nt(n2S)s!di!ans=(n-2-s)^{n-t}{n-2\choose S}\frac{s!}{\prod d_i!}
注意特判对同一个点多个限制和n=1n=1的情况

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
#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 pii pair<int,int>
#define fi first
#define se second
#define re register
#define pb push_back
inline void file(){
    #ifdef Stargazer
    freopen("lx.cpp","r",stdin);
    #endif
}
const int mod=1e9+7;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
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;
}
const int N=2000006,M=20;
int fac[N],ifac[N];
pii lim[M];
vector<int> p[M];
inline void Init(){
	fac[0]=ifac[0]=1;
	for(int i=1;i<N;i++)fac[i]=mul(fac[i-1],i);
	ifac[N-1]=ksm(fac[N-1],mod-2);
	for(int i=N-2;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
int n,m,tot,ans;
inline int C(int n,int m){
	if(n<m)return 0;
	return mul(fac[n],mul(ifac[m],ifac[n-m]));
}
inline void calc(int t,int prod,int s){
	int now=mul(ksm(n-t,n-2-s),mul(C(n-2,s),mul(fac[s],prod)));
	if(t&1)Dec(ans,now);
	else Add(ans,now);
}
void dfs(int pos,int siz,int prod,int sum){
	if(sum>n-2)return;
	if(pos==tot+1)return calc(siz,prod,sum);
	dfs(pos+1,siz,prod,sum);
	for(int i=0;i<p[pos].size();i++)
		dfs(pos+1,siz+1,mul(prod,ifac[p[pos][i]]),sum+p[pos][i]);
}
int main(){
	file();
	Init();
	n=read(),m=read();
	if(n==1){puts("1");return 0;}
	for(int i=1;i<=m;i++)lim[i].fi=read(),lim[i].se=read();
	sort(lim+1,lim+m+1);
	for(int i=1;i<=m;i++){
		if(lim[i].fi!=lim[i-1].fi)tot++;
		p[tot].pb(lim[i].se-1);
	}
	dfs(1,0,1,0);
	cout<<ans;
}
posted @ 2019-07-17 15:22  Stargazer_cykoi  阅读(121)  评论(0编辑  收藏  举报