#JSUTCPC2026H. Poem of Sylvas —— 万木诗
Poem of Sylvas —— 万木诗
Problem Description
My life of sinking to the bottom, a prodigal and the clouds
Like a shallow-experienced migratory bird, lingering on the riverbank
It is the whispering wind and rain outside, the jungle is whispering:
Upon finding her, one finds spring
Therefore, the clouds cover the sun, and the sylvas become a poem
Spring is here, and Paper wants to preserve a tree. She knows that the cost of storage depends on the diameter of the tree[1]. To reduce storage space and ensure that the branches do not break, her goal is to first minimize the diameter as much as possible. She can perform the following operations on the tree:
- Choose two vertices and , and let the sequence of vertices on the simple path[2] from to be , where and ;
- Remove all edges on the path; in other words, remove edges ;
- Connect vertices directly to , in other words, add edges .
It can be proven that after the aforementioned operations, the graph remains a tree. Please assist her in determining the minimum number of operations required to achieve the minimum diameter.
Input
Each test case consists of multiple test cases, and the first line contains the number of test cases (); the description of the test cases is as follows:
The first line of each test case contains an integer () indicating the number of nodes in the tree.
For each test case, the following lines describe a tree: each line contains two integers and (, ) representing an edge between vertices and , ensuring that these edges form a tree.
Ensure that the sum of for all test cases does not exceed .
Output
For each test case, output an integer representing the minimum number of operations to minimize the diameter.
Samples
4
4
1 2
1 3
2 4
2
2 1
4
1 2
2 3
2 4
11
1 2
1 3
2 4
3 5
3 8
5 6
5 7
7 9
7 10
5 11
1
0
0
4
Note
In the first test case, the diameter of the original tree is 3. Paper can perform operations on and . As shown in the figure, the operation involves the following steps:
- Remove the edges , and from the tree
- Add tree edges , and
After the aforementioned operations, the diameter is reduced to . It can be proven that is the minimum diameter.
In the second example, the diameter of the tree is 1. It can be proven that 1 is already the minimum value, so Paper does not need to perform any operation.
The diameter of a tree is the longest possible distance between any pair of vertices, measured by the number of edges on the unique simple path connecting them. ↩︎
A simple path is a route between two vertices in a tree where no vertex is visited twice. It can be proven that a simple path between any two vertices is always unique. ↩︎