


1. You can visit the same node more than once and each time when you reach a special node your score will be increased by 1.
2. You cannot make a jump from node to itself.
The first line of input contains an integer ‘T’ denoting the number of test cases. The description of ‘T’ test cases are as follows -:
The first line contains two space-separated integers ‘N’, ‘K’ representing the number of nodes and the maximum number of horizontal jumps respectively.
The second line contains ‘N’ space-separated integers 0 or 1, representing the elements of array ‘special’.
Each of the next ‘N’-1 lines contains 2 space-separated integers ‘U’ and ‘V’, that represent there is an undirected edge between node ‘U’ and node ‘V’.
For each test case, print the maximum score you can achieve if you start jumping on a tree from root and can make at most ‘K’ horizontal jumps and any possible number of vertical jumps.
The output of each test case will be printed in a new line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
0 <= K <= 10
0 <= Special[i] <= 1
0 <= U, V < N
It is guaranteed that the given edges form a tree, also there will be no self loops and multiple edges.
Where ‘T’ is the number of test cases, ‘N’, ‘K’ representing the number of nodes and the maximum number of horizontal jumps respectively, and ‘Special[i]’ represents whether the ith node is special or not.
Time Limit: 1 sec
The idea is to first create an adjacency list and then use depth-first search or breadth-first search to create a 2D array ‘level’ where ‘level[i]’ is the list of all nodes present at height ‘i’, and an array ‘parent’ such that parent[i] gives the parent of the ith node. Then we will find out the maximum score recursively as follow -:
The idea is to first create an adjacency list and then use depth-first search or breadth-first search same as the previous approach in order to create an array of vectors ‘level’ where ‘level[i]’ is the list of all nodes present at height ‘i’, and an array ‘parent’ such that parent[i] gives the parent of the ith node. Then use another depth-first search to compute the maximum number of special nodes that can be reached using the following dp state.
Here, K is the number of horizontal jumps remaining.