Remove Exactly Two

시간 제한2초메모리 제한2048 MB

요약
트리에서 정확히 두 정점을 지운 뒤 남는 연결 요소 개수의 최댓값을 구한다.
난이도

보통10점 중 6점

유형
트리, DFS, 그리디
정답자
아직 제출이 없습니다

문제

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 nn vertices. You must perform the following operation exactly twice.

  • Select a vertex vv;
  • Remove all edges incident to vv, and also the vertex vv.

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

Two vertices xx and yy are in the same connected component if and only if there exists a path from xx to yy. For clarity, note that the graph with 00 vertices has 00 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 tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

Each of the next n−1n-1 lines contains two integers u_iu\_i and v_iv\_i, denoting the two vertices connected by an edge (1≤u_i,v_i≤n1 \le u\_i,v\_i \le n, u_i≠v_iu\_i \neq v\_i). It is guaranteed that the given edges form a tree.

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

출력

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 00 vertices is 00. Therefore, the answer is 00.

On the second test case, removing two vertices 11 and 22 leaves 22 connected components. As it is impossible to make 33 connected components with 22 vertices, the answer is 22.

On the third test case, removing two vertices 11 and 55 leaves 44 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 55 connected components. Therefore, the answer is 44.

예제1

  1. 예제 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
    
    예상 출력
    0
    2
    4