RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0534

Barn Tree

Platinum IV 플래티넘 IV
난이도
2s
시간 제한
256MB
메모리 제한
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 \(N\) barns (\(2 \leq N \leq 2\cdot 10^5\)) numbered
\(1 \dots N\). There are \(N-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 \(j\)th barn has \(h_j\) hay bales (\(1\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.

Problem credits: Aryansh Shrivastava

제약

SCORING

  • Test cases 2-8 satisfy \(N\leq 5000\)
  • Test cases 7-10 satisfy \(v_i=u_i+1\)
  • Test cases 11-16 satisfy no additional constraints

Problem credits: Aryansh Shrivastava

입력 형식

The first line of input contains the value of \(N.\)

The second line of input contains the space-separated values of \(h_j\) for
\(j = 1 \dots N\).

The final \(N-1\) lines of input each contain two space-separated barn numbers
\(u_i \ v_i\), indicating that there is a bidirectional road connecting \(u_i\) and
\(v_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
출력
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:

  1. From barn \(3\) to barn \(2\), move \(1\) bale.
  2. From barn \(4\) to barn \(2\), move \(2\) bales.
  3. From barn \(2\) to barn \(1\), move \(1\) bale.
문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2022-2023 > December > Silver

태그

평가 및 의견

Barn Tree

개요
출제자 난이도 Platinum IV 플래티넘 IV 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Barn Tree

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8