题意:有n个点和m条限制,每条限制限制了一个点的度数不能为某个数。

求合法的树的个数模10^9+7

n<=10^6

m<=17

思路:WYZ作业

首先m<=17显然是2^m容斥

枚举每一个限制有用或没用,考虑某一个约束情况下的方案数

Caylay定理:n个点的生成树的个数=n^(n-2)

Prufer序列:一个长度为n-2的Prufer序列对应唯一的一棵n个节点的树,度数为a[i]的点在其中出现了(a[i]-1)次

考虑先在序列中填上所有的受约束条件的点,它们的方案数是C(n-2,a[1]-1)*C(n-2-(a[1]-1),a[2]-1)*……

化简得:

(n-2)!/((a[1]-1)!(a[2]-1)!……*(n-2-sigma(a[i]-1))!)

考虑没有约束的点可以随意在剩下的序列中出现,它们的方案数为:

(n-p)^(n-2-sigma(a[i]-1))

其中p为选中的约束条件个数

将两部分相乘,根据p的奇偶性进行容斥计算即可

几个hack点:

1.对于同一个点可能会有多个约束条件,需要判断某一个点是否被多次限制,如果被多次限制就不能计入答案

2.n=1需要特判ans=1(真的吗,这个点可是坑了我5点头盾)

 const mo=;
var flag:array[..]of longint;
fac,exf:array[..]of int64;
a,b,c:array[..]of longint;
n,m,i:longint;
sun:int64; function mult(x,y:longint):int64;
var tmp:int64;
begin
tmp:=x; mult:=;
while y> do
begin
if y and = then mult:=mult*tmp mod mo;
tmp:=tmp*tmp mod mo;
y:=y>>;
end;
end; procedure dfs(k:longint);
var i,j,tot,sum:longint;
ans:int64;
begin
if k=m+ then
begin
tot:=; ans:=fac[n-]; sum:=n-;
for i:= to m do
if c[i]> then
begin
inc(tot);
inc(flag[a[i]]);
sum:=sum-(b[i]-);
end;
for i:= to m do
if (c[i]>)and(flag[a[i]]>) then
begin
for j:= to m do
if c[j]> then dec(flag[a[j]]);
exit;
end;
for i:= to m do
if c[i]> then ans:=ans*exf[b[i]-] mod mo;
//writeln(sum);
if sum>= then
begin
ans:=ans*exf[sum] mod mo;
ans:=ans*mult(n-tot,sum) mod mo;
if tot and = then sun:=(sun+ans) mod mo
else sun:=(sun-ans) mod mo;
sun:=(sun+mo) mod mo;
end;
for i:= to m do
if c[i]> then dec(flag[a[i]]);
exit;
end;
if b[k]= then dfs(k+)
else
begin
c[k]:=;
dfs(k+);
c[k]:=;
dfs(k+);
end;
end; begin
assign(input,'51nod1806.in'); reset(input);
assign(output,'51nod1806.out'); rewrite(output);
readln(n,m);
if n= then
begin
writeln();
exit;
end;
for i:= to m do read(a[i],b[i]);
exf[]:=; exf[]:=; fac[]:=;
for i:= to do fac[i]:=fac[i-]*i mod mo;
for i:= to do exf[i]:=exf[mo mod i]*(mo-mo div i) mod mo;
for i:= to do exf[i]:=exf[i-]*exf[i] mod mo;
dfs();
writeln(sun);
close(input);
close(output);
end.

【51NOD1806】wangyurzee的树(Prufer编码,容斥原理,组合计数)的更多相关文章

  1. 【Foreign】树 [prufer编码][DP]

    树 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 2 2 1 Sample Outp ...

  2. 「模拟赛20180406」膜树 prufer编码+概率

    题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...

  3. 树的Prufer 编码和最小生成树计数

      Prufer数列 Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2.它可以通过简单的迭代方 ...

  4. BSOJ 5553 wangyurzee的树 prufer序列 容斥

    BSOJ我也不知道在哪. 容易想到容斥. 考虑不合法的方案 想到强制某个点的度数为限制即可. 这样就变成了了总方案-一个不合法+两个不合法-3个......的模型了. 坑点 当强制两个相同的点时 方案 ...

  5. HDU 4390 Number Sequence (容斥原理+组合计数)

    HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...

  6. 【BZOJ 3027】 3027: [Ceoi2004]Sweet (容斥原理+组合计数)

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 34 Description John ...

  7. 51nod 1806 wangyurzee的树

    基准时间限制:1 秒 空间限制:131072 KB    wangyurzee有n个各不相同的节点,编号从1到n.wangyurzee想在它们之间连n-1条边,从而使它们成为一棵树.可是wangyur ...

  8. Luogu2290 [HNOI2004]树的计数 (组合计数,prufer编码)

    这不prufer编码吗,防爆long long就行了啊 #include <iostream> #include <cstdio> #include <cstring&g ...

  9. BZOJ1005--[HNOI2008]明明的烦恼(树的prufer编码)

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5768  Solved: 2253[Submit][Stat ...

  10. 树的prufer编码

    prufer是无根树的一种编码方式,一棵无根树和一个prufer编码唯一对应,也就是一棵树有唯一的prufer编码,而一个prufer编码对应一棵唯一的树. 第一部分:树编码成prufer序列. 树编 ...

随机推荐

  1. 移动端UI自动化Appium测试——Windows系统Appium环境配置

    1.安装JDK,官网下载即可,这里用的1.8,环境变量配置 2.安装Android sdk,API >= 17,环境变量配置 3.安装Nodejs,官网http://nodejs.org/dow ...

  2. android开发学习——Socket发送和接收

    client -- server发送过程中,涉及的输入流输出流: http://blog.csdn.net/dlwh_123/article/details/35982015   (良心好文)   需 ...

  3. 工作记录 主要了解EF 列注释DataAnnotations

    遇到一个问题,是子类型必须完全转换为父类型,普通的显示转化.隐式转化.Cast.ConvertAll等方法无效,用Newtonsoft.Json转化才解决 var json = JsonConvert ...

  4. "言官误国"

    "言官误国" 之前读<明朝那些事儿>的时候,了解到了一个全新的概念,确切的说与之前的印象完全不符合的概念:言官.之前我印象中的言官都是魏征那样的人,为国为民.冒死直谏等 ...

  5. openmv第一次调试

    2018-09-19  20:14:51 import sensor, image, time import car import json import time from pyb import U ...

  6. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  7. 键盘工具栏的快速集成--IQKeyboardManager

    转自:http://www.cnblogs.com/gaoxiaoniu/p/5333187.html 键盘工具栏的快速集成--IQKeyboardManager IQKeyboardManager, ...

  8. iTOP-4412开发板全新升级支持4G全网通模块

    开发板支持4G,GPS,CAN,485,WIFI蓝牙,重力加速度计,陀螺仪等模块. 核心板参数 尺寸:6cm*7cm 高度:连同连接器在内0.26cm CPU:Exynos4412,四核Cortex- ...

  9. JS Object 属性判断

    in 方法 var shapeInfo = {name:“lium”}; if (“name” in shapeInfo) {...}

  10. CAD参数绘制文字(com接口)

    在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...