删除边AI (CI翻译)
Frums - CreditsAI (CI翻译)
You are given a tree with $n$ vertices, numbered from $1$ to $n$.
给定一棵有 $n$ 个顶点的树,顶点编号为 $1$ 到 $n$。AI (CI翻译)
You need to perform $m$ operations on this tree. The $i$-th operation consists of two steps:
你需要对这棵树进行 $m$ 次操作。第 $i$ 次操作包含两个步骤:AI (CI翻译)
- an edge between vertex
$1$and vertex$v_i$is added, which creates a cycle; - you choose one edge of this cycle (possibly the newly added one) and delete it, so the graph becomes a tree again.
- 添加一条连接顶点
$1$和顶点$v_i$的边,从而形成一个环; - 选择并删除这个环上的一条边(可以是刚添加的边),使图重新变成一棵树。
If there is already an edge between vertex $1$ and vertex $v_i$, the new edge is still added, and the cycle consists of these two parallel edges.
如果顶点 $1$ 和顶点 $v_i$ 之间已经有一条边,仍然会添加新边,此时这个环由这两条平行边组成。AI (CI翻译)
Find the maximum possible distance$^{\text{∗}}$ between vertices $1$ and $n$ after all $m$ operations.
求完成所有 $m$ 次操作后,顶点 $1$ 和顶点 $n$ 之间的最大可能距离$^{\text{∗}}$。AI (CI翻译)
$^{\text{∗}}$The distance between two vertices in a tree is the number of edges on the unique simple path between them.
$^{\text{∗}}$树中两个顶点之间的距离是它们之间唯一简单路径上的边数。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 two integers $n$ and $m$ ($2 \le n \le 5 \cdot 10^5$, $1 \le m \le 5 \cdot 10^5$) — the number of vertices and the number of operations.
每个测试用例的第一行包含两个整数 $n$ 和 $m$($2 \le n \le 5 \cdot 10^5$,$1 \le m \le 5 \cdot 10^5$)——分别表示顶点数和操作次数。AI (CI翻译)
Each of the next $n - 1$ lines contains two integers $x$ and $y$ ($1 \le x, y \le n$, $x \ne y$) — an edge of the initial tree. It is guaranteed that the given edges form a tree.
接下来的 $n - 1$ 行,每行包含两个整数 $x$ 和 $y$($1 \le x, y \le n$,$x \ne y$),表示初始树中的一条边。保证给出的边构成一棵树。AI (CI翻译)
The next line contains $m$ integers $v_1, v_2, \ldots, v_m$ ($2 \le v_i \le n$), describing the operations.
接下来一行包含 $m$ 个整数 $v_1, v_2, \ldots, v_m$($2 \le v_i \le n$),描述这些操作。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
保证所有测试用例的 $n$ 之和不超过 $5 \cdot 10^5$。AI (CI翻译)
It is guaranteed that the sum of $m$ over all test cases does not exceed $5 \cdot 10^5$.
保证所有测试用例的 $m$ 之和不超过 $5 \cdot 10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer — the maximum possible distance between vertices $1$ and $n$ after all $m$ operations.
对于每个测试用例,输出一个整数——完成所有 $m$ 次操作后,顶点 $1$ 和顶点 $n$ 之间的最大可能距离。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, the tree consists of a single edge $(1, 2)$, and it stays the same after every operation. Thus, the answer is $1$.
在第一个测试用例中,这棵树只有一条边 $(1, 2)$,每次操作后它都保持不变。因此答案为 $1$。AI (CI翻译)
In the second test case, the distance between vertices $1$ and $3$ is initially $2$, and neither operation can increase it. Thus, the answer is $2$.
在第二个测试用例中,顶点 $1$ 和顶点 $3$ 之间的初始距离为 $2$,两次操作都无法增加这个距离。因此答案为 $2$。AI (CI翻译)
In the third test case, we can delete the newly added edge in the first operation. In the second operation, the edge $(1, 5)$ is added, and we delete the edge $(1, 2)$. After that, the path from $1$ to $7$ is $1 \to 5 \to 4 \to 2 \to 3 \to 7$, so the distance is $5$. It can be shown that no larger distance is possible. Thus, the answer is $5$.
在第三个测试用例中,第一次操作时可以删除刚添加的边。第二次操作时,添加边 $(1, 5)$,并删除边 $(1, 2)$。此后,从 $1$ 到 $7$ 的路径为 $1 \to 5 \to 4 \to 2 \to 3 \to 7$,因此距离为 $5$。可以证明无法得到更大的距离,所以答案为 $5$。AI (CI翻译)
样例
5
2 2
1 2
2 2
3 2
1 2
2 3
2 3
7 2
1 2
2 3
3 7
2 4
4 5
1 6
6 5
10 4
1 2
2 3
3 10
2 4
4 5
5 6
1 7
7 8
8 9
9 8 6 10
7 1
1 2
2 7
1 3
3 4
4 5
5 6
6
1
2
5
6
2
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。