#P8274. [USACO22OPEN] Balancing a Tree G
[USACO22OPEN] Balancing a Tree G
Background
Thanks to @tiger2005 for providing the SPJ.
Problem Description
Farmer John has done extensive research on the evolution of different cow breeds. The results form a rooted tree with () nodes, numbered , where each node corresponds to a cow breed. For each , the parent of node is node (), meaning that breed evolved from breed . We call node an ancestor of node if or is an ancestor of .
The breed associated with node has an integer number of spots . Define the tree's "imbalance" as the maximum value of over all pairs of nodes where is an ancestor of .
Farmer John does not know the exact value of each , but he knows a lower bound and an upper bound for each value. Your task is to assign an integer value () to each node to minimize the imbalance of the tree.
Input Format
The first line of input contains (), the number of independent subtest cases to solve, and an integer .
The first line of each subtest case contains . The second line contains integers .
The next lines each contain two integers and .
It is guaranteed that the sum of over all subtest cases does not exceed .
Output Format
For each subtest case, output one or two lines depending on the value of .
The first line of each subtest case contains the minimum imbalance.
If , then output an additional line containing space-separated integers , one assignment of spot counts that achieves the above imbalance. Any valid assignment is accepted.
3 0
3
1 1
0 100
1 1
6 7
5
1 2 3 4
6 6
1 6
1 6
1 6
5 5
3
1 1
0 10
0 1
9 10
3
1
4
3 1
3
1 1
0 100
1 1
6 7
5
1 2 3 4
6 6
1 6
1 6
1 6
5 5
3
1 1
0 10
0 1
9 10
3
3 1 6
1
6 5 5 5 5
4
5 1 9
Hint
[Sample Explanation 1]
For the first subtest case, the minimum imbalance is . One way to achieve imbalance is to set .
[Sample Explanation 2]
This subtest case is exactly the same as the first one except for the value of . Another way to achieve imbalance is to set .
[Constraints]
- Test points 3-4: for all , .
- Test points 5-6: for all , .
- Test points 7-16: no additional constraints.
In each subtask, the first half of the test points satisfy , and the second half satisfy .
Translated by ChatGPT 5