51nodwangyurzee的树

链接:http://www.51nod.com/contest/problem.html#!problemId=1806

题意:中文题。

分析:一个度数为d的点会在prufer序列中出现d-1次,而每一个prufer序列都对应一棵树,那么我们就可以直接用容斥原理+组合数学+prufer序列解决掉这题了。注意prufer序列长度为n-2,注意n=1的情况。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=2000010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int n,q[20],u[20],d[20];
ll mul[N],inv[N],now[N];
void deal(int n) {
    mul[0]=now[0]=1;
    mul[1]=inv[1]=now[1]=1;
    for (int i=2;i<=n;i++) {
        mul[i]=mul[i-1]*i%MOD;
        inv[i]=(MOD-MOD/i)*inv[MOD%i]%MOD;
        now[i]=now[i-1]*inv[i]%MOD;
    }
}
ll C(ll n,ll m) {
    if (n<m) return 0;
    return mul[n]*now[m]%MOD*now[n-m]%MOD;
}
ll qpow(ll a,ll b) {
    ll ret=1;
    while (b) {
        if (b&1) ret=ret*a%MOD;
        a=a*a%MOD;b>>=1;
    }
    return ret;
}
int main()
{
    int i,j,g,m,bo,sum;
    ll ans=0,tot;
    scanf("%d%d", &n, &m);
    for (i=0;i<m;i++) scanf("%d%d", &u[i], &d[i]);
    deal(n);
    for (i=0;i<(1<<m);i++) {
        g=0;sum=0;bo=1;tot=1;
        memset(q,0,sizeof(q));
        for (j=0;j<m;j++)
        if (i&(1<<j)) {
            if (q[u[j]]) { bo=0;break ; }
            tot=tot*C(n-2-sum,d[j]-1)%MOD;
            g++;q[u[j]]=1;sum+=d[j]-1;
        }
        if (!bo||sum>n-2) continue ;
        tot=tot*qpow(n-g,n-2-sum)%MOD;
        if (g&1) ans=(ans-tot)%MOD;
        else ans=(ans+tot)%MOD;
    }
    if (n==1&&m==0) printf("1\n");
    else printf("%I64d\n", (ans+MOD)%MOD);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值