Red and Blue
Time limit2sMemory limit32 MB
Given merge, loss, and red-count range observations on bags of marbles, decide if a red/blue coloring fits all entries and print one.
- Level
Hard8 of 10
- Topics
- Union-find, Prefix sum, Shortest path
- Solved
- No attempts yet
Problem
Pay attention to the memory limit.
When he was young, Jeyeon had marbles numbered from to . Each marble was either red or blue. Jeyeon stored the marbles by putting each one into one of distinct bags.
Jeyeon valued his marbles, so he wrote down every action he took with them in a diary, in order. Each diary entry is one of the following three types.
1 i j: Merge the bag containing marble with the bag containing marble . ()2 i: Marble was lost while he was playing with it. () Sadly, the lost marble was never found again.3 i l h: Observe the bag containing marble . That bag contained at least and at most red marbles. ()
Ten years later, Jeyeon found a diary with entries. He now wants to reconstruct the color of each marble from the diary. Help him do it.
Marbles were always kept inside bags, and the diary is assumed to record every movement or change of a marble.
Input
The first line contains two integers and ().
The next lines contain the diary entries in the format described above. Only marbles that have not been lost appear in the entries. In a type 1 entry, marble and marble are guaranteed to be in different bags.
Output
If there is a color assignment for the marbles that satisfies all entries, print YES. Otherwise, print NO.
If such an assignment exists, print on the next line a string of length . The -th character is R if marble is red and B if it is blue. If there are several answers, you may print any of them.