1972: [USACO 2022 December Contest Silver] Problem 1. Barn Tree
题目描述
Farmer John's farm has barns () numbered . There are roads, where each road connects two barns and it is possible to get from any barn to any other barn via some sequence of roads. Currently, the th barn has hay bales ().
To please his cows, Farmer John would like to move the hay such that each barn has an equal number of bales. He can select any pair of barns connected by a road and order his farmhands to move any positive integer number of bales less than or equal to the number of bales currently at the first barn from the first barn to the second.
Please determine a sequence of orders Farmer John can issue to complete the task in the minimum possible number of orders. It is guaranteed that a sequence of orders exists.
输入
The first line of input contains the value of
The second line of input contains the space-separated values of for .
The final lines of input each contain two space-separated barn numbers , indicating that there is a bidirectional road connecting and .
输出
Each order should be formatted as three space-separated positive integers: the source barn, the destination barn, and the third describes the number of hay bales to move from the source to the destination.
If there are multiple solutions, output any.
样例输入
4
2 1 4 5
1 2
2 3
2 4
样例输出
3
3 2 1
4 2 2
2 1 1
提示
In this example, there are a total of twelve hay bales and four barns, meaning each barn must have three hay bales at the end. The sequence of orders in the sample output can be verbally translated as below:
- From barn to barn , move bale.
- From barn to barn , move bales.
- From barn to barn , move bale.
SCORING:
- Test cases 2-8 satisfy
- Test cases 7-10 satisfy
- Test cases 11-16 satisfy no additional constraints.