QOJ.ac

QOJ

حد الوقت: 3 s حد الذاكرة: 2048 MB مجموع النقاط: 100

#2196. Foolprüf Security

الإحصائيات

Alice 和 Bob 获得了一张秘密地下设施的地图。该设施由 $n$ 个安全单元和 $m$ 个化学实验室组成,通过双向隧道连接。该设施的地图构成了一棵树:恰好有 $n + m - 1$ 条隧道,且不存在环。对应安全单元的顶点编号为 $1$ 到 $n$,化学实验室的编号为 $n + 1$ 到 $n + m$。每条隧道连接一个安全单元和一个化学实验室;不存在连接两个安全单元或两个化学实验室的隧道。

如果 Alice 或 Bob 被捕,他们决定将地图拆分成两部分。为此,他们计算了这棵树的 Prüfer 序列。Alice 将 Prüfer 序列中 $1$ 到 $n$ 之间的数字按其在原始序列中出现的顺序保存到了她的数据存储中,而 Bob 以同样的方式保存了 Prüfer 序列中 $n + 1$ 到 $n + m$ 之间的数字。

一棵具有 $k$ 个顶点的树的 Prüfer 序列是一个由 $k - 2$ 个整数组成的序列,构造方法如下:找到编号最小的叶子节点(度数为 1 的顶点),将其从树中移除,然后记录其唯一邻居的编号。重复此过程 $k - 3$ 次,直到只剩下一条边。所记录的 $k - 2$ 个顶点编号序列即为 Prüfer 序列。

Alice 和 Bob 安全返回,准备合并他们的数据以恢复原始地图。他们在备份过程中可能出错,这意味着可能不存在这样的地图。Alice 和 Bob 需要你的帮助来恢复任何一张与所收集数据一致的设施地图,使得 Alice 和 Bob 的部分分别是该地图 Prüfer 序列的子序列。

输入格式

第一行包含四个整数 $n, m, k_a$ 和 $k_b$ ($2 \le n, m \le 10^5$; $1 \le k_a, k_b$; $k_a + k_b \le n + m - 2$)。 第二行包含 $k_a$ 个整数 $a_1, a_2, \dots, a_{k_a}$ ($1 \le a_i \le n$) —— Alice 的地图部分。 第三行包含 $k_b$ 个整数 $b_1, b_2, \dots, b_{k_b}$ ($n + 1 \le b_i \le n + m$) —— Bob 的地图部分。

输出格式

如果不存在这样的地图,输出 “No”。 否则,第一行输出 “Yes”,随后输出 $n + m - 1$ 行描述可能的设施地图。每行包含两个整数 $u_i$ 和 $v_i$ —— 表示设施中第 $i$ 条隧道连接的安全单元和化学实验室。

样例

样例输入 1

4 5 4 2
1 3 3 4
7 8

样例输出 1

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

样例输入 2

4 3 3 1
3 2 2
6

样例输出 2

No

说明

第一个样例中树的 Prüfer 序列为 $(7, 1, 6, 3, 3, 8, 4)$。

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.