Spring Branch——春枝
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Statement
Homeward-bound during the flowers' season, I dread to see them fade; Through endless flames of war, the journey is hard and delayed. How much regret has the spring wind witnessed, in vain? To whom could I entrust a solitary bloom, for whom to view and retain?
As spring returns, traveler Paper passes through a war-torn region. Seeing a tree in full bloom, she worries about its withering. The ongoing war and the perilous roads mean that storing an entire tree would be prohibitively expensive—a cost determined by the longest path between branches (i.e., the tree's diameter[1]). To preserve the beauty of spring, Paper wants to send a flower as a token of her feelings, but a tree with a long diameter is vulnerable to loss. She can employ a clever trick: select a branch path (e.g., from starting point to end point ), shift its edges, and then tie all branches along the path (except the starting point) directly to starting point , making the path as compact as a new branch without any loss. This way, the tree remains a tree, but its path is shortened. Please help Paper find the minimum number of operations required to minimize the diameter, so that she can express her regret for the spring wind and preserve the beauty of spring.
Spring arrives, and Paper wants to store a tree. She knows that the cost of storing it depends on the tree's diameter. The diameter of a tree is the longest possible distance between any pair of vertices, which is itself measured by the number of edges on the unique simple path connecting them. To reduce storage space without breaking branches, her goal is to first minimize the diameter. She can do the following for the tree:
- Take two vertices and , and let the simple path[2] from to be The vertex sequence on it is , where and ;
- Remove all edges on the path; in other words, remove the edges ;
- Connect vertices directly to . In other words, add the edge .
It can be shown that after the above operations, the graph is still a tree. Please help her determine the minimum number of operations required to achieve the minimum diameter.
Input
Each test contains multiple test cases. The first line contains the number of test cases, (). The test cases are described as follows:
The first line of each test case contains an integer, (), representing the number of nodes in the tree. The next lines of each test case describe a tree: each line contains two integers, and (, ), representing the edges between vertices and , and ensuring that these edges form a tree.
The sum of across all test cases is guaranteed to be no more than . The next lines of each test case describe a tree. Each line contains two integers and (where , ), representing an edge between vertices and . These edges are guaranteed to form a tree.
The sum of across all test cases is guaranteed to not exceed .
Output
For each test case, output an integer representing the minimum number of operations required 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
Notes

In the first test case, the original tree has a diameter of 3. Paper can perform the operation on and . As shown in the figure, the operation consists of the following steps:
-
Remove tree paths , , and .
-
Added tree paths , , and .
After the above operations, the diameter is reduced to . It can be shown that is the minimum diameter.
In the second example, the diameter of the tree is 1. It can be shown that 1 is already the minimum value, so Paper does not need to perform any operations.
The diameter of a tree is the longest possible distance between any pair of vertices, which is itself measured by the number of edges on the unique simple path connecting them. ↩︎
A simple path is a path between two vertices in a tree that does not visit any vertex repeatedly. It can be proved that a simple path between any two vertices is always unique. ↩︎
2025 JSUT Collegiate Programming Contest 江苏理工学院新生赛-同步赛
- 状态
- 已结束
- 规则
- ACM/ICPC
- 题目
- 15
- 开始于
- 2025-11-8 12:00
- 结束于
- 2025-11-8 17:00
- 持续时间
- 5 小时
- 主持人
- 参赛人数
- 15