Codeforces Round 1000 (Div. 2)
C
Remove Exactly Two
Codeforces
1600
原题
← BD →
翻译中…

Recently, Little John got a tree from his aunt to decorate his house. But as it seems, just one tree is not enough to decorate the entire house. Little John has an idea. Maybe he can remove a few vertices from the tree. That will turn it into more trees! Right?

翻译中…

You are given a tree$^{\text{∗}}$ of $n$ vertices. You must perform the following operation exactly twice.

翻译中…
  • Select a vertex $v$;
  • Remove all edges incident to $v$, and also the vertex $v$.
翻译中…

Please find the maximum number of connected components after performing the operation exactly twice.

翻译中…

Two vertices $x$ and $y$ are in the same connected component if and only if there exists a path from $x$ to $y$. For clarity, note that the graph with $0$ vertices has $0$ connected components by definition.$^{\text{†}}$

翻译中…

$^{\text{∗}}$A tree is a connected graph without cycles. $^{\text{†}}$But is such a graph connected?

翻译中…

Input

翻译中…

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.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$).

翻译中…

Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$, denoting the two vertices connected by an edge ($1 \le u_i,v_i \le n$, $u_i \neq v_i$). It is guaranteed that the given 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 maximum number of connected components on a separate line.

翻译中…

Note

翻译中…

On the first test case, removing a vertex twice will make the graph empty. By definition, the number of connected components in the graph with $0$ vertices is $0$. Therefore, the answer is $0$.

翻译中…

On the second test case, removing two vertices $1$ and $2$ leaves $2$ connected components. As it is impossible to make $3$ connected components with $2$ vertices, the answer is $2$.

翻译中…

On the third test case, removing two vertices $1$ and $5$ leaves $4$ connected components, which are $\left\{ 2,4\right\}$, $\left\{ 3\right\}$, $\left\{ 6\right\}$, and $\left\{ 7\right\}$. It can be shown that it is impossible to make $5$ connected components. Therefore, the answer is $4$.

翻译中…

样例

Input #1
3
2
1 2
4
1 2
2 3
2 4
7
1 2
1 3
2 4
4 5
5 6
5 7
Output #1
0
2
4
InputCopy OutputCopy
题目信息
题号
2063C
时间限制
2 s
内存限制
256 MB
标签
暴力
数据结构
DFS 及类似
动态规划
图论
贪心
排序
树

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