Codeforces Round 947 (Div. 1 + Div. 2)
D
Paint the Tree
Codeforces
1700
原题
← CE →
翻译中…

378QAQ has a tree with $n$ vertices. Initially, all vertices are white.

翻译中…

There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order:

翻译中…
  1. Move $P_A$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red.
  2. Move $P_B$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.
翻译中…

Initially, the vertex $a$ is painted red. If $a=b$, the vertex $a$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.

翻译中…

378QAQ wants to know the minimum number of steps to paint all vertices blue.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains one integer $n$ ($1\leq n\leq 2\cdot 10^5$).

翻译中…

The second line of each test case contains two integers $a$ and $b$ ($1\leq a,b\leq n$).

翻译中…

Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output the minimum number of steps to paint all vertices blue.

翻译中…

Note

翻译中…

In the first test case, 378QAQ can paint all vertices blue in the following order:

翻译中…
  • Initially, $P_A$ is located on the vertex $1$, and $P_B$ is located on the vertex $2$. The vertex $1$ is painted red and the vertex $2$ is white.
  • 378QAQ moves $P_A$ to the vertex $2$ and paints it red. Then 378QAQ moves $P_B$ to the vertex $1$ and paints it blue.
  • 378QAQ moves $P_A$ to the vertex $1$. Then 378QAQ moves $P_B$ to the vertex $2$ and paints it blue.
翻译中…

样例

Input #1
3
2
1 2
1 2
5
1 2
1 2
1 3
1 4
1 5
8
5 4
7 1
1 5
1 8
8 3
7 2
8 6
3 4
Output #1
2
8
13
InputCopy OutputCopy
题目信息
题号
1975D
时间限制
2 s
内存限制
256 MB
标签
暴力
DFS 及类似
动态规划
贪心
最短路
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。