#P15774. [JAG 2025 Summer Camp #2] Double 01 on Tree
[JAG 2025 Summer Camp #2] Double 01 on Tree
题目描述
Ryan has a rooted tree with vertices, where each vertex is written with a number or .
Ryan's friend also has a rooted tree, where each vertex is written with a number or . Let the number of vertices in this tree be .
Ryan wants to arrange these vertices in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex. Note that there are no constraints between the vertices in Ryan's tree and the vertices in his friend's tree.
After arranging the vertices, let be the sequence obtained by reading the numbers written on the vertices from left to right. Ryan wants to minimize the inversion number of . Find the minimum possible inversion number of .
Since Ryan has friends, solve the above problem for each of his friends.
The numbers written on the vertices of Ryan's friends' trees are given in an encrypted form. See the bottom of the Input section for details.
输入格式
The input consists of a single test case of the following format.
$$\begin{aligned} & N \\ & P_2 \ P_3 \ \ldots \ P_N \\ & V_1 \ V_2 \ \ldots \ V_N \\ & Q \\ & M_1 \\ & P_{1,2} \ P_{1,3} \ \ldots \ P_{1,M_1} \\ & U_{1,1} \ U_{1,2} \ \ldots \ U_{1,M_1} \\ & M_2 \\ & P_{2,2} \ P_{2,3} \ \ldots \ P_{2,M_2} \\ & U_{2,1} \ U_{2,2} \ \ldots \ U_{2,M_2} \\ & \vdots \\ & M_Q \\ & P_{Q,2} \ P_{Q,3} \ \ldots \ P_{Q,M_Q} \\ & U_{Q,1} \ U_{Q,2} \ \ldots \ U_{Q,M_Q} \end{aligned} $$The first line contains an integer () representing the number of vertices in Ryan's tree.
The second line contains integers. Each (, ) represents that the parent of vertex is vertex . Note that vertex is the root of the tree and is not given.
The third line contains integers. Each (, ) represents that the number written on vertex .
The fourth line contains an integer () representing the number of Ryan's friends.
For each friend (), the input for their tree is given in the following format:
- The first line contains an integer (), representing the number of vertices in the -th friend's tree.
- The second line contains integers. Each (, ) represents that the parent of vertex is vertex . Note that vertex is the root of the tree and is not given.
- The third line contains integers. Each (, ) represents the encrypted number written on vertex of the -th friend's tree.
Additionally, the sum of the () does not exceed .
Decrypting the Numbers on the Vertices of Ryan’s Friends' Trees
Let , and for each friend (), let denote the answer for the -th friend. The actual value (, , ) written on vertex of the -th friend's tree is determined as follows:
$$V_{k,i} = (\text{powmod}(X_{k-1}, i, 998244353) + U_{k,i}) \mod 2. $$Here, denotes .
输出格式
Print lines. The -th line should contain a single integer, representing the minimum possible inversion number for Ryan and his -th friend.
6
1 1 2 3 3
0 1 1 0 0 0
4
4
1 2 2
1 0 1 0
6
1 1 2 3 3
1 1 0 0 1 0
1
0
15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 0
9
7
4
42
提示
The decrypted values corresponding to this sample are shown below
6
1 1 2 3 3
0 1 1 0 0 0
4
4
1 2 2
1 0 1 0
6
1 1 2 3 3
0 0 1 1 0 1
1
1
15
1 2 3 2 5 6 2 2 9 10 1 12 13 12
1 1 1 0 1 1 0 0 1 0 0 1 1 0 1