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

Balancing a Tree

Platinum II 플래티넘 II
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

Farmer John has conducted an extensive study of the evolution of different cow
breeds. The result is a rooted tree with \(N\) (\(2\le N\le 10^5\)) nodes labeled
\(1\ldots N\), each node corresponding to a cow breed. For each \(i\in [2,N]\),
the parent of node \(i\) is node \(p_i\) (\(1\le p_i), meaning that breed \(i\)
evolved from breed \(p_i\). A node \(j\) is called an ancestor of node \(i\) if
\(j=p_i\) or \(j\) is an ancestor of \(p_i\).

Every node \(i\) in the tree is associated with a breed having an integer number
of spots \(s_i\). The "imbalance" of the tree is defined to be the maximum of
\(|s_i-s_j|\) over all pairs of nodes \((i,j)\) such that \(j\) is an ancestor of \(i\).

Farmer John doesn't know the exact value of \(s_i\) for each breed, but he knows
lower and upper bounds on these values. Your job is to assign an integer value
of \(s_i \in [l_i,r_i]\) (\(0\le l_i\le r_i\le 10^9\)) to each node such that the
imbalance of the tree is minimized.

Problem credits: Andrew Wang

제약

SCORING

  • Test cases 3-4 satisfy \(l_i=r_i\) for all \(i\).
  • Test cases 5-6 satisfy \(p_i=i-1\) for all \(i\).
  • Test cases 7-16 satisfy no additional constraints.

Within each subtask, the first half of the test cases will satisfy \(B=0\), and
the rest will satisfy \(B=1\).

Problem credits: Andrew Wang

입력 형식

The first line contains \(T\) (\(1\le T\le 10\)), the number of independent test
cases to be solved, and an integer \(B\in \{0,1\}\).

Each test case starts with a line containing \(N\), followed by \(N-1\) integers
\(p_2,p_3,\ldots,p_N\).

The next \(N\) lines each contain two integers \(l_i\) and \(r_i\).

It is guaranteed that the sum of \(N\) over all test cases does not exceed \(10^5\).

출력 형식

For each test case, output one or two lines, depending on the value of \(B\).

The first line for each test case should contain the minimum imbalance.

If \(B=1,\) then print an additional line with \(N\) space-separated integers
\(s_1,s_2,\ldots, s_N\) containing an assignment of spots that achieves the above
imbalance. Any valid assignment will be accepted.

예제 1
입력
3 0
3
1 1
0 100
1 1
6 7
5
1 2 3 4
6 6
1 6
1 6
1 6
5 5
3
1 1
0 10
0 1
9 10
출력
3
1
4
설명

For the first test case, the minimum imbalance is \(3\). One way to achieve
imbalance \(3\) is to set \([s_1,s_2,s_3]=[4,1,7]\).

예제 2
입력
3 1
3
1 1
0 100
1 1
6 7
5
1 2 3 4
6 6
1 6
1 6
1 6
5 5
3
1 1
0 10
0 1
9 10
출력
3
3 1 6
1
6 5 5 5 5
4
5 1 9
설명

This input is the same as the first one aside from the value of \(B\). Another way
to achieve imbalance \(3\) is to set \([s_1,s_2,s_3]=[3,1,6]\).

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2021-2022 > US Open > Gold

태그

평가 및 의견

Balancing a Tree

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

Log in to rate problems.

개별 의견

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

풀이 제출

Balancing a Tree

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