#P17889. [ICPC 2026 Nanchang I] UNO
[ICPC 2026 Nanchang I] UNO
Problem Description
Troubadour_Ggmz and Fengmi123 are playing a special UNO game. UNO cards have four colors: red, yellow, green, and blue, denoted by R, Y, G, and B, respectively. Each color contains cards, with ranks being any integers from to ; there may be two cards with the same color and rank. The whole deck has cards.
At the beginning of the game, Troubadour_Ggmz arranges all cards in a row, then Fengmi123 can perform the following operation any number of times: choose two adjacent cards, and if they are the same color or the same rank, swap their positions.
The goal of the game is to rearrange all cards into a state that is both color-ordered and rank-ordered, i.e.:
- The color order is , meaning all red cards appear before all yellow cards, all yellow cards appear before all green cards, and all green cards appear before all blue cards;
- For cards of the same color, the sequence of ranks must be non-decreasing.
Fengmi123 wants to know whether she can achieve the target state through a sequence of allowed operations (possibly zero).
Input Format
There are multiple test cases. The first line contains an integer (), the number of test cases. For each test case:
The first line contains a positive integer ().
Then lines follow, each containing a positive integer and a character , where , $c_i \in \{\texttt{R}, \texttt{Y}, \texttt{G}, \texttt{B}\}$, indicating the rank and color of the -th card in order. It is guaranteed that each color appears exactly times, but cards with the same color and rank may appear multiple times.
It is guaranteed that the sum of over all test cases does not exceed .
Output Format
For each test case, if the target state can be reached, output a line containing “YES”, otherwise output a line containing “NO”.
You may output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will all be recognized as positive responses.
2
2
1 R
2 Y
2 R
1 G
1 Y
2 B
2 G
1 B
2
1 R
2 Y
1 Y
2 B
2 R
1 G
2 G
1 B
YES
NO