#P8900. [USACO22DEC] Barn Tree S
[USACO22DEC] Barn Tree S
Description
Farmer John’s farm has barns , labeled . There are roads, each connecting two barns, and from any barn it is possible to reach any other barn via some roads. Currently, barn contains haybales .
To keep his cows happy, Farmer John wants to move the hay so that every barn has the same number of haybales. He may choose any pair of barns connected by a single road and instruct his farmhands to move any positive integer number of haybales, up to the amount in the first barn, from the first barn to the second barn.
Find a sequence of commands that Farmer John can issue to complete this task using as few commands as possible. The input guarantees that such a sequence exists.
Input Format
The first line contains the integer .
The second line contains space-separated integers for .
Each of the last lines contains two space-separated barn indices , indicating there is a bidirectional road between and .
Output Format
Output the minimum number of commands, then output a sequence of that many commands, one per line.
Each command should be three space-separated positive integers: the source barn, the destination barn, and the number of haybales moved from the source to the destination.
If there are multiple solutions, output any of them.
4
2 1 4 5
1 2
2 3
2 4
3
3 2 1
4 2 2
2 1 1
Hint
Sample 1 Explanation
In this example, there are twelve haybales and four barns, which means each barn must end up with three haybales. The sequence in the sample output can be described in natural language as follows:
- From barn to barn , move haybale.
- From barn to barn , move haybales.
- From barn to barn , move haybale.
Testpoint Properties
- Testpoints satisfy .
- Testpoints satisfy .
- Testpoints have no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号