【解题思路】

  引理:Prufer编码

定义:不断删除树中度数为1的最小序号的点,并输出与其相连的节点的序号,直至树中只有两个节点,所得输出序列即为Prufer编码。

性质:任意一棵n节点的树都可以用长为(n-2)的Prufer编码唯一表示;m度的节点在序列中出现次数为m-1。

  所以,我们只要计算合法的Prufer序列即可。

  设没有度数限制的节点共x个,有度数限制的节点集y,cnt=∑(d-1)(d∈y)。记range(n)=[1,n)∩N。

  对于没有度数限制的节点,共A=xn-2-cnt种相对方案;

  对于有度数限制的节点,共B=∏C(cnt-∑(dj-1)(j∈range(i)),di-1)(i∈range(n+1))种相对方案;

  根据乘法原理,ans=A*B*C(n-2,cnt)。时间复杂度O(n)。

【参考代码】

 import math

 def Ginput(prompt=""):
try:
return raw_input(prompt)
except:
return input(prompt) if __name__=="__main__":
n=int(Ginput())
cnt=S=0
pai=1
for i in range(n):
d=int(Ginput())
if d>-1:
cnt+=1
S+=d-1
pai*=math.factorial(d-1)
tmp=n-2-S
ans=math.factorial(n-2)/math.factorial(tmp)/pai*(n-cnt)**tmp
print(ans)

bzoj1005题解的更多相关文章

  1. bzoj1211树的计数 x bzoj1005明明的烦恼 题解(Prufer序列)

    1211: [HNOI2004]树的计数 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3432  Solved: 1295[Submit][Stat ...

  2. 【BZOJ1005】[HNOI2008]明明的烦恼(prufer序列)

    [BZOJ1005][HNOI2008]明明的烦恼(prufer序列) 题面 BZOJ 洛谷 题解 戳这里 #include<iostream> #include<cstdio> ...

  3. 【BZOJ1005/1211】[HNOI2008]明明的烦恼/[HNOI2004]树的计数 Prufer序列+高精度

    [BZOJ1005][HNOI2008]明明的烦恼 Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可 ...

  4. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  5. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  6. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  7. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  8. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  9. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

随机推荐

  1. matlab filtfilt 函数

    紧接上一篇,简单分析matlab中的非常好用的 filtfilt 函数,一款零相移滤波函数. 其matlab中的语法如下: y = filtfilt(data,x);1非常简单,不是一般的简单!然而, ...

  2. css负边距之详解(子绝父相)

    来源 | http://segmentfault.com 原文 |  The Definitive Guide to Using Negative Margins   自从1998年CSS2作为推荐以 ...

  3. hightchart 报错 Error in mounted hook: "Error: Highcharts error #17: www.highcharts.com/errors/17"

    这个错误是应该导入hightchart 使用的相关的 东西 ,这里是worldcloud import  Wordcloud    from  'highcharts/modules/wordclou ...

  4. elasticsearch启动问题

    ES安装完一直启动不了,问题解决. 报错: ERROR: bootstrap checks failed system call filters failed to install; check th ...

  5. vue element-ui NavMenu错位问题

    原因:子菜单全部打开后太长超过100% 解决方法:设置只能点击打开当前的菜单

  6. loj2573[TJOI2018]数字计算

    题意:操作1:x=x*m,输出x%mod.2.x/=map[m].m即第m次操作,保证该次操作为1操作,并且每个操作最多只会被删一次.q<=1e5. 线段树维护操作信息的乘积,删除把对应位置的权 ...

  7. C#中ArrayList 、Array与、string、string[]数组的相关转换

    一.ArrayList 与 string.string[]数组的转换 1.ArrayList 转换为 string[] : ArrayList list = new ArrayList(); list ...

  8. python网络爬虫学习

    网络爬虫 Requests官方中文教程地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html Beautiful So ...

  9. 选择排序-Python & Java

    选择排序:1.找出最小的数值放在第一位2.找出剩余数据中最小的数值放在第二位,以此类推,直到最后一个数值 算法的时间复杂度为:O(n) ''' 选择排序: 1.找出最小的数值放在第一位 2.找出剩余数 ...

  10. ajax请求是的动画实现

    ajax请求是的动画实现 ajax是基于XMLHttpRequest对象封装,ajax的具体属性就有: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type S ...