codeforces 9D How many trees? (組合二叉樹)
題目連結: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;
}
相關文章
- How To Efficiently Drop A Table With Many Extents
- coca How many 搭配 大寫
- 96-Unique Binary Search Trees 二叉搜尋樹的數量
- [doc]How To Efficiently Drop A Table With Many Extents
- HDU1213-How Many Tables
- 6.20-合併二叉樹二叉樹
- Oracle ASM How many allocation units per fileOracleASM
- Codeforces Round #362 (Div. 2) C 模擬二叉樹二叉樹
- HDU 1213 How Many Tables(並查集)並查集
- 滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹(二叉查詢樹)和最優二叉樹二叉樹
- LeetCode.617. 合併二叉樹LeetCode二叉樹
- LeetCode617. 合併二叉樹LeetCode二叉樹
- codeforces 341C Iahub and Permutations(組合數dp)
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- 動態規劃 hdu 1978 How many ways動態規劃
- 二叉樹 & 二叉查詢樹二叉樹
- Day20 | 654.最大二叉樹 、 617.合併二叉樹 、 700.二叉搜尋樹中的搜尋 98.驗證二叉搜尋樹二叉樹
- leetcode 617.合併二叉樹 JavaLeetCode二叉樹Java
- 排序二叉樹和平衡二叉樹排序二叉樹
- 二叉查詢樹(二叉排序樹)排序
- 二叉樹(順序儲存二叉樹,線索化二叉樹)二叉樹
- 迴歸樹(Regression Trees)模型的優缺點模型
- 手擼二叉樹——二叉查詢樹二叉樹
- 手擼二叉樹——AVL平衡二叉樹二叉樹
- 資料結構之樹結構概述(含滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹、紅黑樹、B-樹、B+樹、B*樹)資料結構二叉樹
- leetcode 每日一題 617 合併二叉樹LeetCode每日一題二叉樹
- 【Leetcode千題】617. 合併二叉樹LeetCode二叉樹
- 二叉樹二叉樹
- CodeForces571A. Lengthening Sticks(組合數學-容斥)
- 二叉樹的應用(1)--二叉樹排序樹基本操作二叉樹排序
- One more way regarding germany niubian how to remain difficult forREMAI
- HDU 3038 How Many Answers Are Wrong (帶權並查集)並查集
- 判斷二叉樹是否為滿二叉樹二叉樹
- 資料結構中的樹(二叉樹、二叉搜尋樹、AVL樹)資料結構二叉樹
- 自己動手作圖深入理解二叉樹、滿二叉樹及完全二叉樹二叉樹
- 二叉樹、B樹以及B+樹二叉樹
- 平衡二叉樹,B樹,B+樹二叉樹
- Codeforces 869C The Intriguing Obsession:組合數 or dpGUISession