Tree Subtree Divisibility

3.7

3 votes
Graphs, Algorithms, Depth First Search
Problem

Alice has received a beautiful tree as a gift from her math teacher. This tree has nodes connected by edges, and each node is assigned a value represented by .

Alice enjoys playing with this tree by cutting certain edges and dividing it into multiple subtrees. She has a favorite number , and she's curious to find out the number of ways she can cut the tree into subtrees so that the sum of values in each subtree is divisible by . The cutting order of the edges doesn't matter.

Help Alice solve this interesting problem by determining the count of good cuttings she can make when dividing the initial tree into subtree, subtrees, subtrees, and so on, up to subtrees.

  • A good cutting is a cutting in which sum of all values of each subtree is divisible by .

As the number of good cuttings can be huge you need to print the answer modulo .

 Input format

  • First line of the input contains an integer , representing the number of test cases.
  • The first line of each test case contains integers , .
  • The next line of each test case contains integers separated by a space, representing the array .
  • The next lines of each test case contains integers each, representing an edge of the tree.

Output format

  • For each test case, output integers separated by a space telling the count of good cuttings when dividing the tree into subtree, subtrees, subtrees, and so on, upto subtrees.

Constraints

  • It is guaranteed that sum of  over all test cases does not exceed
Sample Input
2
5 3
3 2 3 2 5
1 2
1 3
2 4
2 5
5 1
1 2 3 4 5
1 2
1 5
2 3
2 4
Sample Output
1 2 1 0 0 
1 4 6 4 1 
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case -: 

  • To divide the initial tree into 2 subtrees the possible set of edges which can be removed are -: 
    • {(1, 2)}
    • {(1, 3)}
  • To divide the initial tree into 3 subtrees the possible set of edges which can be removed are -: 
    • {(1, 2), (1, 3)}
Editor Image

?