码迷,mamicode.com
首页 > 其他好文 > 详细

9D-树形DP

时间:2014-08-26 13:09:56      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   log   amp   

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

#include <iostream>
using namespace std;
long long int tbl[36][36];
long long int get(int n, int h) {
    long long int &ans = tbl[n][h];
    if (ans == -1) {
        ans = 0;
        if (n == 0) ans = 1;
        else if(h == 0)
        ans = 0;
        else 
        for(int j = 0; j <= n-1; ++j)
        ans += get(j, h-1) * get(n-1-j, h-1);
    }
    return ans;
}
int main(void){
    int n,h;
    cin >> n >> h;
    for (int i = 0; i <= n; ++i)
    fill_n(tbl[i], 36, -1);
    cout << get(n, n) - get(n, h-1) << endl;
    return 0;
}

 

9D-树形DP

标签:style   blog   color   os   io   for   div   log   amp   

原文地址:http://www.cnblogs.com/Susake/p/3936904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!