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 ...
随机推荐
- struts2的单个文件上传
本文主要两种方式,一:通过 FileUtils.copyFile(file, savefile);方法复制:二:通过字节流方式复制 web.xml <?xml version="1.0 ...
- ios之UISplitViewController
iPad的屏幕比iPhone大,所以在界面上,iPad比iPhone多一个UISplitViewController,用来实现iPad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容. ...
- 数据库事务ACID和事务的隔离级别
借鉴:https://blog.csdn.net/zh521zh/article/details/69400053和https://blog.csdn.net/May_3/article/detail ...
- nginx + 一个端口 部署多个单页应用(history模式)
目前web开发 使用一般前后端分离技术,并且前端负责路由.为了美观,会采用前端会采用h5 history 模式的路由.但刷新页面时,前端真的会按照假路由去后端寻找文件.此时,后端必须返回index(i ...
- 【http】【转发】HTTP访问控制(CORS)
当一个资源从与该资源本身所在的服务器不同的域或端口请求一个资源时,资源会发起一个跨域 HTTP 请求. 比如,站点 http://domain-a.com 的某 HTML 页面通过 <img ...
- 文件的软硬链接& 文件编辑vi和vim
目录 文件的软硬链接 1.软链接 2.硬链接 文件编辑vi和vim 须先安装vim命令的软件包yum install -y vim 三种模式: 1.普通模式 2.编辑模式 3.末行模式 文件的软硬链接 ...
- Ubuntu桌面主题设置以及优化
安装好Ubuntu后,觉得桌面不太美观,便动了修改主题的想法.听说Flatabulous不错,在网上搜索看过主题效果后也觉得蛮不错的,于是准备修改. 安装Unity Tweak Tool Unity ...
- 快速入门Numpy
教你十分钟学会使用numpy. 简单介绍一下numpy的话,这就是一个基于多维数组的python科学计算的核心库. 基本信息 # 一般用np作为numpy的缩写 import numpy as np ...
- mysql参数讲解
MySQL配置参数详解: http://blog.csdn.net/wlzx120/article/details/52301383 深入理解mysql参数 http://blog.itpub.net ...
- Java面试——String、StringBuider以及StringBuffer的区别和使用场景
1. String.StringBuider.StringBuffer的区别 String是不可变的对象,因此在每次对String类型进行改变的时候,都会生成一个新的String对象,然后将指针指 ...