HDU 5629 Clarke and tree dp+prufer序列

Clarke and tree

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5629

Description

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a CS, did a research on data structure.
Now Clarke has n nodes, he knows the degree of each node no more than ai. He wants to know the number of ways to choose some nodes to compose to a tree of size s(1≤s≤n).

Input

The first line contains one integer T(1≤T≤10), the number of test cases.
For each test case:
The first line contains an integer n(2≤n≤50).
Then a new line follow with n numbers. The ith number ai(1≤ai<n) denotes the number that the degree of the ith node must no more than ai.

Output

For each test case, print a line with n integers. The ith number denotes the number of trees of size i modulo 109+7.

Sample Input

1
3
2 2 1

Sample Output

3 3 2

Hint:

At first we know the degree of node 1 can not more than 2, node 2 can not more than 2, node 3 can not more than 1. So
For the trees of size 1, we have tree ways to compose, are 1, 2 and 3. i.e. a tree with one node.
For the trees of size 2, we have tree ways to compose, are 1-2, 1-3, 2-3.
For the trees of size 3, we have two ways to compose, are 1-2-3, 2-1-3.

题意

给你n个点,每个点的度数最多为a[i]
然后分别问你点数为s的树,一共有多少种,(1<=s<=n)

题解:

考虑prufer的序列
对于点数为s,在这道题中z可以转化为这样:长度为s-2的序列里面,s个数都出现小于a[i]次的序列个数有多少个
我们直接n^4dp就好了
dp[i][j][k]表示考虑到了第i个数,我用了j,当前序列的长度为k的方案数

代码

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 62;
long long dp[maxn][maxn][maxn];
//考虑到了第i个点,现在出现了j个数,长度为k的方案数
long long c[maxn][maxn];
int a[maxn],n;
void init()
{
    for(int i=0;i<maxn;i++)
    {
        c[i][0]=c[i][i]=1;
        for(int j=1;j<i;j++)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }
}
int main()
{
    init();
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=i;j++)
            {
                for(int k=0;k<=n-2;k++)
                {
                    dp[i][j][k]=(dp[i][j][k]+dp[i-1][j][k])%mod;
                    for(int l=1;l<=a[i]&&l+k-1<=n-2;l++)
                        dp[i][j+1][k+l-1]=(dp[i][j+1][k+l-1]+c[k+l-1][l-1]*dp[i-1][j][k])%mod;
                }
            }
        }
        printf("%d",n);
        for(int i=2;i<=n;i++)
            printf(" %lld",dp[n][i][i-2]);
        printf("\n");
    }
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5196324.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞