【Foreign】树 [prufer编码][DP]
树
Time Limit: 10 Sec Memory Limit: 256 MB
Description

Input

Output

Sample Input
3
2 2 1
Sample Output
HINT


Solution
由于是带标号的无根树的计数,于是我们运用prufer编码的性质来解题。
prufer编码的几个性质:
1.对于大小为s的树,prufer编码是一个长度为 s-2 的序列;
2.i在序列中出现的次数<deg[i];
3.一个prufer编码表示一棵树。
所以这题可以转化为求prufer编码的计数。
我们令f[i][j][k]表示前i个点,选择了j个,prufer编码长度为k的方案数。那么显然有
其中 f[i-1][j][k] 表示不选择该点的方案数,后面的式子表示选择了该点的方案数,选择该点可以在编码中出现0-deg[i]-1次,然后在编码中的出现顺序可以任意所以要乘上C。
最后如果i=1显然输出n,否则由于prufer编码是长度i-2的序列,所以输出f[n][i][i-2]。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cmath>
using namespace std;
typedef long long s64;
const int ONE=;
const int MOD=; int n;
int deg[ONE];
int C[ONE][ONE];
int f[ONE][ONE][ONE]; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} void Mod(int &a)
{
if(a>MOD) a-=MOD;
} int main()
{
n=get();
for(int i=;i<=n;i++) deg[i]=get(); C[][]=;
for(int i=;i<=n;i++)
{
C[i][]=;
for(int j=;j<=n;j++)
C[i][j] = (C[i-][j] + C[i-][j-]) % MOD;
} f[][][]=;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
for(int k=;k<=n;k++)
{
f[i][j][k] += f[i-][j][k]; Mod(f[i][j][k]);
if(!j) continue;
for(int l=; l < deg[i] && l <= k ;l++)
{
f[i][j][k] += (s64)f[i-][j-][k-l] * C[k][l] % MOD;
Mod(f[i][j][k]);
}
} for(int i=;i<=n;i++)
{
if(i==) printf("%d ",n);
else printf("%d ",f[n][i][i-]);
}
}
【Foreign】树 [prufer编码][DP]的更多相关文章
- 「模拟赛20180406」膜树 prufer编码+概率
题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...
- BSOJ 5445 -- 【2018雅礼】树 prufer序列 dp
BSOJ在哪我也不知道 没有链接. 对于有标号无根树的统计和有度数限制 一般采用prufer序列. 根据prufer序列 容易知道 某个点的出现次数+1为当前点的度数. 对于这道题 考虑设f[i][j ...
- 树的prufer编码
prufer是无根树的一种编码方式,一棵无根树和一个prufer编码唯一对应,也就是一棵树有唯一的prufer编码,而一个prufer编码对应一棵唯一的树. 第一部分:树编码成prufer序列. 树编 ...
- 【转】ACM 2567 -- 树的Prufer编码
本文介绍北京大学ACM网站2567号题目的解法.介绍部分基本翻译自网站上的题目介绍. 题目介绍: 给定一棵各节点编号为整数1,2,3...n的树(例如,无环连通图),其Prufer编码(Pruf ...
- BZOJ1005--[HNOI2008]明明的烦恼(树的prufer编码)
1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5768 Solved: 2253[Submit][Stat ...
- Luogu2290 [HNOI2004]树的计数 (组合计数,prufer编码)
这不prufer编码吗,防爆long long就行了啊 #include <iostream> #include <cstdio> #include <cstring&g ...
- 【转】prufer编码
既然有人提到了,就顺便学习一下吧,来源:http://greatkongxin.blog.163.com/blog/static/170097125201172483025666/ 一个含有n个点的完 ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 排列组合,Prufer编码
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1109D.html 题意 所有边权都是 [1,m] 中的整数的所有 n 个点的树中,点 a 到点 b 的距离 ...
- prufer编码
看51nod的一场比赛,发现不会大家都A的一道题,有关prufer的 我去年4月就埋下prufer这个坑,一直没解决 prufer编码是什么 对于一棵无根树的生成的序列,prufer序列可以和无根树一 ...
随机推荐
- C++学习007-使用exit退出进程
使用exit可以实现退出当前进程. 如下 在程序接收到一个字符后,就退出进程 编写环境 vs2015 int main() { int a = 10, b = 20; std::cout <&l ...
- Wordpress 设置后台自定义post 排序
创建新的 Post type时,文章在后台默认使用 Titile 列进行升序排序,但是通常情况下我们需要按日期 Date 进行降序排序, function wpse_81939_post_types_ ...
- HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...
- 使用PNotify构建消息弹窗
参考地址 官网:http://sciactive.com/pnotify/ GitHub:https://github.com/sciactive/pnotify npm仓库:https://www. ...
- 【iOS开发】动态添加子视图 UIView 的正确方法
很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...
- linux服务器su之后变成bash-4.1#
当前为root权限 cd /home/jboss 执行如下命令,将缺失的配置文件拷贝到指定位置即可 cp ./.bashrc /root cp ./.bash_profile /root 然后切换账号 ...
- 详细介绍弹性盒模型(display:flex)
弹性盒模型,即Flexbox,是css3中的新特性,其实弹性盒模型的原身是dispaly:box:这里,我们暂时不考虑旧的,我们只看新的. 为容器指定弹性盒子,只需在父元素(也就是容器)中设置:dis ...
- JAXB使用方式
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...
- hdu 1575 Tr A (二分矩阵)
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- [Leetcode] Unique binary search trees ii 唯一二叉搜索树
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...