Remove Exactly Two
시간 제한2초메모리 제한2048 MB
트리에서 정확히 두 정점을 지운 뒤 남는 연결 요소 개수의 최댓값을 구한다.
문제
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 tree1 of vertices. You must perform the following operation exactly twice.
- Select a vertex ;
- Remove all edges incident to , and also the vertex .
Please find the maximum number of connected components after performing the operation exactly twice.
Two vertices and are in the same connected component if and only if there exists a path from to . For clarity, note that the graph with vertices has connected components by definition.2
1A tree is a connected graph without cycles.
2But is such a graph connected?
입력
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
Each of the next lines contains two integers and , denoting the two vertices connected by an edge (, ). It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of over all test cases does not exceed .
출력
For each test case, output the maximum number of connected components on a separate line.
힌트
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 vertices is . Therefore, the answer is .
On the second test case, removing two vertices and leaves connected components. As it is impossible to make connected components with vertices, the answer is .
On the third test case, removing two vertices and leaves 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 connected components. Therefore, the answer is .