Codeforces Round 986 (Div. 2)
D
Alice's Adventures in Cards
Codeforces
2000
原题
← CE →
翻译中…

Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are $n$ different types of cards in their card game. Alice currently has a card of type $1$ and needs a card of type $n$ to escape Wonderland. The other players have one of each kind of card.

翻译中…

In this card game, Alice can trade cards with the three other players. Each player has different preferences for the $n$ types of cards, which can be described by permutations$^{\text{∗}}$ $q$, $k$, and $j$ for the Queen, King, and Jack, respectively.

翻译中…

A player values card $a$ more than card $b$ if for their permutation $p$, $p_a \gt p_b$. Then, this player is willing to trade card $b$ to Alice in exchange for card $a$. Alice's preferences are straightforward: she values card $a$ more than card $b$ if $a \gt b$, and she will also only trade according to these preferences.

翻译中…

Determine if Alice can trade up from card $1$ to card $n$ subject to these preferences, and if it is possible, give a possible set of trades to do it.

翻译中…

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($2\le n\le 2\cdot 10^5$) — the number of card types.

翻译中…

The next three lines contain the preferences of the Queen, King, and Jack respectively. Each of these lines contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1\le p_i\le n$) — a permutation corresponding to the player's preferences.

翻译中…

The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, on the first line output a single string "YES" or "NO" (without the quotes) denoting whether Alice can trade up to card $n$.

翻译中…

If the first line was "YES", then on the next line output $k$ — the number of trades Alice will make. On the next $k$ lines output space separated a character $c\in \{\texttt{q}, \texttt{k}, \texttt{j}\}$ and integer $x$, denoting that Alice trades with player $c$ to get card $x$. It must be the case that on the $k$'th line, $x = n$. If there are multiple solutions, print any of them.

翻译中…

You can output this answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. The same goes for the character $c$ denoting the player in the trade ($\texttt{Q}, \texttt{K}, \texttt{J}$ will all be accepted alongside their lowercase variants).

翻译中…

Note

翻译中…

In the first testcase, Alice can trade with the King to get card $2$. She can then trade with the Queen to get card $3$.

翻译中…

In the second testcase, even though Alice can trade with the Queen to get card $3$, with the King to get card $2$, and then with the Jack to get card $4$, this is not a valid solution since it doesn't respect Alice's preferences. We can show that there is no way for Alice to get to card $4$.

翻译中…

样例

Input #1
2
3
1 3 2
2 1 3
1 2 3
4
2 3 1 4
1 2 3 4
1 4 2 3
Output #1
YES
2
k 2
q 3
NO
InputCopy OutputCopy
题目信息
题号
2028D
时间限制
2 s
内存限制
256 MB
标签
构造
数据结构
动态规划
图论
贪心
模拟
三分

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。