【题解】codeforces 9DHow many trees?

本文探讨了如何使用动态规划解决二叉搜索树计数问题,即计算具有特定数量节点且高度超过给定阈值的不同二叉搜索树的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Examples
input
3 2
output
5
input
3 3
output
4
【翻译】
用n个点组成二叉树,问高度大于h的有多少个。(摘自LuoGu)
【题解】
这道题目似曾相识QwQ,好像是初赛里的问题求解
是用DP做的
dp[i][j]表示有i个节点,高度不大于j的二叉树个数
设k为左子树节点总数,则右子树节点数为i-k-1
得到dp[i][j]=sigma(dp[k][j-1]*dp[i-k-1][j-1])(i>k>=0)
因为输出的是大于h的树的总数,所以ans=dp[n][n]-dp[n][h-1]

Code:

#include <bits/stdc++.h>
using namespace std;
#define res register int
#define maxn 40
#define ll long long
int n,h;
ll dp[maxn][maxn];

inline int read(){
    int s=0,w=1;
    char c=getchar();
    while (c<'0' || c>'9'){if (c=='-')w=-1;c=getchar();}
    while (c>='0' && c<='9') s=s*10+c-'0',c=getchar();
    return s*w;
}

int main(){
    n=read(),h=read();
    for (res i=0;i<=n;++i) dp[0][i]=1;
    for (res i=1;i<=n;++i)
        for (res j=1;j<=n;++j)
            for (res k=0;k<j;++k) dp[j][i]+=dp[k][i-1]*dp[j-k-1][i-1];
    printf("%I64d\n",dp[n][n]-dp[n][h-1]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值