Red-Black Tree
시간 제한2초메모리 제한1024 MB
주어진 이진 루트 트리의 각 정점을 빨강 또는 검정으로 칠해, 빨강 정점끼리 이어진 간선이 없고 루트에서 void까지 가는 모든 경로의 검정 정점 수가 같도록 만든다.
문제
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 (), the number of vertices in the tree. The vertices are numbered by integers from to .
The next line contains space-separated integers: , , , (). A number means that vertex is a child of vertex . In case means that 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 to 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 characters. The character at -th position must be "R" if vertex is red, or "B" if it is black.
If it is not possible to color the tree, print "Impossible".