C. Kefa and Park
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Kefa decided to celebrate his first big salary by going to the restaurant.

He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.

The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats.

Your task is to help Kefa count the number of restaurants where he can go.

Input

The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.

The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).

Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.

It is guaranteed that the given set of edges specifies a tree.

Output

A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.

Examples
Input
4 1
1 1 0 0
1 2
1 3
1 4
Output
2
Input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
Output
2
Note

Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.

Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.

Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.

题意:给一棵树  告诉你n个节点上分别是否有猫 从根节点开始到叶子节点的路径上最多能连续经过m只猫

问从根节点,能够到达多少个叶子节点

题解:dfs处理  搜索的同时 ,记录连续经过的猫的最大值

有一点技巧处理,如何判断叶子节点的问题 :因为存储的是双向边,除了根节点,

只有叶子节点的父亲节点的个数为1 标记处理一下。就可以判断出叶子节点。

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int nedge;
int n,m;
int pre[];
int have[];
int used[];
int a,b,c;
int ans=;
int vis[];
struct node
{
int to;
int w;
int pre;
}N[];
void add(int aa,int bb,int cc)
{
nedge++;
N[nedge].to=bb;
N[nedge].w=cc;
N[nedge].pre=pre[aa];
pre[aa]=nedge;
}
void dfs(int s,int jishu,int zhi)
{
if(s!=&&zhi<=m&&vis[s]<)
{
ans++;
return ;
}
for(int i=pre[s];i;i=N[i].pre)
{
if(!used[N[i].to])
{
used[N[i].to]=;
if(have[N[i].to])
dfs(N[i].to,jishu+,max(zhi,jishu+));
else
dfs(N[i].to,,zhi);
}
}
}
int main()
{
scanf("%d %d",&n,&m);
nedge=;
memset(pre,,sizeof(pre));
memset(used,,sizeof(used));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
scanf("%d",&have[i]);
for(int i=;i<n;i++)
{
scanf("%d %d",&a,&b);
add(b,a,);
vis[b]++;
add(a,b,);
vis[a]++;
}
used[]=;
if(have[])
dfs(,,);
else
dfs(,,);
cout<<ans<<endl;
return ;
}

最新文章

  1. mysql 中基础英语单词 (一)关于数据库创建与查找 (包括简写单词)
  2. JavsScript+dom
  3. XML 链接
  4. gitlab 配置
  5. vpn与局域网冲突解决方案
  6. 【CentOS】samba服务器安装与配置
  7. NLog官方文档
  8. 数据库连接类oracleHelper
  9. SQL操作(增删改查)
  10. Linq101-Partitioning
  11. ISO7816协商模式和特定模式
  12. Struts2--标签tag
  13. reStructuredText文件语法简单学习
  14. 低版本IDE 打开 高版本 IDE 代码时 unit
  15. 【转载】chown和chmod使用
  16. BZOJ2240 : ural1676 Mortal Combat
  17. AtomicLong.lazySet 是如何工作的?
  18. CRC分段校验
  19. 使用C#利用cmd来调用java jar包获取其中的数据
  20. virtualbox+vagrant学习-2(command cli)-2-vagrant cloud命令--有问题

热门文章

  1. react 信用卡格式检验
  2. RuPengGame游戏引擎 精灵 createSprite 创建 setSpritePosition 设置位置 playSpriteAnimate 播放动画 setSpriteFlipX设置翻转 精灵图片下载地址
  3. 51nod——2478 小b接水(预处理 思维)
  4. position的 relative+absolute实现固定标签在窗口的某个位置
  5. Java 获取Web项目相对webapp地址
  6. Windows10系统下查看mysql的端口号并修改
  7. GNU汇编程序框架
  8. vue.js 图表chart.js使用
  9. vue 项目中使用mock假数据实现前后端分离
  10. OpenCV学习笔记(五) 文件存取