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 ;
}

Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)的更多相关文章

  1. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  2. 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

    题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...

  3. Codeforces Round #321 (Div. 2) Kefa and Park 深搜

    原题链接: 题意: 给你一棵有根树,某些节点的权值是1,其他的是0,问你从根到叶子节点的权值和不超过m的路径有多少条. 题解: 直接dfs一下就好了. 代码: #include<iostream ...

  4. Codeforces Round #321 (Div. 2) A, B, C, D, E

    580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...

  5. Codeforces Round #321 (Div. 2)C(tree dfs)

    题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...

  6. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  8. Codeforces Round #290 (Div. 2) B (dfs)

    题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...

  9. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

随机推荐

  1. XAMPP安装过程中,出现的问题

    这次运行一个简单的前端(html+css+js+ajax)+php后端项目,运行XAMPP的时候,出现两个问题: phpmyadmin运行不起来,一直报1544错误 请求本地图片及php文件报403错 ...

  2. API调用微信getWXACodeUnlimit()获取小程序码

    微信文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/qr-code/getWXACodeUnlimit.html? ...

  3. PowerDesigner导入Excel模板生成实体

        在Excel里整理好的表模型数据,可直接导入PowerDesigner.此功能通过PowerDesigner的脚本功能来实现,使用起来也简单.具体操作方法:     打开PowerDesign ...

  4. Android系统中标准Intent的使用

    Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...

  5. ubuntu 压缩 解压 命令大全

    ubuntu下文件压缩/解压缩命令总结 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz ...

  6. Python中的bytes

    bytes_lst = [ ('创建bytes',), ('bytes可哈希',), ('编码与解码',), ('常见编码类型',), ('ord() 与 chr()',), ] 创建bytes &g ...

  7. 初学python来进行odoo12版本开发

    这是我的第一篇博客.请多多指教! 首先要下载odoo-12的源代码 官方下载路径:          https://github.com/odoo/odoo/archive/12.0.zip 随便新 ...

  8. 水题:HDU-1088-Write a simple HTML Browser(模拟题)

    解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...

  9. L1-039 古风排版 (20 分)

    L1-039 古风排版 (20 分)   中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<),是每一列的字符数.第 ...

  10. The DOM in JavaScript

    DOM : Document Object Model   D is for document :  The DOM cant work without a document . When you c ...