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 ...
随机推荐
- [Jquery] Jquery获取浏览器宽高的代码
<script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...
- C#变成数据导入Excel和导出Excel
excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...
- 1920-Jangbi的Rush
描述 最后一届的OSL决赛由神族的Jangbi对阵人族Fantasy.Jangbi5BG爆叉叉准备一波rush,但是范特西早有防备,在地图上埋下了许多地雷.但是Jangbi显然不是毕姥爷那样的无脑平A ...
- POJ3714+最近点对
特判标记即可 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...
- CoreData的简单使用(二)数据的增删改查,轻量级的版本迁移
上一篇中我们已经使用CoreData创建了一个SQLite数据库 CoreData的简单使用(一)数据库的创建 现在对数据库进行数据的CRUD(增删改查) 1.Data Model 的设置 创建一个D ...
- DHTMLX 前端框架 建立你的一个应用程序 教程(七)-- 添加筛选功能
表格的过滤筛选 我们在每列第一行添加一个文本,用做数据的条件筛选. 我们还提供服务端的筛选 ,当有大量数据时 , 我们可以使用dhtmlxConnector 进行后台数据的筛选. 添加过滤器到表格列中 ...
- VirtualBox虚拟vdi磁盘扩容
http://blog.chinaunix.net/uid-25627207-id-3342576.html
- Keepalived+MySQL双主
一.Keepalived+MySQL Replication的应用场景 MySQL的高可用方案有cluster,MMM,MHA等,这些高可用方案都要三台服务器以上,成本有点高,今天介绍一个低成本高可用 ...
- FileSystemWatcher使用方法
FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删除.改动.重命名等活动.可以动态地定义需要监控的文件类型及文件属性改动的类型. 1.常用的几个基本属性: (1) P ...
- Oracle系列之权限
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 赋予权限:前三个要在管理员权限用户下进行操作 方法1: grant dba to db_user;--赋予用户数据库管理权限 方法2: ...