CF1795F.Blocking Chips

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree, consisting of nn vertices. There are kk chips, placed in vertices a1,a2,…,aka_1, a_2, \dots, a_k. All aia_i are distinct. Vertices a1,a2,…,aka_1, a_2, \dots, a_k are colored black initially. The remaining vertices are white.

You are going to play a game where you perform some moves (possibly, zero). On the ii-th move (11-indexed) you are going to move the ((i−1) mod k+1)((i - 1) \bmod k + 1)-st chip from its current vertex to an adjacent white vertex and color that vertex black. So, if k=3k=3, you move chip 11 on move 11, chip 22 on move 22, chip 33 on move 33, chip 11 on move 44, chip 22 on move 55 and so on. If there is no adjacent white vertex, then the game ends.

What's the maximum number of moves you can perform?

你被给定一棵包含 nn 个顶点的树。有 kk 个棋子,分别放置在顶点 a1,a2,…,aka_1, a_2, \dots, a_k 上。所有 aia_i 互不相同。初始时,顶点 a1,a2,…,aka_1, a_2, \dots, a_k 被染成黑色,其余顶点为白色。

你将进行一场游戏,执行若干次(可能为零次)操作。在第 ii 次操作(从 11 开始编号)中,你将移动编号为 ((i−1) mod k+1)((i - 1) \bmod k + 1) 的棋子:将其从当前所在顶点移至一个相邻的白色顶点,并将该目标顶点染成黑色。例如,若 k=3k = 3,则你在第 11 次操作中移动第 11 个棋子,第 22 次操作中移动第 22 个棋子,第 33 次操作中移动第 33 个棋子,第 44 次操作中再次移动第 11 个棋子,第 55 次操作中再次移动第 22 个棋子,依此类推。若当前棋子所在顶点没有相邻的白色顶点,则游戏结束。

你最多能执行多少次操作?

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of vertices of the tree.

Each of the next n−1n - 1 lines contains two integers vv and uu (1≤v,u≤n1 \le v, u \le n) — the descriptions of the edges. The given edges form a tree.

The next line contains a single integer kk (1≤k≤n1 \le k \le n) — the number of chips.

The next line contains kk integers a1,a2,…,aka_1, a_2, \dots, a_k (1≤ai≤n1 \le a_i \le n) — the vertices with the chips. All aia_i are distinct.

The sum of nn over all testcases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)——树的顶点数。

接下来的 n−1n - 1 行,每行包含两个整数 vv 和 uu(1≤v,u≤n1 \le v, u \le n)——边的描述。所给边构成一棵树。

下一行包含一个整数 kk(1≤k≤n1 \le k \le n)——筹码的数量。

再下一行包含 kk 个整数 a1,a2,…,aka_1, a_2, \dots, a_k(1≤ai≤n1 \le a_i \le n)——放置筹码的顶点。所有 aia_i 互不相同。

所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each testcase, print a single integer — the maximum number of moves you can perform.

对于每个测试用例,输出一个整数——你能执行的最大移动次数。

输入输出样例

  • 输入#1

    5
    5
    1 2
    2 3
    3 4
    4 5
    1
    3
    5
    1 2
    2 3
    3 4
    4 5
    2
    1 2
    5
    1 2
    2 3
    3 4
    4 5
    2
    2 1
    6
    1 2
    1 3
    2 4
    2 5
    3 6
    3
    1 4 6
    1
    1
    1

    输出#1

    2
    0
    1
    2
    0

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

首页