Best Tree
Time limit1sMemory limit512 MB
Given the degree sequence of a tree, find the maximum possible size of a maximum matching over all trees realizing that sequence.
- Level
Medium7 of 10
- Topics
- Tree, Greedy, Math, Dynamic programming
- Solved
- No attempts yet
Problem
You are given the degree sequence of a tree (the degrees of all its vertices listed in arbitrary order).
Among all trees with the given degree sequence, find a tree whose maximum matching is as large as possible.
Input
The first line contains one integer (): the number of test cases.
The following lines contain test cases.
The first line of each test case contains one integer (): the number of vertices.
The next line contains integers (): the degree sequence of a tree.
It is guaranteed that and that at least one tree has the given degree sequence.
It is also guaranteed that the sum of over all test cases is at most .
Output
For each test case, print one integer: the largest maximum matching among all trees with the given degree sequence.
Hint
In the first test case, you can build a path with 10 vertices. This path has the same degree sequence and the largest possible maximum matching.
In the second test case, the only possible tree is a star (one vertex connected to all the others), and its maximum matching is 1.