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 ≤ 35h ≤ n).

Output

Output one number — the answer to the problem. It is guaranteed that it does not exceed 9· 1018.

  题意: 给定n个点(1~n)使他们构成二叉树  求高度大于h 的有多少种;

   我们开一个数组 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;}


0 0
原创粉丝点击