Separate Them All

 In Chefland, there are 

cities numbered from to . The cities are connected using

roads 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
  • , the number of test cases. Then the test cases follow.
  • The first line of each test case contains two integers
  • and
  • .
  • The next
  • lines contain two integers each, and , denoting a road between cities and
  • .
  • The next
  • lines contain four space separated integers, , , and denoting that the road between the cities and is removed and the road between the cities and
    • 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 there exists a road between cities
  • and
    • .
    • It is guaranteed that the given edges form a tree initially as well as after every query.

    Subtasks

    • Subtask 1 (30 points):
      • Sum of
    over all test cases does not exceed
    • .
  • Subtask 2 (70 points):
    • Sum of
  • over all test cases does not exceed
  • .
  • Sum of
  • over all test cases does not exceed
      • .

    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
    and . The degree of these nodes are and respectively. Hence, the cost is . Now, the degree of node becomes
  • .
  • Remove edge between nodes
  • and . The degree of these nodes are and respectively. Hence, the cost is . Now, the degree of node becomes
  • .
  • Remove edge between nodes
  • and . The degree of these nodes are and respectively. Hence, the cost is
    • .

    Thus, the total cost is

    . It can be proven that this is the minimum cost required to remove all the edges.

    • Query

    : The edge between and is removed and the edge between and is added. The tree looks like: The minimum total cost to remove all the edges of this tree is
  • .

  • Query

  • : The edge between and is removed and the edge between and is added. The tree looks like: The minimum total cost to remove all the edges of this tree is
  • .

  • Query

  • : The edge between and is removed and the edge between and is added. Thus, the tree remains the same as the initial tree. The total cost of removing all the edges is
  • , as explained earlier.

  • Query

  • : The edge between and is removed and the edge between and is added. The tree looks like: The minimum total cost to remove all the edges of this tree is .
    Previous
    Next Post »