Edge Addeleting
Frums - Credits
You are given a tree with vertices, numbered from to .
You need to perform operations on this tree. The -th operation consists of two steps:
- an edge between vertex and vertex is added, which creates a cycle;
- you choose one edge of this cycle (possibly the newly added one) and delete it, so the graph becomes a tree again.
If there is already an edge between vertex and vertex , the new edge is still added, and the cycle consists of these two parallel edges.
Find the maximum possible distance between vertices and after all operations.
The distance between two vertices in a tree is the number of edges on the unique simple path between them.
Input
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 two integers and (, ) — the number of vertices and the number of operations.
Each of the next lines contains two integers and (, ) — an edge of the initial tree. It is guaranteed that the given edges form a tree.
The next line contains integers (), describing the operations.
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the maximum possible distance between vertices and after all operations.
Note
In the first test case, the tree consists of a single edge , and it stays the same after every operation. Thus, the answer is .
In the second test case, the distance between vertices and is initially , and neither operation can increase it. Thus, the answer is .
In the third test case, we can delete the newly added edge in the first operation. In the second operation, the edge is added, and we delete the edge . After that, the path from to is , so the distance is . It can be shown that no larger distance is possible. Thus, the answer is .
5 2 2 1 2 2 2 3 2 1 2 2 3 2 3 7 2 1 2 2 3 3 7 2 4 4 5 1 6 6 5 10 4 1 2 2 3 3 10 2 4 4 5 5 6 1 7 7 8 8 9 9 8 6 10 7 1 1 2 2 7 1 3 3 4 4 5 5 6 6
1 2 5 6 2
Discussions & Comments
Share your approach or ask questions about this problem (0)
You must be logged in to comment or start a discussion on this problem.
Log InNo comments yet. Be the first to comment!
Actions
Open on CodeforcesAbout Platform
Problem Discussions
0Have questions or want to share your solution approach? Join the discussion.
View / write comments