아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Barn Tree

시간 제한4초메모리 제한1024 MB

요약
각 노드에 건초가 있는 트리에서 간선을 따라 옮기는 순서를 만들어, 모든 노드가 같은 양의 건초를 갖도록 하는 최소 순서를 구한다.
난이도

보통10점 중 7점

유형
트리, DFS, 그리디, 구현
정답자
아직 제출이 없습니다

문제

Farmer John's farm has NN barns (2≤N≤2⋅1052 \leq N \leq 2\cdot 10^5) numbered 1…N1 \dots N. There are N−1N-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 h_jh\_j hay bales (1≤h_j≤1091\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 h_jh\_j for j=1…Nj = 1 \dots N.

The final N−1N-1 lines of input each contain two space-separated barn numbers u_i v_iu\_i \ v\_i, indicating that there is a bidirectional road connecting u_iu\_i and v_iv\_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.

힌트

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:

  1. From barn 33 to barn 22, move 11 bale.
  2. From barn 44 to barn 22, move 22 bales.
  3. From barn 22 to barn 11, move 11 bale.

예제1

  1. 예제 1

    입력
    4
    2 1 4 5
    1 2
    2 3
    2 4
    
    예상 출력
    3
    3 2 1
    4 2 2
    2 1 1