#P1830D. Mex Tree
Mex Tree
No submission language available for this problem.
Description
You are given a tree with nodes. For each node, you either color it in or .
The value of a path is equal to the MEX of the colors of the nodes from the shortest path between and .
The value of a coloring is equal to the sum of values of all paths such that .
What is the maximum possible value of any coloring of the tree?
The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
- The MEX of is , because does not belong to the array.
- The MEX of is , because and belong to the array, but does not.
- The MEX of is because , , , and belong to the array, but does not.
Each test contains multiple test cases. The first line of input contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of nodes in the tree.
The following lines of each test case contains integers and () — indicating an edge between vertices and . It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of across all test cases does not exceed .
For each test case, print the maximum possible value of any coloring of the tree.
Input
Each test contains multiple test cases. The first line of input contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of nodes in the tree.
The following lines of each test case contains integers and () — indicating an edge between vertices and . It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of across all test cases does not exceed .
Output
For each test case, print the maximum possible value of any coloring of the tree.
Note
In the first sample, we will color vertex in and vertices in . After this, we consider all paths:
- with value
- with value
- with value
- with value
- with value
- with value
We notice the sum of values is which is the maximum possible.