Logo
(追記) (追記ここまで)

33220번 - Balance by Elimination 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 2048 MB95466.667%

문제

You are given a binary tree with $n$ nodes. The nodes are conveniently numbered from 1ドル$ to $n$. Node 1ドル$ is the root of the binary tree.

The height of the subtree rooted at node $u$ is: $$h_u = 1 + \max \left(h_\textrm{left child}, h_\textrm{right child}\right)$$ If a left or right child doesn't exist, its subtree height is defined to be 0. In particular, if a node is a leaf, it has a height of 1ドル$.

You want the tree to become height-balanced. A node is height-balanced if: $$\left|h_\textrm{left child} - h_\textrm{right child}\right| < 2$$ A binary tree is height-balanced if all its nodes are height-balanced.

Find a way to remove at most 1 leaf from the tree, such that the binary tree becomes height-balanced, or output that this is impossible. For example, the tree of the second sample input (visualized in Figure B.1) becomes balanced when removing node 5ドル$.

입력

The input consists of:

  • One line containing a single integer $n$ (1ドル \leq n \leq 10^5$), the number of nodes in the binary tree.
  • Then $n$ lines follow, numbered from 1ドル$ to $n$. The $i$th line contains two integers, the labels of the left and right child of node $i$.

If a left child or right child does not exist, the corresponding integer is equal to 0ドル$. It is guaranteed that the input graph is a binary tree.

출력

Output a single integer:

  • If the tree is already balanced, output "balanced".
  • If it's impossible to make the tree height-balanced, output "impossible".
  • Else, output the number of the leaf you want to remove.

제한

예제 입력 1

4
4 2
0 0
0 0
0 3

예제 출력 1

balanced

예제 입력 2

5
2 3
0 0
0 4
0 5
0 0

예제 출력 2

5

예제 입력 3

4
2 0
3 0
4 0
0 0

예제 출력 3

impossible

힌트

Figure B.1: Visualization of Sample Input 2.

출처

University > Delft University of Technology > Freshmen Programming Contest 2022 B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /