Codeforces Round #382 (Div. 2)E. Ostap and Tree
2 seconds
256 megabytes
standard input
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.
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.
Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).
2 0
1 2
1
2 1
1 2
3
4 1
1 2
2 3
3 4
9
7 2
1 2
2 3
1 4
4 5
1 6
6 7
91
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的更多相关文章
- 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 ...
- Codeforces Round #382 (Div. 2) 继续python作死 含树形DP
A - Ostap and Grasshopper zz题能不能跳到 每次只能跳K步 不能跳到# 问能不能T-G 随便跳跳就可以了 第一次居然跳越界0.0 傻子哦 WA1 n,k = map ...
- 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/ ...
- 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 ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- 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 ...
- Codeforces Round #382 (Div. 2) D. Taxes 歌德巴赫猜想
题目链接:Taxes D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- ext combobox getValue
使用combobox时,它有一个hiddenName的属性,专门用于提交combobox中value的值. 现假设某combobox的Id为comboId,hiddenName属性的值为hiddenV ...
- [DP] Rod-cutting problem
给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同.下面是一个例子 我们要通过切成小段卖出尽可能高的总价钱.问题是:How to decompose the problem? De ...
- IBM
http://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1306mongodb2/
- easyui源码翻译1.32--panel(面板)
前言 昨天发布了表格datagrid的翻译源码 ,easyui的许多插件有依赖关系 比如datagrid 的渲染需要panel.resizable.linkbutton.pagination 今 ...
- 第六章Audio设备
6.1 Audio设备介绍 USB协议制定时,为了方便不同设备的开发商基于USB进行设计,定义了不同的设备类来支持不同类型的设备.虽然在USB标准中定义了USB_DEVICE_CLASS_AUDIO- ...
- Qt for PC,Qt for iOS,Qt for Android (居士的博客)
http://blog.csdn.net/Esonpo/article/details/38081607 http://blog.csdn.net/Esonpo/article/details/380 ...
- java集合类——Stack类
查看java的API文档,Stack继承Vector类. 栈的特点是后进先出. API中Stack自身的方法不多,基本跟栈的特点有关. import java.util.Stack; public c ...
- TFS源代码管理
一.服务器配置 1.创建一个Visual Studio Online账户 打开VS,选择团队资源管理器(视图菜单下),然后在团队资源管理器中选择注册Team Foundation Service,打开 ...
- 轻松搭建自己的Linux发行版本
许多人想要搭建自己的Linux发行版本,可能是觉得有趣,也可能是为了学习更多的Linux知识,或者因为他们有很正式的问题要解决.但是秘密是:自己搭建完美的发行版本不是很困难的一件事.事实上,我们收集了 ...
- How to Customize Server Header using NginX headers-more module
http://wiki.nginx.org/HttpHeadersMoreModule#Version headers_more When you are browsing a website, yo ...