zoukankan      html  css  js  c++  java
  • codeforces9D How many trees?

    传送门:http://codeforces.com/problemset/problem/9/D

    【题解】

    树形dp,f(i,j)表示i个节点,高度为j的方案数,枚举左子树大小和哪一个子树高度为j-1即可。

    不加任何优化时间复杂度$O(n^4)$

     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef long long ll;
     5 const int M = 50;
     6 
     7 int n, h;
     8 ll f[M][M];
     9 
    10 int main() {
    11     cin >> n >> h;
    12     f[0][0] = 1;
    13     for (int i=1; i<=n; ++i)
    14         for (int j=1; j<=i; ++j) {
    15             for (int k=0; k<=i-1; ++k)
    16                 for (int l=0; l<=j-1; ++l)
    17                     f[i][j] += f[k][j-1]*f[i-k-1][l];
    18             for (int k=0; k<=i-1; ++k)
    19                 for (int l=0; l<=j-2; ++l)
    20                     f[i][j] += f[k][j-1]*f[i-k-1][l];
    21         }
    22     ll ans = 0;
    23     for (int i=h; i<=n; ++i) ans += f[n][i];
    24     cout << ans;
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    电感
    电容
    电阻
    函数异常规格说明
    异常处理深度解析
    自定义内存管理
    单例类模板
    数组类模板
    数组类模板
    类模板深度剖析
  • 原文地址:https://www.cnblogs.com/galaxies/p/codeforces9d.html
Copyright © 2011-2022 走看看