史莱姆与查询AI (CI翻译)
You are given a tree with $n$ vertices numbered from $1$ to $n$. A slime occupies exactly $m$ vertices of the tree. It is guaranteed that the subgraph induced by the occupied vertices is connected.
给定一棵包含 $n$ 个顶点的树,顶点编号为 $1$ 到 $n$。史莱姆恰好占据树中的 $m$ 个顶点。保证由被占据顶点诱导的子图是连通的。AI (CI翻译)
Initially, the slime occupies vertices $s_1,s_2,\ldots,s_m$.
初始时,史莱姆占据顶点 $s_1,s_2,\ldots,s_m$。AI (CI翻译)
First, we define a function $f$ on a sequence of vertices. Consider a sequence $a_1,a_2,\ldots,a_k$. There are $k$ pieces of food. For each $i$, the $i$-th piece of food is located at vertex $a_i$. At first, only the first piece of food appears.
首先,我们定义一个作用于顶点序列的函数 $f$。考虑一个序列 $a_1,a_2,\ldots,a_k$。共有 $k$ 份食物。第 $i$ 份食物位于顶点 $a_i$。起初只有第一份食物出现。AI (CI翻译)
The slime may perform the following operations any number of times:
史莱姆可以执行以下操作任意次:AI (CI翻译)
- Move. The slime removes itself from one currently occupied vertex and expands to one currently unoccupied vertex.Formally, let
$S$be the current set of occupied vertices. Choose a vertex$u\in S$and a vertex$v\notin S$, and replace$S$with$(S\setminus{u})\cup{v}$. After the operation, the subgraph induced by$S$must still be connected. - Eat. If the
$i$-th piece of food has appeared and the slime currently occupies vertex$a_i$, then the slime may eat the$i$-th piece of food. If$1\le i \lt k$, the$(i+1)$-th piece of food appears immediately after that. Eating does not change the occupied vertices.
- 移动。史莱姆从一个当前占据的顶点移出,并扩展到一个当前未占据的顶点。形式化地,设
$S$为当前占据顶点的集合。选择一个顶点$u\in S$和一个顶点$v\notin S$,并将$S$替换为$(S\setminus{u})\cup{v}$。操作后,由$S$诱导的子图仍必须连通。 - 进食。如果第
$i$份食物已经出现,且史莱姆当前占据顶点$a_i$,那么史莱姆可以吃掉第$i$份食物。如果$1\le i \lt k$,则第$(i+1)$份食物会立即出现。进食不会改变被占据的顶点。
Define $f([a_1,a_2,\ldots,a_k])$ as the minimum number of Move operations needed for the slime to eat all $k$ pieces of food in order, starting from the initial occupied vertices $s_1,s_2,\ldots,s_m$.
将 $f([a_1,a_2,\ldots,a_k])$ 定义为:从初始占据顶点 $s_1,s_2,\ldots,s_m$ 开始,按顺序吃掉全部 $k$ 份食物所需的最少移动操作次数。AI (CI翻译)
There are $q$ queries. The input is forced online. The input gives encoded values $p_1,p_2,\ldots,p_q$. Let $\mathrm{ans}_0=0$. For each $i=1,2,\ldots,q$, the actual vertex of the $i$-th query is $c_i=((p_i-1+\mathrm{ans}_{i-1}) \bmod n)+1$, where $\mathrm{ans}_i=f([c_1,c_2,\ldots,c_i])$.
共有 $q$ 次查询。输入必须在线处理。输入给出编码后的值 $p_1,p_2,\ldots,p_q$。令 $\mathrm{ans}_0=0$。对于每个 $i=1,2,\ldots,q$,第 $i$ 次查询的实际顶点为 $c_i=((p_i-1+\mathrm{ans}_{i-1}) \bmod n)+1$,其中 $\mathrm{ans}_i=f([c_1,c_2,\ldots,c_i])$。AI (CI翻译)
For each $i=1,2,\ldots,q$, output $\mathrm{ans}_i$.
对于每个 $i=1,2,\ldots,q$,输出 $\mathrm{ans}_i$。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
输入包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来给出各测试用例的描述。AI (CI翻译)
The first line of each test case contains three integers $n$, $m$, and $q$ ($2\le m\le n\le 10^5$, $1\le q\le 10^5$) — the number of vertices in the tree, the number of vertices occupied by the slime, and the number of queries.
每个测试用例的第一行包含三个整数 $n$、$m$ 和 $q$($2\le m\le n\le 10^5$,$1\le q\le 10^5$)——树中的顶点数、史莱姆占据的顶点数以及查询次数。AI (CI翻译)
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1\le u,v\le n$, $u\ne v$), denoting an edge of the tree.
接下来的 $n-1$ 行每行包含两个整数 $u$ 和 $v$($1\le u,v\le n$,$u\ne v$),表示树中的一条边。AI (CI翻译)
The next line contains $m$ distinct integers $s_1,s_2,\ldots,s_m$ ($1\le s_i\le n$) — the vertices initially occupied by the slime. It is guaranteed that these vertices induce a connected subgraph.
下一行包含 $m$ 个互不相同的整数 $s_1,s_2,\ldots,s_m$($1\le s_i\le n$)——史莱姆初始占据的顶点。保证这些顶点诱导的子图是连通的。AI (CI翻译)
The next line contains $q$ integers $p_1,p_2,\ldots,p_q$ ($1\le p_i\le n$) — the encoded query vertices.
下一行包含 $q$ 个整数 $p_1,p_2,\ldots,p_q$($1\le p_i\le n$)——编码后的查询顶点。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
保证所有测试用例的 $n$ 之和不超过 $10^5$。AI (CI翻译)
It is guaranteed that the sum of $q$ over all test cases does not exceed $10^5$.
保证所有测试用例的 $q$ 之和不超过 $10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output $q$ integers. The $i$-th integer should be $\mathrm{ans}_i$.
对于每个测试用例,输出 $q$ 个整数。第 $i$ 个整数应为 $\mathrm{ans}_i$。AI (CI翻译)
Note
说明
AI (CI翻译)In the explanations below, the underlined vertex is the newly occupied vertex after a Move, and vertices where the slime eats food are written in bold.
在下面的说明中,下划线标出的顶点表示一次移动后新占据的顶点,而史莱姆进食食物的顶点用粗体表示。AI (CI翻译)

AI (CI翻译)
In the first test case, after decoding, the food appears at vertices $1,4,5$ in order. Initially, the slime occupies $[1,2]$.
在第一个测试用例中,解码后,食物依次出现在顶点 $1,4,5$。初始时,史莱姆占据 $[1,2]$。AI (CI翻译)
- For
$[1]$, the slime already occupies vertex$\mathbf{1}$, so$\mathrm{ans}_1=0$. - For
$[1,4]$, one optimal process is$[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]$, so$\mathrm{ans}_2=2$. - For
$[1,4,5]$, one optimal process is$[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]\to[3,\underline{\mathbf{5}}]$, so$\mathrm{ans}_3=3$.
- 对于
$[1]$,史莱姆已经占据顶点$\mathbf{1}$,因此$\mathrm{ans}_1=0$。 - 对于
$[1,4]$,一种最优过程为$[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]$,因此$\mathrm{ans}_2=2$。 - 对于
$[1,4,5]$,一种最优过程为$[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]\to[3,\underline{\mathbf{5}}]$,因此$\mathrm{ans}_3=3$。

AI (CI翻译)
In the second test case, after decoding, the food appears at vertices $5,3,6,2$ in order. Initially, the slime occupies $[1,2,3]$.
在第二个测试用例中,解码后,食物依次出现在顶点 $5,3,6,2$。初始时,史莱姆占据 $[1,2,3]$。AI (CI翻译)
- For
$[5]$, one optimal process is$[1,2,3]\to[1,3,\underline{\mathbf{5}}]$, so$\mathrm{ans}_1=1$. - For
$[5,3]$, the same process also lets the slime eat at vertex$\mathbf{3}$, so$\mathrm{ans}_2=1$. - For
$[5,3,6]$, one optimal process is$[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]$, so$\mathrm{ans}_3=2$. - For
$[5,3,6,2]$, one optimal process is$[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]\to[1,\underline{\mathbf{2}},3]$, so$\mathrm{ans}_4=3$.
- 对于
$[5]$,一种最优过程为$[1,2,3]\to[1,3,\underline{\mathbf{5}}]$,因此$\mathrm{ans}_1=1$。 - 对于
$[5,3]$,同样的过程也使史莱姆能够在顶点$\mathbf{3}$进食,因此$\mathrm{ans}_2=1$。 - 对于
$[5,3,6]$,一种最优过程为$[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]$,因此$\mathrm{ans}_3=2$。 - 对于
$[5,3,6,2]$,一种最优过程为$[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]\to[1,\underline{\mathbf{2}},3]$,因此$\mathrm{ans}_4=3$。

AI (CI翻译)
In the third test case, after decoding, the food appears at vertices $7,5,6,4,3$ in order. Initially, the slime occupies $[1,2,4]$.
在第三个测试用例中,解码后,食物依次出现在顶点 $7,5,6,4,3$。初始时,史莱姆占据 $[1,2,4]$。AI (CI翻译)
- For
$[7]$, one optimal process is$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]$, so$\mathrm{ans}_1=2$. - For
$[7,5]$, one optimal process is$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]$, so$\mathrm{ans}_2=4$. - For
$[7,5,6]$, one optimal process is$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{6}}]$, so$\mathrm{ans}_3=6$. - For
$[7,5,6,4]$, continue with$[1,3,\mathbf{6}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{4}}]$, so$\mathrm{ans}_4=8$. - For
$[7,5,6,4,3]$, continue with$[1,2,\mathbf{4}]\to[1,2,\underline{\mathbf{3}}]$, so$\mathrm{ans}_5=9$.
- 对于
$[7]$,一种最优过程为$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]$,因此$\mathrm{ans}_1=2$。 - 对于
$[7,5]$,一种最优过程为$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]$,因此$\mathrm{ans}_2=4$。 - 对于
$[7,5,6]$,一种最优过程为$[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{6}}]$,因此$\mathrm{ans}_3=6$。 - 对于
$[7,5,6,4]$,继续执行$[1,3,\mathbf{6}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{4}}]$,因此$\mathrm{ans}_4=8$。 - 对于
$[7,5,6,4,3]$,继续执行$[1,2,\mathbf{4}]\to[1,2,\underline{\mathbf{3}}]$,因此$\mathrm{ans}_5=9$。
In the fourth test case, after decoding, the food appears at vertices $3,4,5,2,1$.
在第四个测试用例中,解码后,食物依次出现在顶点 $3,4,5,2,1$。AI (CI翻译)
In the fifth test case, after decoding, the food appears at vertices $6,1,3,5,2,4$.
在第五个测试用例中,解码后,食物依次出现在顶点 $6,1,3,5,2,4$。AI (CI翻译)
In the sixth test case, after decoding, the food appears at vertices $7,5,6,1,4$.
在第六个测试用例中,解码后,食物依次出现在顶点 $7,5,6,1,4$。AI (CI翻译)
样例
10
5 2 3
3 5
2 1
4 3
3 2
1 2
1 4 3
6 3 4
5 1
1 3
6 1
4 1
2 1
1 2 3
5 2 5 6
7 3 5
3 7
4 2
1 3
2 1
6 3
5 2
1 2 4
7 3 2 5 2
5 2 5
3 1
1 5
2 1
4 1
1 2
3 3 3 4 2
6 3 6
4 6
3 2
1 2
5 4
2 4
2 4 5
6 6 1 2 4 6
7 4 5
5 2
3 1
2 1
3 7
6 3
4 2
1 2 3 4
7 4 4 5 1
4 3 4
3 1
1 4
2 1
1 2 3
4 1 2 3
6 2 5
2 4
5 4
2 1
6 4
3 2
1 2
6 1 1 1 2
7 2 5
2 4
7 3
3 6
1 3
1 2
5 2
1 2
4 6 1 6 5
8 4 6
5 2
3 2
7 5
4 3
8 7
1 2
6 5
2 3 4 5
8 7 3 3 7 8
0 2 3
1 1 2 3
2 4 6 8 9
1 2 3 4 4
1 2 3 4 4 4
1 2 3 3 4
1 1 2 2
2 4 6 8 9
1 4 7 10 11
2 3 4 4 5 5
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。