codeforces 9D How many trees? (组合二叉树)
来源:互联网 发布:网络最火的直播软件 编辑:程序博客网 时间:2024/06/06 01:31
题目链接:http://vjudge.net/contest/view.action?cid=47681#problem/D
Description
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...
For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.
However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus — Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.
This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with n nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to n. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).
In Hexadecimal's game all the trees are different, but the height of each is not lower than h. In this problem «height» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?
Input
The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n).
Output
Output one number — the answer to the problem. It is guaranteed that it does not exceed 9· 1018.
我们开一个数组 DP[36][36];
设DP[n][h] 表示有n 个节点构成 高度为 h的二叉树的数量;
这颗树的根节点可以使1~n中的任意数值 我们假设根节点为 m ,
则左子树有m-1个点 右子树有 n-m个点 ,要构成高度为h的树 可以分成以下两种情况来考虑;
1)左子树的高度为h-1 右子树的高度为0~h-1的任意高度;
left_count=dp[m-1][h-1]*sum(dp[n-m][i]) (i<=0<=h-1);
2)右子树的高度为h-1 左子树的高度小于h-1;
right_count=dp[n-m][h-1]*sum(dp[m-1][i]) (i<=0<=h-2);
dp[n][h]=left_count+right_count;
代码如下:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef __int64 lld;const int N =36;lld dp[N][N];void init(){ lld right_sum,left_sum; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int n=1;n<N;n++){ for(int h=1;h<N;h++){//枚举高度 for(int m=1;m<=n;m++){//枚举根节点 left_sum = right_sum = 0; for(int i=0;i<h;i++) right_sum+=dp[n-m][i]; dp[n][h]+=dp[m-1][h-1]*right_sum; for(int i=0;i<h-1;i++) left_sum+=dp[m-1][i]; dp[n][h]+=dp[n-m][h-1]*left_sum; } } }}int main(){ int n,h; init(); while(cin>>n>>h){ /* for(int i=0;i<N;i++){ for(int j=0;j<N;j++) cout<<dp[i][j]<<" "; cout<<endl; } cout<<"****"<<endl;*/ lld sum=0; for(int i=h;i<=n;i++) sum+=dp[n][i]; cout<<sum<<endl; } return 0;}
下面是别人写的:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef __int64 lld;const int N =36;lld dp[N][N];void init(){ for(int i=0;i<N;i++) dp[0][i]=1; for(int i=1;i<N;i++){ for(int j=1;j<N;j++){ for(int k=0;k<i;k++) dp[i][j]+=dp[k][j-1]*dp[i-k-1][j-1]; } }}int main(){ int n,h; init(); while(cin>>n>>h){ cout<<dp[n][35]-dp[n][h-1]<<endl; } return 0;}
- codeforces 9D How many trees? (组合二叉树)
- CodeForces 9D How many trees?
- Codeforces 9D How many trees? 题解
- CodeForces 9 D.How many trees?(dp)
- Codeforces Beta Round #9 (Div. 2 Only), problem: (D) How many trees? DP
- codeforces 9D How many trees?(DP,注意状态表示方法)
- Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp
- (卡特兰数,二叉树数量)How Many Trees? &&Count the Trees -- HDOJ
- hdu 4359 codeforces 9D 组合 二叉树 DP
- #9 (Div. 2 Only) D. How many trees? (dp)(好题)
- hdu 1130 How Many Trees?(二叉树节点的Catalan数)
- UVa10303 - How Many Trees?
- Codeforces_9D-How many trees?
- How Many Trees?
- hdu1130-How Many Trees?
- How Many Trees?
- How Many Trees?
- How Many Trees?
- Android 播放声音
- 智能生活 “视”不可挡——首届TCL杯HTML5智能电视开发大赛等你来挑战
- OpenGL像素操作
- android service生命周期
- Interleaving String
- codeforces 9D How many trees? (组合二叉树)
- 2
- 防信息(电磁)泄漏计算机
- linux常用端口
- 斯坦福机器学习公开课学习笔记(2)—监督学习 梯度下降
- 设计模式-----策略模式(strategy)
- GitHub详细教程
- 使用友元函数计算两点之间的距离
- maven 报Missing com.sun:tools:jar:1.X.X 最终解决方法