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 
using namespace std;
vector 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


你可能感兴趣的:(树形dp)