HDU - 5370 Tree Maker dp

HDU - 5370

f[ i ] 表示顶点数为  i 的二叉树的种数, 其实就是卡特兰数。

dp[ i ][ j ] 表示 i 个顶点分给 j 个二叉树的方案数, 这个是个 n ^ 3 的dp。

然后根据提供的信息建出二叉树, 我们考虑如何计算, 对于在 u 的左儿子添加了 x 个点的二叉树,

我们进入 u 的左儿子, 计算出跟这个扩展有关的点, 和有几个地方还能接二叉树, 然后就用求出来的dp算方案数,

这个东西需要递归处理, 先处理完儿子再处理父亲。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 500 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int b) {
    int ans = 1;
    while(b) {
        if(b & 1) ans = 1LL * ans * a % mod;
        a = 1LL * a * a % mod; b >>= 1;
    }
    return ans;
}

int n, f[N];
int dp[N][N];
int pa[N];
int ch[N][2];
int addson[N][2];
int tot;
int sz[N];
int leaf[N];
int ans;

void dfs(int u) {
    sz[u] = 1;
    leaf[u] = 0;
    if(addson[u][0]) {
        if(ch[u][0]) {
            dfs(ch[u][0]);
            ans = 1LL * ans * dp[addson[u][0] - sz[ch[u][0]]][leaf[ch[u][0]]] % mod;
        }
        else {
            ans = 1LL * ans * dp[addson[u][0]][1] % mod;
        }
    }
    else {
        if(ch[u][0]) {
            dfs(ch[u][0]);
            sz[u] += sz[ch[u][0]];
            leaf[u] += leaf[ch[u][0]];
        }
        else {
            leaf[u]++;
        }
    }

    if(addson[u][1]) {
        if(ch[u][1]) {
            dfs(ch[u][1]);
            ans = 1LL * ans * dp[addson[u][1] - sz[ch[u][1]]][leaf[ch[u][1]]] % mod;
        }
        else {
            ans = 1LL * ans * dp[addson[u][1]][1] % mod;
        }
    }
    else {
        if(ch[u][1]) {
            dfs(ch[u][1]);
            sz[u] += sz[ch[u][1]];
            leaf[u] += leaf[ch[u][1]];
        }
        else {
            leaf[u]++;
        }
    }
}

void init() {
    tot = ans = 1;
    memset(pa, 0, sizeof(pa));
    memset(addson, 0, sizeof(addson));
    memset(ch, 0, sizeof(ch));
    memset(sz, 0, sizeof(sz));
    memset(leaf, 0, sizeof(leaf));
}

int main() {
    f[0] = 1;
    for(int i = 1; i < N; i++) {
        f[i] = 1LL * f[i - 1] * (4 * i - 2) % mod * power(i + 1, mod - 2) % mod;
    }
    for(int i = 0; i < N; i++) {
        dp[i][1] = f[i];
        dp[0][i] = 1;
    }

    for(int i = 1; i < N; i++) {
        for(int j = 2; j < N; j++) {
            for(int k = 0; k <= i; k++) {
                add(dp[i][j], 1LL * dp[i - k][j - 1] * f[k] % mod);
            }
        }
    }

    int cas = 1;
    while(scanf("%d", &n) != EOF) {
        init();
        int cur = 1;
        for(int o = 1; o <= n; o++) {
            int op, x; scanf("%d", &op);
            if(op == 3 || op == 4) scanf("%d", &x);
            if(op == 0) {
                cur = pa[cur];
            }
            else if(op == 1) {
                if(!ch[cur][0]) {
                    ch[cur][0] = ++tot;
                    while(tot >= 502);
                    pa[tot] = cur;
                }
                cur = ch[cur][0];
            }
            else if(op == 2) {
                if(!ch[cur][1]) {
                    ch[cur][1] = ++tot;
                    pa[tot] = cur;
                }
                cur = ch[cur][1];
            }
            else if(op == 3) {
                addson[cur][0] = x;
            }
            else {
                addson[cur][1] = x;
            }
        }
        dfs(1);
        printf("Case #%d: %d\n", cas++, ans);
    }
    return 0;
}

/*
*/

 

posted @ 2019-07-16 20:20  NotNight  阅读(95)  评论(0编辑  收藏  举报