#597
Unrated
Barn Tree
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Note: the time limit for this problem is 4s, two times the default. The memory limit is also twice the default.

Farmer John's farm has NN barns (2N21052 \leq N \leq 2\cdot 10^5) numbered 1N1 \dots N. There are N1N-1 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 jjth barn has hjh_j hay bales (1hj1091\le h_j\le 10^9).

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 N.N.

The second line of input contains the space-separated values of hjh_j for j=1Nj = 1 \dots N.

The final N1N-1 lines of input each contain two space-separated barn numbers ui viu_i \ v_i, indicating that there is a bidirectional road connecting uiu_i and viv_i.

출력

Output the minimum possible number of orders, followed a sequence of orders of that length, one per line.

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.

예제 입력 1

4
2 1 4 5
1 2
2 3
2 4

예제 출력 1

3
3 2 1
4 2 2
2 1 1

점수

Test cases 2-8 satisfy N5000N\leq 5000Test cases 7-10 satisfy vi=ui+1v_i=u_i+1Test cases 11-16 satisfy no additional constraints

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.