33OJ
登录 注册

#P17889. [ICPC 2026 Nanchang I] UNO

    ID: 20625 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>模拟Special Judge枚举排序江西ICPC2026省赛/邀请赛

[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 nn cards, with ranks being any integers from 11 to nn; there may be two cards with the same color and rank. The whole deck has 4n4n cards.

At the beginning of the game, Troubadour_Ggmz arranges all 4n4n 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 R<Y<G<B\mathtt{R} < \mathtt{Y} < \mathtt{G} < \mathtt{B}, 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 tt (1≤t≤10001 \le t \le 1000), the number of test cases. For each test case:

The first line contains a positive integer nn (1≤n≤10001 \le n \le 1000).

Then 4n4n lines follow, each containing a positive integer xix_i and a character cic_i, where 1≤xi≤n1 \le x_i \le n, $c_i \in \{\texttt{R}, \texttt{Y}, \texttt{G}, \texttt{B}\}$, indicating the rank and color of the ii-th card in order. It is guaranteed that each color appears exactly nn times, but cards with the same color and rank may appear multiple times.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

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