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 ...
随机推荐
- css中文本框与按钮对不齐解决方案
我们先对对input标记设定样式,代码如下: html 代码 <form> <input type=”text” name=”text1” id=”text1” /> < ...
- [转载]C# Random 生成不重复随机数
Random 类 命名空间:System 表示伪随机数生成器,一种能够产生满足某些随机性统计要求的数字序列的设备. 伪随机数是以相同的概率从一组有限的数字中选取的.所选数字并不具有完全的随机性,因为它 ...
- vs2012+cmake+opencv+opencv unable to find a build program corresponding to "Visual Studio 12 Win64". CMAKE_MAKE_PROGRAM is not set
搜索了下,说什么的都有! 一,提示找不到 cmake-2.8.12.1 的 modles 卸载了cmake后发现 cmd 中的 cmake --version 还是 2.8.11.1 找到是我的cyg ...
- 一个用于清除loadrunner产生log文件的批处理
@echo off set work_path="%~dp0" for /R %%s in (*.txt,*.log) do ( del /f "%%s" ) ...
- ASP.NET MVC 入门1、简介
什么是MVC模式 MVC(Model-View-Controller,模型-视图-控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Co ...
- 【HDOJ】1061 Rightmost Digit
这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...
- bzoj1497
这道题让我涨姿势了 对于这类问题,我们称作最大权闭合图问题 就是每个点都有一个点权,要求选择一个点集,其中每个点的指向的点也在点集中,使这样一个点权和最大 对于这种问题,我们添加源点s,汇点t 对于点 ...
- 【CSS3】
Web前端实验室http://demo.doyoe.com/ ::before ::afterCSS3已经将伪元素的前缀更改为双冒号,而伪类则保持为单冒号 backface-visibility ht ...
- 使用Xcode查找项目中的中文字符串
打开”Find Navigator” 切换搜索模式到 “Find > Regular Expression” 输入@"[^"]*[\u4E00-\u9FA5]+[^" ...
- Bootstrap Paginator分页插件的使用
今天,我为大家带来的一款做得非常优秀的分页插件BootStrap Paginator,他是一款js插件,由于本人也是才刚刚搞出来的,所以暂时对它也不是特别了解,只能大楖告诉大家怎么使用.我这里使用的是 ...