#include<bits/stdc++.h>
#define max 100005
using namespace std;
int cats[max];
vector<int>tree[max];
int visit[max];
int ans=;
int n,m;
vector<int>::iterator it;
void init()
{
ans=;
memset(visit,,sizeof(visit));
for(int i=;i<n;i++)
tree[i].clear();
memset(cats,,sizeof(cats));
}
void dfs(int node,int cat)
{
//cout<<"zhuangtai"<<node<<" "<<cat<<tree[node].size()<<" "<<endl;
visit[node]=;
if(cat>m) return;
else
{
//cout<<"jiancha"<<node<<" "<<cat<<" "<<tree[node].size()<<endl; //if(tree[node].size()==1&&node!=1)
//{
//cout<<"shenegui"<<node<<" "<<cat<<endl;
//ans++;
//}
bool ok=;
for(int i=;i<tree[node].size();i++)
{
if(!visit[tree[node][i]])
{
ok=;
//visit[tree[node][i]]=1;
if(cats[tree[node][i]])
dfs(tree[node][i],cat+cats[tree[node][i]]);
else dfs(tree[node][i],);
}
//ans++;
}
//for(it=tree[node].begin();it!=tree[node].end();it++){
// if(!visit[*it]){
// if
//}
//}
ans+=ok;
}
}
/*4 1
1 1 0 0
1 2
1 3
1 4 7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7 3 2
1 1 1
1 2
2 3
*/
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<=n;i++)
{
cin>>cats[i];
}
int a,b;
//cout<<"ok"<<endl;
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
tree[a].push_back(b);
tree[b].push_back(a);
}
visit[]=;
dfs(,cats[]);
if(ans==) cout<<<<endl;
else
cout<<ans<<endl;
}
return ;
}
B - Kefa and Park

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 mconsecutive 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 "xiyi" (without the quotes) (1 ≤ xi, yi ≤ nxi ≠ 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.

Sample Input

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

Hint

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.

Kefa and Park的更多相关文章

  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. CF580C Kefa and Park dfs

    Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual pa ...

  3. codeforces 580C Kefa and Park(DFS)

    题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...

  4. 【CF580C】Kefa and Park

    题目大意:给定一棵 N 个节点的有根树(其中根节点始终为 1 号节点),点有点权,点权只有 1 和 0 两种,求从根节点到叶子节点的路径中,有多少条路径满足:路径上最大连续点权为 1 的节点个数不超过 ...

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

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

  6. CodeForces - 580C Kefa and Park 【BFS】

    题目链接 http://codeforces.com/problemset/problem/580/C 题意 根节点是 1 然后所有的叶子结点都是饭店 从根节点到叶子结点的路径上 如果存在 大于m 个 ...

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

    dfs一遍,维护当前连续遇到的喵的数量,然后剪枝,每个统计孩子数量判断是不是叶子结点. #include<bits/stdc++.h> using namespace std; ; int ...

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

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

  9. chd校内选拔赛题目+题解

    题目链接   A. Currency System in Geraldion 有1时,所有大于等于1的数都可由1组成.没有1时,最小不幸的数就是1. #include<iostream> ...

随机推荐

  1. ThinkPHP访问不存在的模块跳到404页面

    在ACTION中新建一个文件EmptyAction.class.php,文件中的代码如下: <?php class EmptyAction extends Action{     functio ...

  2. 如何更改firefox默认搜索引擎?一步搞定!

    由于开发设计的需要,ytkah平时习惯使用firefox作为默认浏览器,火狐浏览器可添加的扩展功能比较,比如firebug.nofollow.seoquake等,还有比较友好的功能就是选中关键词拖动直 ...

  3. uchome 2.0 存在持久XSS漏洞

    发布时间:2010-09-03 影响版本:uchome 2.0 漏洞描述:看源码分析的,出错位置较敏感,而且基本没有利用限制,个人主页自定义风格时,可@import外部css文件 测试方法: 本站提供 ...

  4. hadoop学习之一

         Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Hadoop的框架最核心的设计 ...

  5. loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination

    1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...

  6. Ninth scrum meeting - 2015/11/3

    今天课上老师询问了每个团队的进度,我们发现有好多团队都已经基本完成了, 距离预定的alpha版本开发完成时间也越来越近了,我们的工作也都在有条不紊的进行着.今天又出现了git pull时有冲突的情况, ...

  7. Dialog类介绍

    Dialog类实现为一个简单的漂浮窗口,完全在Activity中创建.使用基本的Dialog类,你可以创建一个新的实例并设定标题和布局,如下所示: Dialog d = new Dialog(MyAc ...

  8. C语言课程2——我们交流的工具:Coding.net

    各位同学,大家好,在我们本学期既有老师的课堂授课,同样也有我与你们在线的辅导:那么问题来了,我与你们之间是通过何种方式进行交流,比如你的代码我怎么修改,怎样看到修改了哪些地方,我对你们的代码怎样批注, ...

  9. 转SISD、MIMD、SIMD、MISD计算机的体系结构的Flynn分类法

    1. 计算平台介绍 Flynn于1972年提出了计算平台的Flynn分类法,主要根据指令流和数据流来分类,共分为四种类型的计算平台,如下图所示: 单指令流单数据流机器(SISD) SISD机器是一种传 ...

  10. MySQL关闭过程详解和安全关闭MySQL的方法

    MySQL关闭过程详解和安全关闭MySQL的方法 www.hongkevip.com 时间: -- : 阅读: 整理: 红客VIP 分享到: 红客VIP(http://www.hongkevip.co ...