Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)

185 篇文章 0 订阅
116 篇文章 0 订阅

Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.

Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than kDistance between two vertices of the tree is the minimum possible number of edges of the path between them.

As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 1000 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.

Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.

Output

Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).

Examples
input
2 0
1 2
output
1
input
2 1
1 2
output
3
input
4 1
1 2
2 3
3 4
output
9
input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
output
91
Note

In the first sample, Ostap has to paint both vertices black.

In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.

In the third sample, the valid ways to paint vertices are: {1, 3}{1, 4}{2, 3}{2, 4}{1, 2, 3}{1, 2, 4}{1, 3, 4}{2, 3, 4},{1, 2, 3, 4}.

题意:给一棵全是白点的树,要求给树上的一些点染色,使得所有距离白点最近的黑点和其白点之间的距离不大于k,问方案数。

分析:dp[0][u][i] 表示 以u为根的子树中离根最近的黑点距离根的距离为i的方案数,dp[1][u][i]表示以u为根的子树中离根最远的未满足点距离根的距离为i的方案数。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007  
using namespace std;
int n,k,u,v;
long long ans,fl[25],fw[25],dp[2][105][25];
vector<int> G[105];
void dfs(int u,int fa)
{
	for(int v : G[u])
	 if(v != fa) dfs(v,u);
	dp[0][u][0] = dp[1][u][0] = 1;             //满足条件最浅和不满足条件最深 
	for(int v : G[u])
	 if(v != fa)
	 {
	 	memset(fl,0,sizeof(fl));
		memset(fw,0,sizeof(fw)); 
	 	for(int i = 0;i <= k;i++)
		{ 
	 		for(int j = 0;j <= k;j++)           //win win
	 	  	 fw[min(i,j+1)] += dp[0][u][i] * dp[0][v][j] % MOD;
	 	  	for(int j = 0;j <= k;j++)           //win lose
	 	  	 if(k - i >= j+1) fw[i] += dp[0][u][i] * dp[1][v][j] % MOD;
			 else fl[j+1] += dp[0][u][i] * dp[1][v][j] % MOD; 
		}
		for(int i = 0;i <= k;i++)
		{
			for(int j = 0;j <= k;j++)         //lose win
			 if(k - j - 1 >= i) fw[j+1] += dp[1][u][i] * dp[0][v][j] % MOD;
			 else fl[i] += dp[1][u][i] * dp[0][v][j] % MOD;
			for(int j = 0;j <= k;j++)          // lose lose 
			 fl[max(i,j+1)] += dp[1][u][i] * dp[1][v][j] % MOD;
		}
		for(int i = 0;i <= k;i++)
		{
			dp[0][u][i] = fw[i] % MOD;
			dp[1][u][i] = fl[i] % MOD;
		}
	 }
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>k;
	for(int i = 1;i < n;i++)
	{
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u); 
	}
	dfs(1,-1);
	for(int i = 0;i <= k;i++) ans = (ans + dp[0][1][i]) % MOD;
	cout<<ans<<endl;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值