CF2237H.Slime and Queries

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices numbered from 11 to nn. A slime occupies exactly mm vertices of the tree. It is guaranteed that the subgraph induced by the occupied vertices is connected.

Initially, the slime occupies vertices s1,s2,…,sms_1,s_2,\ldots,s_m.

First, we define a function ff on a sequence of vertices. Consider a sequence a1,a2,…,aka_1,a_2,\ldots,a_k. There are kk pieces of food. For each ii, the ii-th piece of food is located at vertex aia_i. At first, only the first piece of food appears.

The slime may perform the following operations any number of times:

  • Move. The slime removes itself from one currently occupied vertex and expands to one currently unoccupied vertex.

    Formally, let SS be the current set of occupied vertices. Choose a vertex u∈Su\in S and a vertex v∉Sv\notin S, and replace SS with (S∖u)∪v(S\setminus{u})\cup{v}. After the operation, the subgraph induced by SS must still be connected.

  • Eat. If the ii-th piece of food has appeared and the slime currently occupies vertex aia_i, then the slime may eat the ii-th piece of food. If 1≤i<k1\le i \lt k, the (i+1)(i+1)-th piece of food appears immediately after that. Eating does not change the occupied vertices.

Define f([a1,a2,…,ak])f([a_1,a_2,\ldots,a_k]) as the minimum number of Move operations needed for the slime to eat all kk pieces of food in order, starting from the initial occupied vertices s1,s2,…,sms_1,s_2,\ldots,s_m.

There are qq queries. The input is forced online. The input gives encoded values p1,p2,…,pqp_1,p_2,\ldots,p_q. Let ans0=0\mathrm{ans}_0=0. For each i=1,2,…,qi=1,2,\ldots,q, the actual vertex of the ii-th query is ci=((pi−1+ansi−1) mod n)+1c_i=((p_i-1+\mathrm{ans}_{i-1}) \bmod n)+1, where ansi=f([c1,c2,…,ci])\mathrm{ans}_i=f([c_1,c_2,\ldots,c_i]).

For each i=1,2,…,qi=1,2,\ldots,q, output ansi\mathrm{ans}_i.

给你一棵有 nn 个顶点的树,顶点编号为 11 到 nn。一只史莱姆恰好占据该树中的 mm 个顶点。保证这些被占据顶点所诱导出的子图是连通的。

初始时,史莱姆占据顶点 s1,s2,…,sms_1,s_2,\ldots,s_m。

首先,我们定义一个作用于顶点序列的函数 ff。考虑一个顶点序列 a1,a2,…,aka_1,a_2,\ldots,a_k。共有 kk 份食物;其中第 ii 份食物位于顶点 aia_i。最初,仅有第 11 份食物出现。

史莱姆可任意多次执行以下两种操作:

  • 移动(Move):史莱姆从当前被占据的一个顶点上撤离,并扩展至一个当前未被占据的顶点。
    形式化地,设 SS 为当前被占据顶点集合。选择一个顶点 u∈Su\in S 和一个顶点 v∉Sv\notin S,并将 SS 替换为 (S∖{u})∪{v}(S\setminus\{u\})\cup\{v\}。操作后,SS 所诱导的子图仍必须保持连通。

  • 进食(Eat):若第 ii 份食物已出现,且史莱姆当前正占据顶点 aia_i,则史莱姆可吃掉第 ii 份食物。若 1≤i<k1\le i < k,则在吃掉第 ii 份食物后,第 (i+1)(i+1) 份食物会立即出现。进食操作不改变被占据的顶点集合。

定义 f([a1,a2,…,ak])f([a_1,a_2,\ldots,a_k]) 为:从初始占据顶点集 s1,s2,…,sms_1,s_2,\ldots,s_m 出发,按顺序吃完全部 kk 份食物所需的最少 移动(Move) 操作次数。

共有 qq 个查询,输入强制在线。输入给出编码后的值 p1,p2,…,pqp_1,p_2,\ldots,p_q。令 ans0=0\mathrm{ans}_0=0。对每个 i=1,2,…,qi=1,2,\ldots,q,第 ii 个查询对应的实际顶点为

ci=((pi−1+ansi−1) mod n)+1,c_i = ((p_i - 1 + \mathrm{ans}_{i-1}) \bmod n) + 1,

其中 ansi=f([c1,c2,…,ci])\mathrm{ans}_i = f([c_1,c_2,\ldots,c_i])。

对每个 i=1,2,…,qi = 1,2,\ldots,q,输出 ansi\mathrm{ans}_i。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains three integers nn, mm, and qq (2≤m≤n≤1052\le m\le n\le 10^5, 1≤q≤1051\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.

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1\le u,v\le n, u≠vu\ne v), denoting an edge of the tree.

The next line contains mm distinct integers s1,s2,…,sms_1,s_2,\ldots,s_m (1≤si≤n1\le s_i\le n) — the vertices initially occupied by the slime. It is guaranteed that these vertices induce a connected subgraph.

The next line contains qq integers p1,p2,…,pqp_1,p_2,\ldots,p_q (1≤pi≤n1\le p_i\le n) — the encoded query vertices.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

It is guaranteed that the sum of qq over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含三个整数 nn、mm 和 qq(2≤m≤n≤1052\le m\le n\le 10^5,1≤q≤1051\le q\le 10^5)—— 分别表示树中的顶点数、被史莱姆占据的顶点数以及查询次数。

接下来的 n−1n-1 行每行包含两个整数 uu 和 vv(1≤u,v≤n1\le u,v\le n,u≠vu\ne v),表示树中的一条边。

下一行包含 mm 个互不相同的整数 s1,s2,…,sms_1,s_2,\ldots,s_m(1≤si≤n1\le s_i\le n)—— 表示史莱姆初始占据的顶点。保证这些顶点构成一个连通子图。

再下一行包含 qq 个整数 p1,p2,…,pqp_1,p_2,\ldots,p_q(1≤pi≤n1\le p_i\le n)—— 表示经过编码的查询顶点。

保证所有测试用例的 nn 之和不超过 10510^5。

保证所有测试用例的 qq 之和不超过 10510^5。

输出格式

For each test case, output qq integers. The ii-th integer should be ansi\mathrm{ans}_i.

对于每个测试用例,输出 qq 个整数。第 ii 个整数应为 ansi\mathrm{ans}_i。

输入输出样例

  • 输入#1

    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

    输出#1

    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

说明/提示

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.

In the first test case, after decoding, the food appears at vertices 1,4,51,4,5 in order. Initially, the slime occupies [1,2][1,2].

  1. For [1][1], the slime already occupies vertex 1\mathbf{1}, so ans1=0\mathrm{ans}_1=0.
  2. For [1,4][1,4], one optimal process is [1,2]→[2,3‾]→[3,4‾][\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}], so ans2=2\mathrm{ans}_2=2.
  3. For [1,4,5][1,4,5], one optimal process is [1,2]→[2,3‾]→[3,4‾]→[3,5‾][\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]\to[3,\underline{\mathbf{5}}], so ans3=3\mathrm{ans}_3=3.

In the second test case, after decoding, the food appears at vertices 5,3,6,25,3,6,2 in order. Initially, the slime occupies [1,2,3][1,2,3].

  1. For [5][5], one optimal process is [1,2,3]→[1,3,5‾][1,2,3]\to[1,3,\underline{\mathbf{5}}], so ans1=1\mathrm{ans}_1=1.
  2. For [5,3][5,3], the same process also lets the slime eat at vertex 3\mathbf{3}, so ans2=1\mathrm{ans}_2=1.
  3. For [5,3,6][5,3,6], one optimal process is [1,2,3]→[1,3,5‾]→[1,3,6‾][1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}], so ans3=2\mathrm{ans}_3=2.
  4. For [5,3,6,2][5,3,6,2], one optimal process is [1,2,3]→[1,3,5‾]→[1,3,6‾]→[1,2‾,3][1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]\to[1,\underline{\mathbf{2}},3], so ans4=3\mathrm{ans}_4=3.

In the third test case, after decoding, the food appears at vertices 7,5,6,4,37,5,6,4,3 in order. Initially, the slime occupies [1,2,4][1,2,4].

  1. For [7][7], one optimal process is [1,2,4]→[1,2,3‾]→[1,3,7‾][1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}], so ans1=2\mathrm{ans}_1=2.
  2. For [7,5][7,5], one optimal process is [1,2,4]→[1,2,3‾]→[1,3,7‾]→[1,2‾,3]→[1,2,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}}], so ans2=4\mathrm{ans}_2=4.
  3. For [7,5,6][7,5,6], one optimal process is [1,2,4]→[1,2,3‾]→[1,3,7‾]→[1,2‾,3]→[1,2,5‾]→[1,2,3‾]→[1,3,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}}], so ans3=6\mathrm{ans}_3=6.
  4. For [7,5,6,4][7,5,6,4], continue with [1,3,6]→[1,2‾,3]→[1,2,4‾][1,3,\mathbf{6}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{4}}], so ans4=8\mathrm{ans}_4=8.
  5. For [7,5,6,4,3][7,5,6,4,3], continue with [1,2,4]→[1,2,3‾][1,2,\mathbf{4}]\to[1,2,\underline{\mathbf{3}}], so ans5=9\mathrm{ans}_5=9.

In the fourth test case, after decoding, the food appears at vertices 3,4,5,2,13,4,5,2,1.

In the fifth test case, after decoding, the food appears at vertices 6,1,3,5,2,46,1,3,5,2,4.

In the sixth test case, after decoding, the food appears at vertices 7,5,6,1,47,5,6,1,4.

在以下说明中,下划线标注的顶点表示一次“移动”(Move)后新占据的顶点,而黏液吃掉食物的顶点以粗体标出。

第一个测试用例中,解码后食物依次出现在顶点 1,4,51,4,5。初始时,黏液占据 [1,2][1,2]。

  1. 对于 [1][1],黏液已占据顶点 1\mathbf{1},因此 ans1=0\mathrm{ans}_1=0。
  2. 对于 [1,4][1,4],一种最优过程为 [1,2]→[2,3‾]→[3,4‾][\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}],因此 ans2=2\mathrm{ans}_2=2。
  3. 对于 [1,4,5][1,4,5],一种最优过程为 [1,2]→[2,3‾]→[3,4‾]→[3,5‾][\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]\to[3,\underline{\mathbf{5}}],因此 ans3=3\mathrm{ans}_3=3。

第二个测试用例中,解码后食物依次出现在顶点 5,3,6,25,3,6,2。初始时,黏液占据 [1,2,3][1,2,3]。

  1. 对于 [5][5],一种最优过程为 [1,2,3]→[1,3,5‾][1,2,3]\to[1,3,\underline{\mathbf{5}}],因此 ans1=1\mathrm{ans}_1=1。
  2. 对于 [5,3][5,3],上述相同过程也使黏液在顶点 3\mathbf{3} 吃到食物,因此 ans2=1\mathrm{ans}_2=1。
  3. 对于 [5,3,6][5,3,6],一种最优过程为 [1,2,3]→[1,3,5‾]→[1,3,6‾][1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}],因此 ans3=2\mathrm{ans}_3=2。
  4. 对于 [5,3,6,2][5,3,6,2],一种最优过程为 [1,2,3]→[1,3,5‾]→[1,3,6‾]→[1,2‾,3][1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]\to[1,\underline{\mathbf{2}},3],因此 ans4=3\mathrm{ans}_4=3。

第三个测试用例中,解码后食物依次出现在顶点 7,5,6,4,37,5,6,4,3。初始时,黏液占据 [1,2,4][1,2,4]。

  1. 对于 [7][7],一种最优过程为 [1,2,4]→[1,2,3‾]→[1,3,7‾][1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}],因此 ans1=2\mathrm{ans}_1=2。
  2. 对于 [7,5][7,5],一种最优过程为 [1,2,4]→[1,2,3‾]→[1,3,7‾]→[1,2‾,3]→[1,2,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}}],因此 ans2=4\mathrm{ans}_2=4。
  3. 对于 [7,5,6][7,5,6],一种最优过程为 [1,2,4]→[1,2,3‾]→[1,3,7‾]→[1,2‾,3]→[1,2,5‾]→[1,2,3‾]→[1,3,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}}],因此 ans3=6\mathrm{ans}_3=6。
  4. 对于 [7,5,6,4][7,5,6,4],继续执行 [1,3,6]→[1,2‾,3]→[1,2,4‾][1,3,\mathbf{6}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{4}}],因此 ans4=8\mathrm{ans}_4=8。
  5. 对于 [7,5,6,4,3][7,5,6,4,3],继续执行 [1,2,4]→[1,2,3‾][1,2,\mathbf{4}]\to[1,2,\underline{\mathbf{3}}],因此 ans5=9\mathrm{ans}_5=9。

第四个测试用例中,解码后食物依次出现在顶点 3,4,5,2,13,4,5,2,1。

第五个测试用例中,解码后食物依次出现在顶点 6,1,3,5,2,46,1,3,5,2,4。

第六个测试用例中,解码后食物依次出现在顶点 7,5,6,1,47,5,6,1,4。

输入解题思路,AI测评打分。不知道怎么写?

首页