In Chefland, there are
cities numbered from to . The cities are connected usingroads such that there exists a path between all pairs of cities. In other words, the cities and roads forma a tree-structure
The roads of Chefland are not in good condition. Therefore, all the roads need to be destroyed. The cost of destroying a road between cities
and is , where and are the degrees of the cities and respectively.
Note that, the degree of a city is the number of roads connected to it before each destroy operation.
Chef wants to find the minimum cost to destroy all the roads. On calculating this cost once, he finds the process very interesting. Therefore, he does this for
more independent queries.
In each query, a road is removed and another road is added. Find the minimum
cost to destroy all roads initially, and after each query. It is
guaranteed that after each query, all the cities are still connected and
form a tree-structure.
Note: Since the queries are independent, the initial tree is retained after each query.
Input Format
- The first line contains a single integer
- is added.
Output Format
For each test case, output
lines. Each line should contain a single integer.The first line contains the answer to the initial tree. The next
lines contain the answer for the updated tree in each query.
Constraints
- .
- It is guaranteed that the given edges form a tree initially as well as after every query.
Subtasks
- Subtask 1 (30 points):
- Sum of
- .
- Sum of
- .
Sample Input 1
1
4 4
1 2
1 3
1 4
1 2 2 3
1 3 2 3
4 1 1 4
2 1 4 2
Sample Output 1
6
5
5
6
5
Explanation
Test Case
: Initially, the tree looks like: The roads can be destroyed as:
- Remove edge between nodes
- .
Thus, the total cost is
. It can be proven that this is the minimum cost required to remove all the edges.
Query
.
Query
.
Query
, as explained earlier.
Query
ConversionConversion EmoticonEmoticon