codeforces 9D How many trees? (組合二叉樹)

bigbigship發表於2014-06-06

題目連結: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;
}


相關文章