PRETTYTREE
Problem
Let T be a rooted tree, not necessarily binary. We denote the root of T as and the number of nodes as . We say that T is beautiful if:
- it consists of a single node, or
- it can be formed by starting with two other beautiful trees and with and adding an edge such that the node becomes the parent of the node .
Thus, a beautiful tree of size can be constructed in the following way. We start with individual nodes, which by definition are beautiful trees. We repeat the following step times: we choose two different beautiful trees and from those we have, such that , add an edge so that the node becomes the parent of the node , and thus create a new beautiful tree.
This problem has two versions. In the first version, we are given a rooted tree and asked to determine if it is beautiful or not. In the second version, we are given a rooted tree and asked to count the number of different ways it can be constructed according to the described process (i.e., the number of different sequences in which the edges can be added). If the tree is not beautiful, it obviously cannot be constructed in any way, and thus the answer must be . The second version of the problem is a generalization of the first.
Input:
The first line of the input will contain two positive integers, and : the number of queries in this file and the version of the problem ( for the first version and for the second).
Following this are queries, each of which has the following format: The first line of the query contains a positive integer : the number of nodes in the tree. The next lines describe the edges of the tree: each line contains two positive integers and , indicating that node has node as its parent. We consider the nodes of the tree to be numbered from to .
Output:
The output should contain lines, one for each query. Each line should contain the answer to the respective version of the problem.
For the first version , the answer should be if the tree of the corresponding query is beautiful, otherwise .
For the second version (), the answer should be the number of different ways the tree of the corresponding query can be constructed according to the described procedure. Because this number can be very large, you are required to find its remainder (modulo) when divided by .
Constraints
- Time Limit: sec.
- Memory Limit: MB.
Examples of Input - Output:
1st
Input:
2 1
5
2 1
3 1
4 3
5 3
2
1 2
Output:
0
1
Explanation of the first example:
In the first example, , , so we have the first version of the problem and we need to determine if the two given trees are beautiful or not. The first tree cannot be constructed using the above process, therefore it is not beautiful. On the other hand, the second tree is beautiful - it can be constructed simply by making node the parent of node .
2nd
Input:
3 2
4
2 1
3 1
4 3
2
1 2
5
2 1
3 1
4 3
5 3
Output:
2
1
0
Explanation of the second example:
In the second example, and , indicating the second version of the problem. We need to find out how many different ways the given trees can be formed. The first tree provided is beautiful and can be constructed in two different ways by adding its edges in the following sequences:
- , ,
- , ,
The second tree in the second example can be formed in only one way, while the third tree is not beautiful and cannot be formed in any way.
Notes
- For test cases with a total value of , it will be and
- For test cases with a total value of , it will be and
- For test cases with a total value of , it will be and
- For test cases with a total value of , it will be and . Each node of any tree will have at most children.
- For test cases with a total value of , it will be and . Each node of any tree will have at most children.
- For test cases with a total value of , it will be and . Each node of any tree will have at most children.
- For test cases with a total value of , it will be and . Each node of any tree will have at most children.
Comments