C. Kefa and Park

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/580/problem/C

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 m consecutivevertices 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 ≤ 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

4 11 1 0 01 21 31 4

Sample Output

2

HINT

题意

给你一棵树,然后每个叶子节点会有一家餐馆

你讨厌猫,就不会走有连续超过m个节点有猫的路

然后问你最多去几家饭店

题解:

直接暴力dfs就好了……

代码:

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::ssecondnc_with_stdio(0);cin.tie(0)
#define maxn 100006
#define mod 1000000007
#define eps 1e-9
#define PI acos(-1)
const double EP  = 1E-10 ;
int Num;
//const int inf=0first7fffffff;
const ll inf=999999999;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
//*************************************************************************************int vis[maxn];
int flag[maxn];
vector<int> E[maxn];
int ans = 0;
int n,m;
void dfs(int x,int y,int z)
{for(int i=0;i<E[x].size();i++){if(E[x][i]==z)continue;if(flag[E[x][i]]==0){if(E[E[x][i]].size()==1)ans++;dfs(E[x][i],0,x);}else if(y+1<=m){if(E[E[x][i]].size()==1)ans++;dfs(E[x][i],y+flag[E[x][i]],x);}}
}
int main()
{n=read(),m=read();for(int i=1;i<=n;i++)flag[i]=read();for(int i=1;i<n;i++){int x=read(),y=read();E[x].push_back(y);E[y].push_back(x);}dfs(1,flag[1],-1);printf("%d\n",ans);
}

Codeforces Round #321 (Div. 2) C. Kefa and Park dfs相关推荐

  1. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  2. Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)

    用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...

  3. Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)

    排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...

  4. Codeforces Round #731 (Div. 3) G. How Many Paths? dfs + 拓扑 + 思维

    传送门 题意: 给你一张nnn个点mmm条边的图,让你对每个点确定一个编号,规则如下: (1)(1)(1) 对于不能到的点编号为000. (2)(2)(2) 对于只有一条路径能到这个点的点编号为111 ...

  5. Codeforces Round #321 (Div. 2)

    水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...

  6. Codeforces Round #321 (Div. 2) Kefa and Company 二分

    原题链接:http://codeforces.com/contest/580/problem/B 题意: 给你一个集合,集合中的每个元素有两个属性,$m_i,s_i$,让你求个子集合,使得集合中的最大 ...

  7. Codeforces Round #321 (Div. 2) E

    终于补好了. 题目链接: http://codeforces.com/contest/580/problem/E E. Kefa and Watch time limit per test 1 sec ...

  8. Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs

    题目链接: http://codeforces.com/contest/552/problem/C 题意: 给你100个砝码,第i个砝码质量是w^i,然后问你能不能在有m的情况下,左边和右边都放砝码, ...

  9. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

    题目链接:https://codeforces.com/contest/1139/problem/C 题意:给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路 ...

最新文章

  1. Microbiome:应用多维宏组学方法协同揭示复杂细菌群落对目标底物代谢的菌间相互关系(一作解读)...
  2. linux日志显示too many open files解决
  3. c# for提升效率的写法
  4. VUE—从入门到飞起(三)
  5. 神策数据加入中国大数据产业生态联盟,神策营销云及融媒解决方案获联盟认证
  6. iPhone审核条例
  7. 九十一、动态规划系列 背包问题之混合背包
  8. 高质量C /C编程指南---附录B :C /C试题和谜底
  9. 银行不断爆雷,金融业苦日子刚刚开始
  10. 浅析php学习的路线图
  11. Cocos2dx坐标转换
  12. jsp中jstl标签的类似 if - else 语句 的语法
  13. 简单说几个常见的数据结构
  14. Java基础算法看这一篇就够了,简单全面一发入魂
  15. Python 库学习 —— Excel存储(xlwt、xlrd)
  16. adobe animate2022动画制作软件
  17. 企业邮箱哪个好用,公司如何选择企业邮箱
  18. H3C光模块专题笔记
  19. 记录Java Web The server encountered an internal error that prevented it from fulfilling报错及解决
  20. NC15979 小q的数列

热门文章

  1. 关于Ubuntu中 E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)解决方案
  2. chrome浏览器开发者工具F12中某网站的sources下的源码如何批量保存?
  3. G - Hard problem CodeForces - 706C DP
  4. 伪分布式Hadoop2.x集群的搭建1
  5. jQuery.Callbacks之demo
  6. js中setAttribute 的兼容性
  7. 7宗命案,潜逃23年,大数据还是认出了她
  8. 数据可视化及数据保存
  9. PHP创建圆柱体的类,创建一个类
  10. 推荐两款工具给爱做实验的人