QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#23259. 家谱

Statistics

金博士探索古代亚马逊王国的遗址时,发现了两份女王家谱。每份家谱都用如下所示的树记录一位初代女王及其女性后代之间的亲子关系。例如,图 1 中 $C$ 是初代女王,$F$ 和 $D$ 是 $C$ 的孩子。同一个人的孩子之间没有顺序之分。

problem_23259_311bbb645078eabc59db67925c556b02.png

图 1

发现这两份家谱时,人们得知它们已经发生了零次或多次如下定义的单次损坏。

单次损坏:母亲 $P$ 和 $P$ 的一个孩子 $Q$ 合并为一个人。两人所有的孩子(不含 $Q$)都成为合并后这个人的孩子。

例如,对图 2 进行合并 $A$ 和 $F$ 的单次损坏,会得到图 3。再对图 3 进行合并 $B$ 和 $D$ 的单次损坏(图 4),则得到图 5。

problem_23259_301fc0f647a0d1cbb0b535e05a61aab8.png

图 2

problem_23259_c78ed595c960e0bc9518e52ef6b2842b.png

图 3

problem_23259_d5f09670c76351bb08f1ca75e6d29258.png

图 4

problem_23259_d57e4c974c2c77a374e0fc9ae6b10b25.png

图 5

对图 5 进行合并 $E$ 和 $G$ 的单次损坏(图 6),会得到图 7。

problem_23259_10c02c0c78bf1eb310b2926ae9fc3c49.png

图 6

problem_23259_12e0c2507b723736ba848eaa343015f2.png

图 7

幸运的是,从未发生涉及没有孩子的人(图中名字为数字的人)的单次损坏。所有没有孩子的人的名字都保留在家谱中,且互不相同。然而,有孩子的人(图中名字为字母的人)的名字一个也没有保留下来。因此,发现的家谱形如图 8。

problem_23259_cbd63bede0c7fa2828f3422a0ff42eb8.png

图 8

对同一份原始家谱进行不同的单次损坏,可以得到不同的结果。但是,图 9 中的两份家谱不可能来自同一份原始家谱。

problem_23259_e1d983c3048847e7bfb10b40efe38f85.png

图 9

请编写程序,判断输入的两份家谱是否可能由同一份原始家谱经过零次或多次单次损坏得到。输入的两份家谱 $S$ 和 $T$ 中,没有孩子的人的名字集合相同。也就是说,将 $S$ 中没有孩子的人的名字组成集合,再将 $T$ 中没有孩子的人的名字组成集合,两者相等。

建议源文件命名为 family.c 或 family.cpp,但也可以用其他名称提交到服务器。

输入格式

标准输入给出以下信息。第一行包含第一份家谱的人数 $N_1$、第二份家谱的人数 $N_2$,以及每份家谱中没有孩子的人数 $K$。第一份家谱的人编号为 $1$ 到 $N_1$,第二份家谱的人编号为 $1$ 到 $N_2$。两份家谱中,编号 $1$ 到 $K$ 的人都没有孩子,且编号相同的人的名字相同。

下一行按人的编号顺序给出第一份家谱中每个人的母亲编号,没有母亲时给出 $0$。再下一行按人的编号顺序给出第二份家谱中每个人的母亲编号,没有母亲时给出 $0$。

输出格式

在标准输出中,若两份家谱可能来自同一份原始家谱,则输出 YES,否则输出 NO,均使用大写英文字母。

子任务

所有子任务均满足 $1 \le K < N_1,N_2$。

  • 1: 17/100 分; $2 \le N_1,N_2 \le 300$. 第一份家谱没有发生过单次损坏。
  • 2: 27/100 分; $2 \le N_1,N_2 \le 300$.
  • 3: 15/100 分; $2 \le N_1,N_2 \le 5\,000$.
  • 4: 41/100 分; $2 \le N_1,N_2 \le 300\,000$.

样例

输入格式 1

3 3 2
3 3 0
3 3 0

输出格式 1

YES

输入格式 2

5 5 3
4 4 5 5 0
4 5 5 0 4

输出格式 2

NO

输入格式 3

7 8 4
7 7 6 6 0 5 5
6 7 7 8 0 5 5 5

输出格式 3

NO

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.