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 ...
随机推荐
- Java中集合类
一.Collection Collection 接口用于表示任何对象或元素组.想要尽可能以常规方式处理一组元素时,就使用这一接口.Collection 在前面的大图也可以看出,它是List 和 Set ...
- POI创建生成excel及设置相关属性
简单的读写到excel中: import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io. ...
- [Tkinter 教程] 布局管理 (Pack Place Grid)
原系列地址: Python Tkinter 简介: 本文讲述如何使用 tkinter 的布局管理 (被称作 layout managers 或 geometry managers). tkinter ...
- bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- iOS 优秀博客
中文 iOS/Mac 开发博客列表 GitHub 上排名前 100 的 Objective-C 项目简介 GitHub 上都有哪些值得关注学习的 iOS 开源项目? iOS开发系列文章(持续更新……) ...
- 【状态压缩 meet in middle】poj3139Balancing the Scale
数组溢出真是可怕的事情 Description You are given a strange scale (see the figure below), and you are wondering ...
- (6)zabbix主机与组配置
1. 创建主机方法 1.1 新建主机configuration(配置)->Hosts(主机)->Create host(创建主机) 见前面的博文 1.2 克隆/完全克隆主机 2. 主机参数 ...
- (4)zabbix监控第一台服务器
2. zabbix监控服务器 创建主机,选择模板以及录入基本信息,过一分钟左右,就可以看到cpu.内存.硬盘等等使用情况.本节以图文为主.by the way, zabbix中文翻译很烂,config ...
- PWA介绍
https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 PWA是一些技术的集合.用于消除web与其他客户端之间的差距,最 ...
- yum安装php7.2
文章来源:https://www.cnblogs.com/hello-tl/p/9404655.html 分享一个算是比较完美的php7.2yum安装 0.更换yum原 # yum install e ...