Codeforces Manthan Codefest 17 C 855C Helga Hufflepuff's Cup(树型DP)

37 篇文章 0 订阅
32 篇文章 0 订阅

想哭,没有眼泪流出.
总算把这题做出来了…
记录一下把,要让自己的博客有价值..
一棵树,每个节点属于1~m的种类之一,但是其中最多存在x个特殊种类,k.
与特殊节点相连的点,大小必须小于他.
用dp[i][j][k]来dp,i为当前节点号码,j为该点所属子树共有的特殊节点数量,k为当前节点的大小 0表示小于k,1表示等于k,2表示大于k.
dp的过程,就是!!!!(一条一条树枝)!!!,进行转移,转移过程中,要用一个temp数组来暂时存储一下.
不太懂啊不太懂
似乎树型dp和dfs的不同,就在于每一个点,都储存他子树的状态,也就是,要在回溯的过程中,做点什么

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
#define _ %MOD
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXM = 2e3+17;
const int MAXV = 2*1e3+17;
const int BIT = 15+3;
const int INF = 0x7fffffff;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int MAXN = 1e5+17;
LL dp[MAXN][10+3][3];
LL n,m,k,x;
vector<int > G[MAXN];
void dfs(int pre,int now)
{
    dp[now][0][0] = k-1;
    dp[now][1][1] = 1;
    dp[now][0][2] = m-k;
    for (int i = 0; i < G[now].size(); ++i)
    {
        int to = G[now][i];
        if(to!=pre)
        {
            dfs(now,to);
            LL temp[10+3][3]={0};
            for (int i = 0; i <= x; ++i)
            {
                for(int j = i;j >= 0;--j)
                {
                    temp[i][0] = (temp[i][0]+((dp[to][j][0]+dp[to][j][1]+dp[to][j][2])_*dp[now][i-j][0])_)_;
                    temp[i][1] = (temp[i][1]+(dp[to][j][0]*dp[now][i-j][1])_)_;
                    temp[i][2] = (temp[i][2]+((dp[to][j][0]+dp[to][j][2])*dp[now][i-j][2])_)_;
                }
            }
            for (int i = 0; i <= x; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    dp[now][i][j] = temp[i][j];
                }
            }
        }
    }
}

int main()
{   
    #ifdef GoodbyeMonkeyKing
    FFF
    #endif
    cin>>n>>m;
    for (int i = 0; i < n-1; ++i)
    {
        int u,v;
        cin>>u>>v;
        u--;v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    cin>>k>>x;
    dfs(0,0);
    LL ans = 0;
    for (int i = 0; i <= x; ++i)
    {
        ans = (ans+(dp[0][i][0] + dp[0][i][1] + dp[0][i][2]))_;
    }
    cout<<(ans)_<<endl;
    return 0;
}

// 1 1
// 3 3

// 1 3 * 1 3
// 3 1

// 1 2 1

// 2 1 1 
// 2 1 3
// 1 1 2
// 3 1 2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值