蜈蚣图AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)You are given a tree $T$ with $N$ vertices numbered $1$ to $N$. The $i$-th edge connects vertices $A_i$ and $B_i$.
给定一棵有 $N$ 个顶点的树 $T$,顶点编号为 $1$ 至 $N$。第 $i$ 条边连接顶点 $A_i$ 和 $B_i$。AI (CI翻译)
For a positive integer $k$, define a "centipede graph of length $k$" as a graph with $3k$ vertices obtained by the following procedure:
对于正整数 $k$,将一个含有 $3k$ 个顶点的图称为“长度为 $k$ 的蜈蚣图”,其构造过程如下:AI (CI翻译)
- Prepare a path graph with
$k$vertices. - For each vertex
$v$of the path graph, add two new vertices adjacent only to$v$.
- 准备一个含有
$k$个顶点的路径图。 - 对于路径图中的每个顶点
$v$,添加两个仅与$v$相邻的新顶点。
For example, a centipede graph of length $4$ is as shown in the figure below.
例如,长度为 $4$ 的蜈蚣图如下图所示。AI (CI翻译)

AI (CI翻译)
Find the maximum $x$ such that a "centipede graph of length $x$" is contained as a subgraph of tree $T$.
求最大的 $x$,使得“长度为 $x$ 的蜈蚣图”是树 $T$ 的子图。AI (CI翻译)
$Q$ test cases are given; solve each of them.
共有 $Q$ 组测试用例,请分别求出每组的答案。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$1 \le Q$$3 \le N \le 2 \times 10^5$$1 \le A_i, B_i \le N$- The given graph is a tree.
- The sum of
$N$over all test cases is at most$2 \times 10^5$. - All input values are integers.
$1 \le Q$$3 \le N \le 2 \times 10^5$$1 \le A_i, B_i \le N$- 给定的图是一棵树。
- 所有测试用例的
$N$之和不超过$2 \times 10^5$。 - 所有输入值均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入按以下格式给出:AI (CI翻译)
Q
\text{case}_1
\text{case}_2
\vdots
\text{case}_Q
Q
\text{case}_1
\text{case}_2
\vdots
\text{case}_QAI (CI翻译)
Each test case is given in the following format:
每组测试用例的输入格式如下:AI (CI翻译)
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}
N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}AI (CI翻译)
Output
输出格式
AI (CI翻译)Output $Q$ lines.
输出 $Q$ 行。AI (CI翻译)
The $i$-th line should contain the answer for the $i$-th test case.
第 $i$ 行应包含第 $i$ 组测试用例的答案。AI (CI翻译)
样例
3
8
1 3
2 4
2 5
2 6
3 5
4 7
5 8
5
1 5
1 2
1 3
3 4
15
13 3
4 13
11 1
14 9
9 13
13 6
1 10
7 9
13 2
10 5
3 12
15 13
9 10
12 8
2
1
3
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。