codeforces 855C Helga Hufflepuff's Cup (树形dp)

Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank.

The Wizarding bank is in the form of a tree with total n vaults where each vault has some type, denoted by a number between 1 to m. A tree is an undirected connected graph with no cycles.

The vaults with the highest security are of type k, and all vaults of type k have the highest security.

There can be at most x vaults of highest security.

Also, if a vault is of the highest security, its adjacent vaults are guaranteed to not be of the highest security and their type is guaranteed to be less than k.

Harry wants to consider every possibility so that he can easily find the best path to reach Bellatrix's vault. So, you have to tell him, given the tree structure of Gringotts, the number of possible ways of giving each vault a type such that the above conditions hold.

Input

The first line of input contains two space separated integers, n and m — the number of vaults and the number of different vault types possible. (1 ≤ n ≤ 105, 1 ≤ m ≤ 109).

Each of the next n - 1 lines contain two space separated integers ui and vi (1 ≤ ui, vi ≤ n) representing the i-th edge, which shows there is a path between the two vaultsui and vi. It is guaranteed that the given graph is a tree.

The last line of input contains two integers k and x (1 ≤ k ≤ m, 1 ≤ x ≤ 10), the type of the highest security vault and the maximum possible number of vaults of highest security.

Output

Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109 + 7.

Example
Input
4 2
1 2
2 3
1 4
1 2
Output
1
Input
3 3
1 2
1 3
2 1
Output
13
Input
3 1
1 2
1 3
1 1
Output
0
Note

In test case 1, we cannot have any vault of the highest security as its type is 1implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2.



树形dp

简略题意:给出一颗树,有m种颜色,第k种颜色是特殊颜色,树上最多有x个特殊颜色点。 
你需要把整个树染色,且保证特殊颜色节点以下条件: 
1. 与其相连的不能有特殊颜色节点。 
2. 与其相连的节点的颜色序号必须小于k。 
问有多少种满足要求的树。


考虑三种情况 小于k ==k 大于k 然后用其中一维为出现次数,暴力更新

所以就是 dp[i][j][3] i为树上的当前节点,j为下面包含j种颜色,然后就可以进行更新,每次更新的时候 一个根的单个树枝是可以更新到根上的,代表所以同一根节点下,遍历过的树枝和根出现过的总的特殊点的次数,然后不断滚动更新就行。


#include <bits/stdc++.h>
using namespace std;
vector<int> e[100010];
typedef long long ll;
ll dp[100010][12][3];
int k,ci;
int n,m;
const int mod = 1e9+7;

void dfs(int x,int y)
{
    dp[x][0][0]=k-1;dp[x][1][1]=1,dp[x][0][2]=m-k;
    for(int i=0;i<e[x].size();i++)
    {
        if(e[x][i]==y) continue;
        dfs(e[x][i],x);
        ll buf[12][3]={0};
        int son=e[x][i];
        for(int j=0;j<=ci;j++)
            for(int kk=0;kk<=j;kk++)
            {
                buf[j][0]=buf[j][0]+((dp[son][kk][0]+dp[son][kk][1]+dp[son][kk][2])%mod)*dp[x][j-kk][0]%mod;
                buf[j][0]%=mod;

                buf[j][1]=buf[j][1]+dp[son][kk][0]*dp[x][j-kk][1];
                buf[j][1]%=mod;

                buf[j][2]=buf[j][2]+(dp[son][kk][0]+dp[son][kk][2])*dp[x][j-kk][2];
                buf[j][2]%=mod;

            }
        memcpy(dp[x],buf,sizeof(buf));
    }
}


int main()
{
    scanf("%d%d",&n,&m);
    int u,v;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    scanf("%d%d",&k,&ci);
    dfs(1,0);
    ll sum=0;
    for(int i=0;i<=ci;i++)
    {
        for(int j=0;j<3;j++)
        {
            sum+=dp[1][i][j];
            sum%=mod;
        }
    }
    printf("%lld\n",sum );
}


引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值