Friendship

5

1 votes
Hard
Problem

There are people living in  houses in the city of Treeland. The houses in the city of Treeland form a tree-like structure with  as the root. You are given the structure of the city in the form of a tree with the houses as nodes. You are also given the ages of people living in each of the houses.

As we all know friendships form between people of almost equal ages. More formally, two person will become friends if and only if the absolute difference between their ages is no more than .

You need to find the number of pairs of people who become friends within each of the  subtrees, i.e, for each subtree with  as root, you need to find the number of unordered pairs of integers  and  , such that  and  both belong to the subtree rooted at and become friends.

Input Format

The first line contains two integers  and , the number of people/houses in Treeland and the maximum possible difference between the ages of two people who can be friends. The second line contains  space separated integers, where the  integer,  denotes the age of the person living in . The next  lines contains the structure of Treeland in the form of  edges. The  edge is represented by two integers  and , denoting an edge between  and .

Output Format

Print a single line containing  space separated integers integers, where the  integer is the answer for the subtree rooted at .

Constraints

Time Limit: 1.5
Memory Limit: 256
Source Limit:
Editor Image

?