Tree and K-Tuple

5

3 votes
Algorithms, Approved, Graphs, Medium, Trees
Problem

Problem:
You have a Tree with N nodes rooted at 1 where node is associated with a value . Now consider the following definition:
K-Tuple: A tuple of nodes is a K-Tuple if:
1.) is an ancestor of
2.)

Calculate the number of K-Tuples in the tree.

Input:
First line contains two integers N and K, the number of nodes in the tree and the value for the Tuple type you need to calculate answer for.
Next line contains N space separated integers where the integer denotes the value .
Next lines contains X and Y denoting that there is an edge between them.

Output:
Output a single integer mod denoting the number of K-tuples in the tree.

Constraints:



Sample Input
7 2
7 6 5 4 3 2 1
1 2 
1 3 
2 4 
2 5 
3 6 
3 7
Sample Output
4
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

There are 4 2-Tuples: (1,2,4), (1,2,5), (1,3,6), (1,3,7).

Editor Image

?