#P17025. [ROI 2026 Day2] 好的染色 - 8

[ROI 2026 Day2] 好的染色 - 8

Problem Description

Ildar decided to devote himself to abstract art. He chose a rooted tree with nn vertices as the basis of his painting: it is an acyclic graph in which vertex 11 is designated as the root. The root has no parent; for any other vertex u≥2u \ge 2, the first vertex on the path from uu to the root is called the parent of uu, denoted by pup_u. Vertices whose parent is vv are called the children of vv. A vertex with no children is called a leaf. It is guaranteed that the root has at least two children.

We perform a depth-first traversal of the tree: first visit the root, then recursively visit the subtrees of its children in order in the same way. The vertices of the tree are numbered in the order of this depth-first traversal. Therefore, for each ii from 11 to nn, the vertex numbers in the subtree of vertex ii form a contiguous interval of integers.

Let the tree have mm leaves. Ildar writes them out in increasing order of their indices to get the sequence l1<l2<…<lml_1 < l_2 < \ldots < l_m, and adds edges connecting every pair of leaves of the form (lj,lj+1)(l_j, l_{j+1}), and also connects lml_m and l1l_1. The added cycle l1→l2→…→lm→l1l_1 \to l_2 \to \ldots \to l_m \to l_1 is called the outer cycle.

Ildar draws the resulting graph on the plane as follows: the outer cycle is drawn as a circle, and the leaves l1,l2,…,lml_1, l_2, \ldots, l_m are placed counterclockwise along the circumference. The arcs between adjacent vertices on the circumference represent the edges of the outer cycle. The other vertices of the tree are represented as distinct points inside the circle. The tree edges are drawn as line segments between their endpoints, and the positions of vertices and edges are such that the edge segments have no common interior points. The figure below shows one possible drawing of the tree.

:::align{center} :::

In Ildar's drawing, the part of the plane inside the outer cycle is divided by the edges of the graph into mm regions, called faces. If two different faces share an edge, they are called adjacent faces. For example, the drawing above produces 5 faces, denoted by Γ1,Γ2,Γ3,Γ4\Gamma_1, \Gamma_2, \Gamma_3, \Gamma_4 and Γ5\Gamma_5.

:::align{center} :::

In the figure above, the adjacent face pairs are (Γ1,Γ2)(\Gamma_1, \Gamma_2), (Γ1,Γ5)(\Gamma_1, \Gamma_5), (Γ2,Γ3)(\Gamma_2, \Gamma_3), (Γ2,Γ4)(\Gamma_2, \Gamma_4), (Γ2,Γ5)(\Gamma_2, \Gamma_5), (Γ3,Γ4)(\Gamma_3, \Gamma_4), and (Γ4,Γ5)(\Gamma_4, \Gamma_5).

To finish the painting, Ildar plans to color each face with one of kk colors. A coloring is called proper if adjacent faces are colored with different colors. Ildar calls the number of different proper colorings of the drawing modulo 109+710^9+7 the potential of the drawing.

After evaluating the potential of the initial drawing, Ildar performs qq operations on the edges of the graph. Consider the ii-th operation: it is given by a number viv_i and acts on the tree edge connecting vertex viv_i and pvip_{v_i}. If this edge is currently visible in the drawing, Ildar deletes it from the drawing; if this edge is currently not in the drawing, he draws it again. After each modification, the set of faces in the drawing may change: when an edge is deleted, two faces may merge into one; when an edge is drawn, one face may split into two. For example, if we delete the edge 8−98 - 9 in the figure above, then faces Γ4\Gamma_4 and Γ5\Gamma_5 merge into a single face Γ4+5\Gamma_{4+5}.

:::align{center} :::

Now the adjacent face pairs are (Γ1,Γ2)(\Gamma_1, \Gamma_2), (Γ1,Γ4+5)(\Gamma_1, \Gamma_{4+5}), (Γ2,Γ3)(\Gamma_2, \Gamma_3), (Γ2,Γ4+5)(\Gamma_2, \Gamma_{4+5}), and (Γ3,Γ4+5)(\Gamma_3, \Gamma_{4+5}).

After each operation, you need to determine the potential of the drawing again, i.e., the number of proper colorings of the faces using at most kk colors modulo 109+710^9+7.

Input Format

The first line contains an integer tt (1≤t≤10 0001 \le t \le 10\,000), the number of testdata sets. The description of the tt testdata sets follows.

The first line of each testdata set contains three integers nn, kk, and qq (3≤n≤1063 \le n \le 10^6, 2≤k≤1092 \le k \le 10^9, 0≤q≤300 0000 \le q \le 300\,000), denoting the number of vertices in the tree, the number of available colors, and the number of operations performed.

The second line of each testdata set contains p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<i1 \le p_i < i), where pip_i is the parent of vertex ii in the tree. It is guaranteed that the vertices are numbered in depth-first traversal order, and that the value 11 appears at least twice in p2,…,pnp_2, \ldots, p_n.

Then follow qq lines, where the ii-th line contains an integer viv_i (2≤vi≤n2 \le v_i \le n), denoting the parameter of the ii-th operation.

It is guaranteed that across all testdata sets, the sum of nn does not exceed 10610^6, and the sum of qq does not exceed 300 000300\,000.

Output Format

Output q+1q+1 numbers. The first number is the potential of the initial drawing, and the remaining numbers are the potential of the drawing after each operation.

2
3 4 5
1 1
2
3
2
3
3
9 4 8
1 2 2 1 5 5 1 8
9
8
3
5
4
3
9
8
12
4
4
4
12
4
96
48
48
24
12
12
12
12
36

Hint

Subtasks

Define the height of the tree as the maximum number of edges on a simple path from the root to any other vertex.

Subtask Score nn kk qq Additional constraints Depends on subtasks
1 6 n=3n = 3 k≤4k \le 4 q≤10q \le 10 t≤100t \le 100, p2=p3=1p_2 = p_3 = 1
2 9 ∑n≤1 000\sum n \le 1\,000 -- q=0q = 0 pi=2⋅⌊i2⌋−1p_{i} = 2 \cdot \lfloor \frac{i}{2} \rfloor - 1, nn is odd
3 10 ∑q≤1 000\sum q \le 1\,000 pi=1p_i = 1 1
4 n≤9n \le 9 k≤4k \le 4 q=0q = 0 t≤100t \le 100
5 3 q≤10q \le 10 4
6 2 ∑n≤1 000\sum n \le 1\,000 k=2k=2 q=0q = 0 --
7 11 -- 2, 4, 6
8 15 ∑q≤1 000\sum q \le 1\,000 1–7
9 4 ∑n≤5 000\sum n \le 5\,000 ∑q≤5 000\sum q \le 5\,000 1–8
10 3 ∑n≤10 000\sum n \le 10\,000 ∑q≤10 000\sum q \le 10\,000 1–9
11 6 ∑n≤100 000\sum n \le 100\,000 ∑q≤5 000\sum q \le 5\,000
12 7 ∑q≤100 000\sum q \le 100\,000 Height does not exceed 2020 1, 4, 5
13 14 -- 1–12
14 3 ∑n≤300 000\sum n \le 300\,000 ∑q≤300 000\sum q \le 300\,000 1–13
15 ∑n≤1 000 000\sum n \le 1\,000\,000 1–14

Translated by DeepSeek V4 Pro.

Translated by ChatGPT 5