Codeforces Manthan, Codefest 17 C. Helga Hufflepuff's Cup 树型DP

来源:互联网 发布:随机抽取数字软件 编辑:程序博客网 时间:2024/06/05 16:53

C. Helga Hufflepuff's Cup
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 vaults ui 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.

Examples
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 1 implying 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.



一棵树需要给所有的节点分配点权,每个点可以取1到m间任意一个数,要求若某点点权为k,则与其相邻的点点权必须小于k,且最多只能标注x个点的点权为k。问有多少种分配点权的方式。


树型DP。

dp[i][j][l]表示以 i 为根的子树上,选了 j 个节点的权值为k的方案数。第 i 号节点的权值等于k时l=0,若小于k则l=1,大于k则l=2.

一开始时,i号点可以取1到m间所有数,所以dp[i][1][0] = 1; dp[i][0][1] = p - 1; dp[i][0][2] = m - p;

向上DP时,每有一个儿子节点to,我们就将之前的方案总数加上这个儿子的子树的贡献。转移方程如下:

(0<=x<=j,0<=k<=j)

dp[now][j][0] = dp[to][j - k][1]*dp[now][k][0];                      当前节点选k,那么儿子的子树所有点权值只能小于k

dp[now][j][1]=(dp[to][j-k][0]+dp[to][j-k][1]+dp[to][j-k][2])*dp[now][k][1];   当前节点小于k,儿子节点可以随意取
dp[now][j][2] = (dp[to][j-k][1] + dp[to][j-k][2])*dp[now][k][2] ;        当前节点大于k,则儿子节点权值不能为k

为了避免一次转移当中,已经转移的状态的数值对未转移状态的影响,需要在每次转移当中用新数组记录,转移后再把DP的值移动过去。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=100005, inf = 0x3f3f3f3f;const ll llinf = 0x3f3f3f3f3f3f3f3f,mod=1000000007;const ld pi = acos(-1.0L);vector<int> v[maxn];ll dp[maxn][11][3],a[maxn][11][3];bool visit[maxn];int n;ll m, p, x;void dfs(int now) {visit[now] = 1;int size = v[now].size(),i,j,k,last=-1;dp[now][1][0] = 1; dp[now][0][1] = p - 1; dp[now][0][2] = m - p;for (i = 0; i < size; i++) {int to = v[now][i];if (!visit[to]) {dfs(to);for (j = 0; j <= x; j++) {for (k = 0; k <= 2;k++) {a[now][j][k] = 0;}}for (j = 0; j <= x; j++) {for (k = j; k >= 0;k--) {if (j != 0&&k!=0) a[now][j][0] += dp[to][j - k][1]*dp[now][k][0];a[now][j][1]+=((dp[to][j-k][0]+dp[to][j-k][1]+dp[to][j-k][2])*dp[now][k][1])%mod;a[now][j][2] += ((dp[to][j-k][1] + dp[to][j-k][2])*dp[now][k][2]) % mod;a[now][j][1] %= mod; a[now][j][2] %= mod; a[now][j][0] %= mod;}}for (j = 0; j <= x; j++) {for (k = 0; k <= 2;k++) {dp[now][j][k] = a[now][j][k];}}}}}int main() {int s,t,i,j,f;scanf("%d%I64d", &n, &m);for (i = 1; i < n; i++) {scanf("%d%d", &s, &t);v[s].push_back(t);v[t].push_back(s);}scanf("%I64d%I64d", &p, &x);mem0(dp); mem0(visit);dfs(1);ll sum = 0;for (i = 0; i <= x; i++) {for (j = 0; j <= 2; j++) {//if (i == 0 && j == 0) continue;sum += dp[1][i][j];sum %= mod;}}/*for (i = 1; i <= n; i++) {for (j = 0; j <= x; j++) {for (f = 0; f <= 2; f++) printf("%d %d %d %lld\n", i, j, f, dp[i][j][f]);}}*/printf("%I64d\n", sum);//system("pause");return 0;}