Prison Break

3.5

2 votes
Problem

Scofield has been sentenced to death for a crime he never committed.Currenty he is in Fox River State Penitentiary.From his sources, he get to know that the prison is in the form of a rooted tree with n vertices, with guards at some vertices. His cell is at vertex 1 of the tree which is also the root vertex.All the leaves of the tree are the escape points of the prison(i.e. if anyhow, a prisoner manages to reach at any leaf vertex, he can escapes).

(NOTE: A rooted tree is a tree where there a particular vertex known as root.All the two vertices which share an edge follow a parent-child relationship.The one closer to the root is known as parent while the other one is known as child.A vertex with no children is called a leaf.)

Scofield thinks to devise an elaborate plan to escape prison and clear his name.He has already marked out what are the vertices with guards in them.Now he needs to choose a leaf vertex to escape. But there is a problem, there is no way he can reach the leaf vertex if the path from his cell(vertex 1) to leaf vertex contains more than K consecutive vertices with guards in them.

Your task is to help Scofield count the number of leaf vertices where he can go.

INPUT:
-----
The first line contains two integers, n and K (2 ≤ n ≤ 100000, 1 ≤ K ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with guards that is still ok for Scofield.

The second line contains n integers a1, a2, a3, ......., where each integer either equals to 0 (then vertex i has no guard), or equals to 1 (then vertex i has a guard).

Next n-1 lines contains the edges of the tree in the format " x  y " (without the quotes) (1 ≤ x , y ≤ n, x≠y), where x and y 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 Scofield's cell(vertex 1) contains at most K consecutive vertices with guards.

SAMPLE INPUT:
------------
4 1
1 1 0 0
1 2
1 3
1 4

SAMPLE OUTPUT:
-------------
2

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Given tree is:

             1
            /|\
           / | \
          /  |  \  
         2   3   4

guards are present at vertex 1 & 2.
escape points are present at 2,3 & 4.But he cannot go to vertex 2.
So, there are 2 feasible escape points.

Editor Image

?