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 ith node is associated with a value Ai. Now consider the following definition:
K-Tuple: A tuple of K+1 nodes (X1,X2,X3,,XK+1) is a K-Tuple if:
1.) Xi is an ancestor of Xi+11iK
2.) AXi>AXi+11iK

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 ith integer denotes the value Ai.
Next N1 lines contains X and Y denoting that there is an edge between them.

Output:
Output a single integer mod (109+7) denoting the number of K-tuples in the tree.

Constraints:
1N,Ai100000
1X,YN
1K20

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

?