#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 NN vertices, where each vertex is written with a number 00 or 11.

Ryan's friend also has a rooted tree, where each vertex is written with a number 00 or 11. Let the number of vertices in this tree be MM.

Ryan wants to arrange these N+MN + M 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 XX be the sequence obtained by reading the numbers written on the vertices from left to right. Ryan wants to minimize the inversion number of XX. Find the minimum possible inversion number of XX.

Since Ryan has QQ 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 NN (1N2000001 \leq N \leq 200\,000) representing the number of vertices in Ryan's tree.

The second line contains N1N - 1 integers. Each PiP_i (2iN2 \leq i \leq N, 1Pi<i1 \leq P_i < i) represents that the parent of vertex ii is vertex PiP_i. Note that vertex 11 is the root of the tree and P1P_1 is not given.

The third line contains NN integers. Each ViV_i (1iN1 \leq i \leq N, 0Vi10 \leq V_i \leq 1) represents that the number written on vertex ii.

The fourth line contains an integer QQ (1Q1000001 \leq Q \leq 100\,000) representing the number of Ryan's friends.

For each friend kk (1kQ1 \leq k \leq Q), the input for their tree is given in the following format:

  • The first line contains an integer MkM_k (1Mk1 \leq M_k), representing the number of vertices in the kk-th friend's tree.
  • The second line contains Mk1M_k - 1 integers. Each Pk,iP_{k,i} (2iMk2 \leq i \leq M_k, 1Pk,i<i1 \leq P_{k,i} < i) represents that the parent of vertex ii is vertex Pk,iP_{k,i}. Note that vertex 11 is the root of the tree and Pk,1P_{k,1} is not given.
  • The third line contains MkM_k integers. Each Uk,iU_{k,i} (1iMk1 \leq i \leq M_k, 0Uk,i10 \leq U_{k,i} \leq 1) represents the encrypted number written on vertex ii of the kk-th friend's tree.

Additionally, the sum of the MkM_k (1kQ1 \leq k \leq Q) does not exceed 200000200\,000.

Decrypting the Numbers on the Vertices of Ryan’s Friends' Trees

Let X0=0X_0 = 0, and for each friend kk (1kQ1 \leq k \leq Q), let XkX_k denote the answer for the kk-th friend. The actual value Vk,iV_{k,i} (1kQ1 \leq k \leq Q, 1iMk1 \leq i \leq M_k, 0Vk,i10 \leq V_{k,i} \leq 1) written on vertex ii of the kk-th friend's tree is determined as follows:

$$V_{k,i} = (\text{powmod}(X_{k-1}, i, 998244353) + U_{k,i}) \mod 2. $$

Here, powmod(a,b,m)\text{powmod}(a, b, m) denotes (abmodm)(a^b \mod m).

输出格式

Print QQ lines. The ii-th line should contain a single integer, representing the minimum possible inversion number for Ryan and his ii-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