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

来源:互联网 发布:淘宝steam慈善包划算吗 编辑:程序博客网 时间:2024/05/18 04:16

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 21 22 31 41 2
Output
1
Input
3 31 21 32 1
Output
13
Input
3 11 21 31 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 );}


阅读全文
0 0
原创粉丝点击