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. python 线程的调用方式

    python 线程的调用方式 #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/5/24 9:44 # ...

  2. 使TextBox的内容换行

    首先你把TextBox控件的MultiLine属性设置为True,然后把TextBox控件的Text属性根据程序需要,在需要换行的地方加入\r\n这样就可实现换行了

  3. RabbitMQ-消费者"未处理完的消息"丢失

    一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...

  4. ElasticSearch High Level REST API【2】搜索查询

    如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...

  5. scrapy 圣墟

    # -*- coding: utf-8 -*- import scrapy from sx.items import SxItem class SkSpider(scrapy.Spider): nam ...

  6. HTTP 配置与编译安装

    目录 HTTP 配置与编译安装 HTTP 相关配置 DSO 定义'Main' Server 的文档页面路径 定义站点主页面 站点访问控制常见机制 基于源地址实现访问控制 日志设定 设定默认字符集 定义 ...

  7. CSS-标准盒模型 & 怪异盒模型

    CSS中Box model分类 CSS中Box model是分为两种:: W3C标准 和 IE标准盒子模型. 大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 怪异模式是“ ...

  8. django admin模块使用

    BBS之admin组件的使用 1.创建超级管理员 创建超级管理员 一. tools>>>>runmanagepyTask>>>>>createsu ...

  9. Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码

    题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...

  10. A1075 PAT Judge (25)(25 分)

    A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...