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 1
1 1 0 0
1 2
1 3
1 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- ;
int Num;
//const int inf=0first7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int vis[maxn];
int flag[maxn];
vector<int> E[maxn];
int ans = ;
int n,m;
void dfs(int x,int y,int z)
{
for(int i=;i<E[x].size();i++)
{
if(E[x][i]==z)continue;
if(flag[E[x][i]]==)
{
if(E[E[x][i]].size()==)
ans++;
dfs(E[x][i],,x);
}
else if(y+<=m)
{
if(E[E[x][i]].size()==)
ans++;
dfs(E[x][i],y+flag[E[x][i]],x);
}
}
}
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++)
flag[i]=read();
for(int i=;i<n;i++)
{
int x=read(),y=read();
E[x].push_back(y);
E[y].push_back(x);
}
dfs(,flag[],-);
printf("%d\n",ans);
}

Codeforces Round #321 (Div. 2) C. Kefa and Park dfs的更多相关文章

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

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

  2. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  3. 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 ...

  4. Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题

    A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...

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

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  6. codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...

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

    http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...

  8. Codeforces Round #321 (Div. 2) A. Kefa and First Steps【暴力/dp/最长不递减子序列】

    A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)

    E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...

随机推荐

  1. [.NET MVC进阶系列0x] EF Code First 数据迁徙(Migrations)

    [因] Entity Framework中使用Code First模式进行开发时,数据库是基于Models中的类自动生成的(生成时间:第一次运行MVC项目时), 每次更改Models中类结构,重新编译 ...

  2. 网站资料收集 主要查看js的学习部分

    1.Asp.Net MVC3.0基本的简单的可能都会用,更深入的使用还需加深研究,之后希望对MVC4.0和5.0进行对比学习,暂时看到@葡萄城控件技术团队博客的MVC5系列正在继续http://www ...

  3. shell脚本编写笔记

    包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...

  4. 【转】 C++中如何在一个构造函数中调用另一个构造函数

    在C++中,一个类的构造函数没法直接调用另一个构造函数,比如: #ifndef _A_H_ #define _A_H_ #include <stdio.h> #include <ne ...

  5. (二)学习C#之内存管理

    一.当你运行你的程序通常都会访问哪些内存空间呢? 电脑自言自语道,“这个人要声明一个整数”或“这个人个方法”或“这个人要创建一个对象” 1.这些信息究竟是存在内存里的什么地方呢? 2.或者说用于描述这 ...

  6. HttpListener supports SSL only for localhost? install certificate

    1.Start-All Programs - 2.execute below lines on that ‘Developer Command Prompt..’ tool makecert -n & ...

  7. Uva11732(trie)

    题意:给你n个字符串 用strcmp()两两比较 ,求字符比较的总次数 分析: 数据量很大我们考虑用孩子兄弟表示法来表示字典树 #include <cstdio> #include < ...

  8. POJ 1655-Balancing Act(树形dp)

    题意: 求n个节点的树中哪个节点删除以后得到的最大连通分量最小. 分析:同上题 #include <map> #include <set> #include <list& ...

  9. HOG学习(一)

    关于HOG里的特征维度 参考 http://gz-ricky.blogbus.com/logs/85326280.html http://blog.sina.com.cn/s/blog_7897fb6 ...

  10. poj 2104 K-th Number(主席树)

    Description You are working for Macrohard company in data structures department. After failing your ...