#P11039. 【MX-X3-T6】「RiOI-4」TECHNOPOLIS 2085
【MX-X3-T6】「RiOI-4」TECHNOPOLIS 2085
Background
Original link: https://oier.team/problems/X3G。

(The image is from a Phigros song illustration. Please contact for removal if needed.)
Ah~ ah~ ah yi~ ah yi~ oh→ ah↑ ah↓ ah~ um~ ai ai↑ ai oh ai um~ oh ah~ love↖ love↑ love↗ love↑ love↑ ah~ ah~ ah yi~ ah yi~ ah→ ah↑ ah↓ ah~ um um↓ um↓ di de di de wu↑ ~~ du← du↖️ du↑ du↗️ du→ du↘️ du↓
You are right, but this is a Dream Bear weekly contest problem, not a place for the setter to "generate electricity", so you need to solve a graph theory problem.
Problem Description
By convention, denotes the lowest common ancestor of nodes with labels in a labeled rooted tree . You are given a rooted tree with root label , size , and node labels , as well as a node set of size . You need to find how many distinct labeled rooted trees of size with node labels satisfy that for any , we have $\operatorname{lca}_T(u,v)=\operatorname{lca}_{T'}(u,v)$.
Output the answer modulo .
We say two labeled rooted trees of size are different if and only if at least one of the following holds:
- Their root nodes are different.
- There exists an edge that appears in one tree but not in the other.
Input Format
The first line contains two positive integers .
The second line contains positive integers , where is the parent label of node for .
The last line contains positive integers, indicating the labels of the selected nodes in the set .
Output Format
One line with one integer, the answer modulo .
5 3
1 1 2 2
3 4 5
1
5 3
1 1 2 2
2 3 4
8
20 10
20 8 7 16 3 15 1 10 17 3 13 15 1 17 1 14 14 8 4
3 7 10 19 15 13 4 6 18 5
553508927
Hint
Sample Explanation #1
Only the tree that is exactly the same as satisfies the requirement.
Sample Explanation #2
For sample 2, all valid arrays are as follows (the root has ):
$\{0,1,1,2,1\},\{0,1,1,2,2\},\{0,1,1,2,3\},\{0,1,1,2,4\},$
$\{0,1,1,5,2\},\{0,5,1,2,1\},\{0,1,5,2,1\},\{5,1,1,2,0\}$。
Constraints
This problem uses bundled testdata.
| Subtask | Score | ||
|---|---|---|---|
For of the data, . It is guaranteed that the input describes a labeled rooted tree, and describes a set of nodes.
Translated by ChatGPT 5