Manthan, Codefest 17-C-Helga Hufflepuff's Cup(树形DP)

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

题意:给你n个点,n-1条边,让后给你个m,代表警戒值的最大值,再给你k和x,代表最优警戒值和最优警戒值的最大数量

然后让你给每个点定义一个警戒值。假如该点定义为最优警戒值,则与该点相连的点的警戒值不能定义为>=k的值。问你有多少种制定方案。

题解:

dp[i][j][0]:以i为根的子树中有j个最优警戒值,并且i点警戒值小于k

dp[i][j][1]:以i为根的子树中有j个最优警戒值,并且i点警戒值等于k

dp[i][j][2]:以i为根的子树中有j个最优警戒值,并且i点警戒值大于k

然后树形DP即可。

#include<set>  
#include<map>     
#include<stack>            
#include<queue>            
#include<vector>    
#include<string> 
#include<time.h>
#include<math.h>            
#include<stdio.h>            
#include<iostream>            
#include<string.h>            
#include<stdlib.h>    
#include<algorithm>   
#include<functional>    
using namespace std;            
#define ll long long       
#define inf 1000000000       
#define mod 1000000007             
#define maxn  100005
#define lowbit(x) (x&-x)            
#define eps 1e-9
vector<int>q[maxn];
int n,m,k,x,dp[maxn][12][3],g[12][3];
void dfs(int u,int p)
{
	int i,j,t;
	dp[u][1][1]=1;
	dp[u][0][0]=k-1;
	dp[u][0][2]=m-k;
	for(t=0;t<q[u].size();t++)
	{
		int v=q[u][t];
		if(v==p)
			continue;
		dfs(v,u);
		memset(g,0,sizeof(g));
		for(i=x;i>=0;i--)
			for(j=x-i;j>=0;j--)
			{
				g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][0])%mod;
				g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][1])%mod;
				g[i+j][0]=(g[i+j][0]+1ll*dp[u][i][0]*dp[v][j][2])%mod;
				g[i+j][1]=(g[i+j][1]+1ll*dp[u][i][1]*dp[v][j][0])%mod;
				g[i+j][2]=(g[i+j][2]+1ll*dp[u][i][2]*dp[v][j][0])%mod;
				g[i+j][2]=(g[i+j][2]+1ll*dp[u][i][2]*dp[v][j][2])%mod;
			}
		for(i=0;i<=x;i++)
			for(j=0;j<=2;j++)
				dp[u][i][j]=g[i][j];
	}
}
int main(void)
{
	int xx,yy,i,j,ans=0;
	scanf("%d%d",&n,&m);
	for(i=1;i<n;i++)
	{
		scanf("%d%d",&xx,&yy);
		q[xx].push_back(yy);
		q[yy].push_back(xx);
	}
	scanf("%d%d",&k,&x);
	dfs(1,0);
	for(i=0;i<=x;i++)
		for(j=0;j<=2;j++)
			ans=(ans+dp[1][i][j])%mod;
	printf("%d\n",ans);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值