Codeforces 735 E Ostap and Tree
Discription
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
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
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那么就相当于没有黑点。
/*
f[x][y][z] => 以x为根的子树中 ,最近的黑点距离x为 y-1 ,
最远的(没有被覆盖到的)白点距离x为 z-1 的方案数。 如果不存在黑点或白点那么那一维是0
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
const int maxn=105;
int hd[maxn],n,m,to[maxn*2],num;
int ne[maxn*2],f[maxn][25][25],k;
int ans=0,g[25][25]; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline void addline(int x,int y){
to[++num]=y,ne[num]=hd[x],hd[x]=num;
} inline void MERGE(int x,int y){
memset(g,0,sizeof(g)); for(int i=k+1;i>=0;i--)
for(int j=k+1;j>=0;j--) if(f[x][i][j])
for(int I=k+1,NearB,FarW;I>=0;I--)
for(int J=k+1;J>=0;J--) if(f[y][I][J]){
NearB=1<<30;
if(i) NearB=i;
if(I) NearB=min(NearB,I+1);
if(NearB>k+1) NearB=0; FarW=0;
if(j&&(!I||(j+I-1)>k)) FarW=j;
if(J&&(!i||(J+i-1)>k)) FarW=max(FarW,J+1); g[NearB][FarW]=add(g[NearB][FarW],f[x][i][j]*(ll)f[y][I][J]%ha);
} memcpy(f[x],g,sizeof(g));
} void dfs(int x,int fa){
f[x][1][0]=f[x][0][1]=1;
for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
dfs(to[i],x);
MERGE(x,to[i]);
}
} inline void calc(){
for(int i=k+1;i>=0;i--) ans=add(ans,f[1][i][0]); /*
for(int i=1;i<=n;i++)
for(int j=0;j<=k+1;j++)
for(int l=0;l<=k+1;l++) printf("f[%d][%d][%d] = %d\n",i,j,l,f[i][j][l]);
*/
} int main(){
scanf("%d%d",&n,&k);
int uu,vv;
for(int i=1;i<n;i++){
scanf("%d%d",&uu,&vv);
addline(uu,vv),addline(vv,uu);
} dfs(1,1);
calc();
printf("%d\n",ans);
return 0;
}
Codeforces 735 E Ostap and Tree的更多相关文章
- Codeforces Round #382 (Div. 2)E. Ostap and Tree
E. Ostap and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 342E :Xenia and Tree
Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...
- Codeforces Edu3 E. Minimum spanning tree for each edge
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- 使用Redis作为高速缓存
Redis适合哪些业务场景常规业务系统的数据库访问中,读写操作的比例一般在7/3到9/1,也就是说读操作远多于写操作,因此高并发系统设计里,通过NoSQL技术将热点数据(短期内变动概率小的数据)放入内 ...
- IE8 png图片黑色阴影的解决方案!
以前都没有留意这个问题,最近开发中才发现. 什么情况下会出现黑边? PNG透明图片,同时有阴影,具备着两个IE8是不会有问题的,再加上使用了 “渐变显示”,就会出现了. 解决方法: img{displ ...
- 【css】清楚浏览器端缓存
/css/common.css?version=1.0.7 在css链接后面加个参数版本号控制,刷新浏览器缓存
- 学习python的第十天(内置算法:列表数据类型,元祖数据类型,字典数据类型)
5.8自我总结 1.列表类型内置算法 1.必须掌握 1.按索引取值(正向取值+反向取值),即可存也可以取 #用于取其中一个值 name = ['yang','wen','yi'] ##正方向取wen, ...
- Django初学及mvt模型理解
Django是python语言用来做web项目的一个模板,创建Django项目之后会自动生成models,views和templates文件夹,又叫mvt框架 文件结构如下: Models:其中mod ...
- C++ STL容器底层机制
1.vector容器 vector的数据安排以及操作方式,与array非常相似.两者的唯一区别在于空间的运用的灵活性.array是静态空间,一旦配置了就不能改变.vector是动态空间,随着元素的加入 ...
- List<T> List<?> 区别用法
List<T>是泛型方法,List<?>是限制通配符 List<T>一般有两种用途:1.定义一个通用的泛型方法.伪代码: public interface Dao{ ...
- 织梦dedecms自定义表单设置必填项
1. 用php验证 在plus/diy.php的第 40行下加 //增加必填字段判断 if($required!=''){ if(preg_match('/,/', $required)) { $re ...
- 关于Linux下安装Oracle时报错:out of memory的问题分析说明
一.说明 在Oracle安装过程中,可能遇到out of memory这种错误,这是由于系统内存不足导致!我们可以通过加内存的方式解决! 而如果是另一种情况呢: 例如我在主机上装了两个Oracle服务 ...
- 一步一步在ubuntu上安装即时通讯服务器-Openfire
1.首先登录到ubuntu server.在安装openfire 服务器之前,先确保你的系统已经更新到最新.然后输入下面的命令,一行一行执行,最后安装可用的更新 sudo apt-get update ...