Kefa and Park CodeForces - 580C

Posted xxxsans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kefa and Park CodeForces - 580C相关的知识,希望对你有一定的参考价值。

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 ≤ 1051 ≤ 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 a 1, a 2, ..., a n, where each a i 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 " x i y i" (without the quotes) (1 ≤ x i, y i ≤ nx i ≠ y i), where x i and y i 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.

题意:一棵以1为根的树,树上有些点是红的。一个叶子是合法的当且仅当从根到它的路径上出现的连续红点个数不超过m。求有多少个叶子是合法的。Input
第一行两个整数n和m(2≤n ≤105,1≤m≤n)
第二行n个整数0或1,如果是1,表示第i个点是红点。
接下来n-1行,每行两个整数x和y,表示树上的一条边。Output输出满足条件的叶子节点数

题解:从根开始沿着树下走,附加参数k -猫连续碰到的顶点数。如果k超过m,则离开。那么答案就是叶子能够达到的数量。

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const long long N=228228;
vector<long long> a[N];
long long c[N],o,x,y,i,j,n,m;
void go(int v,int pr,int k){
    if(k>m)return;
    int ok=1;
    for(int i=0;i<a[v].size();i++)if(a[v][i]!=pr)ok=0,go(a[v][i],v,k*c[a[v][i]]+c[a[v][i]]);
    o+=ok;
}
int main(){
    cin>>n>>m;
    for(i=0;i<n;i++)scanf("%d",&c[i]);
    for(i=1;i<n;i++)scanf("%d%d",&x,&y),x--,y--,a[x].pb(y),a[y].pb(x);
    go(0,-1,c[0]);
    cout<<o<<"
";
}

 

以上是关于Kefa and Park CodeForces - 580C的主要内容,如果未能解决你的问题,请参考以下文章

C. Kefa and Park

CF580C Kefa and Park dfs

C. Kefa and Park树的遍历

Kefa and Company CodeForces - 580B

codeforces 580D:Kefa and Dishes

[2016-03-23][codeforces][580][A][Kefa and First Steps]