CF1881F.Minimum Maximum Distance

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a tree with nn vertices, some of which are marked. A tree is a connected undirected graph without cycles.

Let fif_i denote the maximum distance from vertex ii to any of the marked vertices.

Your task is to find the minimum value of fif_i among all vertices.

For example, in the tree shown in the example, vertices 22, 66, and 77 are marked. Then the array f(i)=[2,3,2,4,4,3,3]f(i) = [2, 3, 2, 4, 4, 3, 3]. The minimum fif_i is for vertices 11 and 33.

你有一棵包含 nn 个顶点的树,其中部分顶点被标记。树是一种无环的连通无向图。

令 fif_i 表示从顶点 ii 到任意一个被标记顶点的最大距离。

你的任务是找出所有顶点中 fif_i 的最小值。

例如,在示例所示的树中,顶点 22、66 和 77 被标记。此时数组 f(i)=[2,3,2,4,4,3,3]f(i) = [2, 3, 2, 4, 4, 3, 3]。fif_i 的最小值出现在顶点 11 和 33 处。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5) — the number of vertices in the tree and the number of marked vertices, respectively.

The second line of each test case contains kk integers aia_i (1≤ai≤n,ai−1<ai1 \le a_i \le n, a_{i-1} \lt a_i) — the indices of the marked vertices.

The next n−1n - 1 lines contain two integers uiu_i and viv_i — the indices of vertices connected by the ii-th edge.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5)—— 分别表示树中顶点的数量和被标记顶点的数量。

每个测试用例的第二行包含 kk 个整数 aia_i(1≤ai≤n, ai−1<ai1 \le a_i \le n,\, a_{i-1} \lt a_i)—— 表示被标记顶点的下标。

接下来的 n−1n - 1 行每行包含两个整数 uiu_i 和 viv_i —— 表示第 ii 条边所连接的两个顶点的下标。

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

输出格式

For each test case, output a single integer — the minimum value of fif_i among all vertices.

对于每个测试用例,输出一个整数——所有顶点中 fif_i 的最小值。

输入输出样例

  • 输入#1

    6
    7 3
    2 6 7
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    4 4
    1 2 3 4
    1 2
    2 3
    3 4
    5 1
    1
    1 2
    1 3
    1 4
    1 5
    5 2
    4 5
    1 2
    2 3
    1 4
    4 5
    10 8
    1 2 3 4 5 8 9 10
    2 10
    10 5
    5 3
    3 1
    1 7
    7 4
    4 9
    8 9
    6 1
    10 9
    1 2 4 5 6 7 8 9 10
    1 3
    3 9
    9 4
    4 10
    10 6
    6 7
    7 2
    2 5
    5 8

    输出#1

    2
    2
    0
    1
    4
    5
  • 输入#2

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

    输出#2

    0
    2
    0

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

首页