E. Ostap and Tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 k. Distance 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 ≤ 100, 0 ≤ 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}.

一道神奇的树形dp。

 #include <bits/stdc++.h>
const int mod = ;
using namespace std;
int n,k,cnt = ,head[];
struct node
{
int nxt,to;
}edge[];
long long int dp[][],f[]; void add(int a,int b)
{
edge[++cnt].to = b,edge[cnt].nxt = head[a];head[a] = cnt;
} void Init()
{
scanf("%d%d",&n,&k);
int a,b; memset(head,-,sizeof(head));
for(int i = ; i < n; ++i)
{
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
} void dfs(int u,int pre)
{
dp[u][] = dp[u][k+] = ;
for(int i = head[u];~i; i = edge[i].nxt)
{
int v = edge[i].to;
if(v == pre) continue;
dfs(v,u);
memset(f,,sizeof(f));
for(int x = ; x <= * k + ; ++x)
{
for(int y = ; y <= * k; ++y)
{
if(x + y <= *k) f[min(x,y+)] = ( f[min(x,y+)] + ( dp[u][x] * dp[v][y] % mod ) ) % mod ;
else f[max(x,y+)] = ( f[max(x,y+)] + ( dp[u][x] * dp[v][y] % mod ) ) % mod ;
} }
memcpy(dp[u], f, sizeof f);
} } void Solve()
{
dfs(,-);
long long int ans = 0ll;
for(int i = ; i <= k; ++i) ans = (ans + dp[][i])%mod;
cout<<ans<<endl;
} int main()
{
// freopen("E.in","r",stdin);
// freopen("E.out","w",stdout);
Init();
Solve();
fclose(stdin);
fclose(stdout);
return ;
}

Codeforces Round #382 (Div. 2)E. Ostap and Tree的更多相关文章

  1. Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs

    A. Ostap and Grasshopper 题面 On the way to Rio de Janeiro Ostap kills time playing with a grasshopper ...

  2. Codeforces Round #382 (Div. 2) 继续python作死 含树形DP

    A - Ostap and Grasshopper zz题能不能跳到  每次只能跳K步 不能跳到# 问能不能T-G  随便跳跳就可以了  第一次居然跳越界0.0  傻子哦  WA1 n,k = map ...

  3. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  4. Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想

    D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...

  5. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

  6. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  7. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  8. Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契

    C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. Codeforces Round #382 (Div. 2) D. Taxes 歌德巴赫猜想

    题目链接:Taxes D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. U当家U盘启动盘制作教程

    U当家U盘启动盘制作工具制作u盘系统安装盘支持老机器.新机器启动以及制作,U当家内置强大的U盘一键重装系统工具支持传统和GPT安装,支持原版操作系统和GHOST操作系统安装(ISO/GHO/WIM), ...

  2. How to steal any developer's local database

    原文链接: http://bouk.co/blog/hacking-developers/ If you’re reading this and you’re a software developer ...

  3. PHP ini_set() 函数

    PHP ini_set用来设置php.ini的值,在函数执行的时候生效,对于虚拟空间来说,很方便,下面为大家介绍下此方法的使用 PHP ini_set用来设置php.ini的值,在函数执行的时候生效, ...

  4. Windows下虚拟Linux

    andlinux cygwin virtualbox VMware XenServer

  5. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-012-AOP总结

    1.AOP是面向对象编程的有力补充,它可以让你把分散在应用中的公共辅助功能抽取成模块,以灵活配置,减少了重复代码,让类更关注于自身的功能

  6. 绕过kernel模块版本校验检测

    kernel module version check bypass . 举例说明 . 内核是怎么实现的 . 怎样去突破 . 总结 . 举例说明 Linux内核版本很多,升级很快,2个小内核版本中内核 ...

  7. BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...

  8. [swustoj 411] 售货员的难题

    售货员的难题(0411) Time limit(ms): 5000 Memory limit(kb): 65535 Submission: 1744 Accepted: 200 Description ...

  9. 使用Xcode查找项目中的中文字符串

    打开”Find Navigator” 切换搜索模式到 “Find > Regular Expression” 输入@"[^"]*[\u4E00-\u9FA5]+[^" ...

  10. ASP.NET操作Word的IIS权限配置

    ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-000 ...