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. AOSP常见漏洞类型简介

    Heap/Stack Overflow(CVE-2017-0541) 漏洞出现在PushcdlStack函数中,如下所示   # /external/sonivox/arm-wt-22k/lib_sr ...

  2. nginx下配置Yii2 rewrite、pathinfo等

    环境说明: 我试用的lnmp安装包安装的nginx,nginx版本是1.14.1 server { listen ; server_name www.baidu.com; #access_log /d ...

  3. 多种方式实现依赖注入及使用注解定义bean

    构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...

  4. 树莓派下ubuntu-mate中ssh服务的安装与开机后自启动

    ssh程序分为客户端程序openssh-client和服务端程序openssh-server. 如果需要ssh登陆到别的电脑,需要安装openssh-client,该程序ubuntu是默认安装的.而如 ...

  5. 使用python实现滑动验证码

    首先安装一个需要用到的模块 pip install social-auth-app-django 安装完后在终端输入pip list会看到 social-auth-app-django social- ...

  6. CSAPP 缓冲区溢出试验

    缓冲区溢出试验是CSAPP课后试验之一,目的是: 更好的理解什么是缓冲区溢出 如何攻击带有缓冲区溢出漏洞的程序 如何编写出更加安全的代码 了解并理解编译器和操作系统为了让程序更加安全而提供的几种特性 ...

  7. LightOJ 1141 Number Transformation

    Number Transformation In this problem, you are given an integer number s. You can transform any inte ...

  8. 浅谈javascript的运行机制

    积累一下这几天学的,记录一下: 一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程 ...

  9. day40--mysql step4 SQLAlchemy

    1.unique = True 表示启动唯一索 2.有add 必须有commit这样数据才会提交 3.ORM功能 #!/usr/bin/env python # -*- coding:utf-8 -* ...

  10. [网站公告]又拍云API故障造成图片无法上传(已恢复)

    大家好,18:00左右开始,又拍云API出现故障,调用图片上传API时出现错误:“The remote server returned an error: (403) Forbidden.”,造成图片 ...