#P8280. 「MCOI-08」Photoelectric Effect
「MCOI-08」Photoelectric Effect
Description
There is a tree with () nodes and () colors, with node as the root. Meanwhile, a color merge function is given, satisfying that for , we have .
How many ways are there to color all nodes such that for any pair of nodes that have no ancestor relationship, the following holds:
- The color of equals the merge of the color of node and the color of node .
Output the answer modulo .
Input Format
This problem contains multiple test cases. The first line contains a positive integer (), the number of test cases. Then follow test cases, and for each test case:
The first line contains two positive integers .
The next lines each contain positive integers. The -th number in the -th line is the value of .
The next line contains positive integers , where is the parent of node .
Output Format
For each test case:
Output one integer, representing the answer.
2
5 2
1 2
2 1
1 2 1 4
5 2
1 2
1 1
1 2 1 4
4
2
Hint
Explanation for Sample 1
The tree looks as follows:

Let be the weight of node . Then there are the following assignment methods:
- ;
- ;
- ;
- .
Constraints and Notes
This problem uses bundled testdata.
For of the testdata, , , and .
For of the testdata, .
- Subtask 1 (5 pts): ;
- Subtask 2 (11 pts): each node has at most children in the tree;
- Subtask 3 (23 pts): each node has at most children in the tree;
- Subtask 4 (13 pts): ;
- Subtask 5 (17 pts): ;
- Subtask 6 (31 pts): no special constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号