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

23659번 - Red-Black Tree 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 1024 MB107231015.152%

문제

Consider a binary rooted tree: a vertex is selected as the root, all edges go in the direction from the root, and each vertex has zero, one, or two children. We will say that each vertex has exactly two outgoing edges: in case it has less than two children, the remaining edges go into the void.

We will say a tree is red-black if the following conditions are satisfied:

  • Each vertex is colored either red or black.
  • There is no edge in the tree which has two red endpoints. The void is considered black.
  • Consider all paths along the edges which go from the root to the void. The number of black vertices is the same on all such paths.

A similar coloring scheme can be used in binary search trees to make them balanced.

You are given a non-empty binary rooted tree. Color its vertices so that it becomes a red-black tree, or determine that it is impossible.

입력

The first line contains an integer $n$ (1ドル \le n \le 500$), the number of vertices in the tree. The vertices are numbered by integers from 1ドル$ to $n$.

The next line contains $n$ space-separated integers: $p_1,ドル $p_2,ドル $\ldots,ドル $p_n$ (0ドル \le p_i \le n$). A number $p_i > 0$ means that vertex $i$ is a child of vertex $p_i$. In case $p_i = 0$ means that $i$ is the root of the tree.

It is guaranteed that the input specifies a valid binary rooted tree: there is exactly one root, each vertex has from 0ドル$ to 2ドル$ children, and it is possible to arrive at any vertex by starting at the root and moving along the edges.

출력

If it is possible to color the tree so that it becomes a red-black tree, print any such coloring as a line of $n$ characters. The character at $i$-th position must be "R" if vertex $i$ is red, or "B" if it is black.

If it is not possible to color the tree, print "Impossible".

제한

예제 입력 1

3
2 0 2

예제 출력 1

BBB

예제 입력 2

4
0 1 1 3

예제 출력 2

RBBR

예제 입력 3

4
4 1 1 0

예제 출력 3

Impossible

노트

출처

Contest > Open Cup > 2018/2019 Season > Stage 9: Grand Prix of Peterhof K번

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

출처

대학교 대회

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

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