csuoj 1351: Tree Counting
这是一个动态规划的题;
当初想到要用dp,但是一直想不到状态转移的方程;
题解上的原话:
动态规划,设 g[i]表示总结点数为 i 的方案种数,另设 f[i][j]表示各个孩子的总结点数为
i,孩子的个数为 j 的方案数,那么有 g[i+1]=f[i][1]+f[i][2]+...+f[i][k],相当于添加一个根节
点之后变成完整的树,同时要把有 1 个孩子,2个孩子, ……,k 个孩子的情况都考虑进去。
对于 f[i][j]的求解可以用类似背包的方法去做,在求解的时候也会用到 g[1], g[2], ..., g[i]
的值,根据前面的那个 g[i+1]的表达式来看,这些 g[]已经在前面算出来了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 205
#define mod 1000000007
using namespace std; long long dp[maxn],f[maxn][]; int main()
{
int t,n,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
memset(dp,,sizeof dp);
memset(f,,sizeof f);
f[][]=;
dp[]=;
for(int i=;i<n;i++)
{
for(int j=;j<=k;j++)
for(int v=;i-v>=j-;v++)
f[i][j]=(f[i][j]+dp[v]*f[i-v][j-])%mod;
for(int j=;j<=k;j++)
dp[i+]=(dp[i+]+f[i][j])%mod;
}
printf("%lld\n",dp[n]);
}
return ;
}
csuoj 1351: Tree Counting的更多相关文章
- CSU 1351 Tree Counting
原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1351 DP题,毫无疑问.由于动态规划题目做得少.不熟悉,刚开始自己用f[i]表示用 i ...
- COJ 1351 Tree Counting 动态规划
题目大意是: 给定一个n,k,表示树上共有n个节点,每个节点最多有k个叶子,问一共多少种摆法,答案对1000000007取模 这里定义一个dp[i]表示 i 个节点对应有多少种方法 f[i][j] 表 ...
- 自由树的计数 Labeled unrooted tree counting
问题: 4个标记为1,2,3,4的节点构成自由树(算法导论里的定义,连接着,无环,无向的图),一共有多少种构造方法?如果N个节点呢? 解决方法: 4个节点可以通过穷举的方式得到答案,一共有16中方式. ...
- 常规DP专题练习
POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...
- [微软官网]One Windows Kernel
One Windows Kernel https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/One-Windows-Kerne ...
- DP 题集 1
关于 DP 的一些题目 参考资料 [Tutorial] Non-trivial DP Tricks and Techniques DP Rain and Umbrellas Mr. Kitayuta, ...
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- HDU4358 Boring counting【dsu on tree】
Boring counting Problem Description In this problem we consider a rooted tree with N vertices. The v ...
随机推荐
- 春&风
流光飞舞中,消逝的背影连同流动的人群,早已荒凉. 谁是谁的谁?落入尘世间,只是光阴下面的一粒尘埃. 那些时光,那些爱,渐行渐远. 留在心底,淡淡的思念,如轻风一阵, 吹过 彼此的容颜.
- 练习题之CyclicBarrier与CountDownLatch
一.CyclicBarrier使用 new Thread().start(); new Thread(new Runnable() {},"XXX").start(); publi ...
- spark下测试akka的分布式通讯功能
采用的spark版本为1.1.0 scala版本为2.10.4 编写scala类文件myactors.scala: package bluejoe import akka.actor._ import ...
- JNI的一些使用
1.简介 Java Native Interface(JNI) 有时候我们必须要调用本地代码c/c++来克服java中的内存管理和性能限制.java支持通过Java Native Interface( ...
- 第一次使用并配置Hibernate
1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...
- eclipse 好用的插件安装地址集合【持续更新】
1.PropertiesEditor (编辑properties文件的插件): http://propedit.sourceforge.jp/eclipse/updates/ 2.Aptana Stu ...
- MS SQLserver数据库安装
SQL2008的安装 1,双击打开setup安装文件 2,点击“全新安装或向现有安装添加功能” 3,安装程序支持规则,安装完之后,点击确定 4,输入产品的密钥,点击下一步 5,弹出“安装程序支持文件” ...
- 12天学好C语言——记录我的C语言学习之路(Day 1)
12天学好C语言--记录我的C语言学习之路 Day 1: 刚刚入门C语言,那么肯定要先把什么是C语言和大家讲清楚,那么大家看下面一段程序(program 1.1): /*//program 1.1 ...
- Poj 1017 / OpenJudge 1017 Packets/装箱问题
1.链接地址: http://poj.org/problem?id=1017 http://bailian.openjudge.cn/practice/1017 2.题目: 总时间限制: 1000ms ...
- syntax error near unexpected token `then'
#!/bin/bashclearfunction test{ if[$1 -eq "root"]&&[$2 -eq "123456"] ...