[数据删除] 的树 [树形dp]

子树问题

在这里插入图片描述


\color{red}{正解部分}

F[i,j]F[i, j] 表示 ii 个节点, 深度不超过 jj 的构成树的方案数, 枚举根节点编号 次大 子树大小转移,

F[i,j]=k=1iF[k,j1]×F[ik,j]×(i2k1)F[i, j] = \sum\limits_{k=1}^{i} F[k, j-1] \times F[i-k, j] \times \begin{pmatrix} i-2 \\ k-1 \end{pmatrix}

\color{red}{实现部分}

#include<bits/stdc++.h>
#define reg register

int read(){
        char c;
        int s = 0, flag = 1;
        while((c=getchar()) && !isdigit(c))
                if(c == '-'){ flag = -1, c = getchar(); break ; }
        while(isdigit(c)) s = s*10 + c-'0', c = getchar();
        return s * flag;
}

const int maxn = 505;
const int mod = 998244353;

int N;
int K;
int C[maxn][maxn];
int F[maxn][maxn];

bool vis[maxn];

int main(){
        freopen("a.in", "r", stdin);
        N = read(), K = read();
        C[0][0] = 1;
        for(reg int i = 1; i <= N; i ++){
                C[i][0] = 1;
                for(reg int j = 1; j <= i; j ++) C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
        }
        for(reg int i = 1; i <= K; i ++) vis[read()] = 1;
        for(reg int i = 1; i <= N; i ++) F[1][i] = 1;
        if(!vis[N])
        for(reg int i = 2; i <= N; i ++)
                for(reg int j = 1; j <= N; j ++)
                        for(reg int k = 1; k <= i; k ++){
                                if(vis[k]) continue ;
                                int add = 1ll*F[k][j-1]*F[i-k][j]%mod*C[i-2][k-1]%mod;
                                F[i][j] = (F[i][j] + add) % mod;
                        }
        int L = read(), R = read();
        for(reg int i = L; i <= R; i ++) printf("%d\n", (F[N][i] - F[N][i-1] + mod) % mod);
        return 0;
}
posted @ 2019-11-02 16:24  XXX_Zbr  阅读(143)  评论(0编辑  收藏  举报