QOJ.ac

QOJ

Limite de temps : 2.0 s Limite de mémoire : 256 MB Points totaux : 100 Hackable ✓

#11771. 排列恢复

Statistiques

有两个隐藏的长度为 $n$ 的排列 $a$ 和 $b$。

长度为 $n$ 的排列是一个由 $1$ 到 $n$ 的 $n$ 个不同整数组成的数组,顺序任意。例如,$[2, 3, 1, 5, 4]$ 是一个排列,但 $[1, 2, 2]$ 不是排列($2$ 在数组中出现了两次),$[1, 3, 4]$ 也不是排列($n=3$ 但数组中出现了 $4$)。

对于每个 $i$ 从 $1$ 到 $n$,给定数值 $a_{b_i}$ 和 $b_{a_i}$。请恢复出任意一组可能的排列 $a$ 和 $b$,或者确定它们不存在。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 10^4$),表示测试用例的数量。接下来是各测试用例的描述。

每个测试用例的第一行包含一个整数 $n$ ($1 \le n \le 2 \cdot 10^5$),表示两个排列的大小。

每个测试用例的第二行包含 $n$ 个整数。其中第 $i$ 个数为 $a_{b_i}$ ($1 \le a_{b_i} \le n$)。保证这 $n$ 个整数互不相同。

每个测试用例的第三行包含 $n$ 个整数。其中第 $i$ 个数为 $b_{a_i}$ ($1 \le b_{a_i} \le n$)。保证这 $n$ 个整数互不相同。

保证所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,如果存在解,第一行输出 “YES”,否则输出 “NO”。该字符串不区分大小写(例如,“yEs”、“yes”、“Yes” 和 “YES” 均可)。

如果输出 “YES”,则额外输出两行: 第一行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$),表示一个合法的排列 $a$。 第二行包含 $n$ 个整数 $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$),表示一个合法的排列 $b$。

如果存在多个解,输出任意一个即可。

样例

样例输入 1

6
3
3 1 2
2 3 1
2
1 2
2 1
5
1 2 3 4 5
1 2 3 4 5
1
1
1
6
4 5 1 2 3 6
1 2 3 4 5 6
10
3 7 5 8 9 1 4 10 6 2
7 8 1 5 10 9 2 3 4 6

样例输出 1

YES
2 1 3
3 2 1
NO
YES
5 4 3 2 1
5 4 3 2 1
YES
1
1
NO
YES
8 2 4 6 1 5 10 7 9 3
10 8 6 1 9 5 3 7 4 2

说明

第一个样例的解为 $a = [2, 1, 3], b = [3, 2, 1]$。这得到:

$$a_{b_1} = a_3 = 3 \quad a_{b_2} = a_2 = 1 \quad a_{b_3} = a_1 = 2$$ $$b_{a_1} = b_2 = 2 \quad b_{a_2} = b_1 = 3 \quad b_{a_3} = b_3 = 1$$

这与输入值 $a_b = [3, 1, 2]$ 和 $b_a = [2, 3, 1]$ 相匹配。

在第二个样例中,可以证明不存在合法的排列 $a$ 和 $b$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.