QOJ.ac

QOJ

実行時間制限: 1.0 s メモリ制限: 256 MB 満点: 100 ハック可能 ✓

#12402. 树的变换

統計

MianKing 拥有两棵带标号的无根树 $S$ 和 $T$,且都有 $n$ 个节点。他想要通过一些操作将 $S$ 变换为 $T$。

在每次操作中,MianKing 可以选择四个不同的节点 $x, y, z, w$,它们在树 $S$ 中构成一条路径 $\{(x, y), (y, z), (z, w)\}$。然后,他从 $S$ 中移除边 $\{(x, y), (y, z), (z, w)\}$,并选择三条端点均属于 $\{x, y, z, w\}$ 的新边加入到 $S$ 中。MianKing 必须保证在操作后 $S$ 仍然是一棵树。

现在你需要帮助 MianKing 在 $10^4$ 次操作内将 $S$ 变换为 $T$。

输入格式

第一行包含一个整数 $n$。

接下来 $n - 1$ 行,每行包含两个整数 $(x, y)$,表示 $S$ 中的一条边。

接下来 $n - 1$ 行,每行包含两个整数 $(x, y)$,表示 $T$ 中的一条边。

$4 \le n \le 100$

保证 $S$ 和 $T$ 均为树。

输出格式

第一行输出一个字符串 "YES"(如果存在在 $10^4$ 次操作内将 $S$ 变换为 $T$ 的方案),否则输出 "NO"(均不含引号)。

如果存在方案,则:

第一行输出一个整数 $m$,表示你的方案中的操作次数。

接下来对于每次操作,输出两行。第一行包含四个整数 $(x, y, z, w)$,第二行包含六个整数 $a_1, b_1, a_2, b_2, a_3, b_3$,其中 $(a_i, b_i)$ 表示你在该操作中选择的第 $i$ 条新边。

对于每次操作,你必须保证满足以下条件:

  1. $1 \le x, y, z, w, a_i, b_i \le n$
  2. $x, y, z, w$ 互不相同,且在当前树 $S$ 中构成一条路径 $\{(x, y), (y, z), (z, w)\}$。
  3. $a_i, b_i \in \{x, y, z, w\}$
  4. 在移除边 $\{(x, y), (y, z), (z, w)\}$ 并添加新边 $(a_1, b_1), (a_2, b_2), (a_3, b_3)$ 后,$S$ 在此时仍然是一棵树。
  5. 在所有操作完成后,$S$ 应与 $T$ 相同。
  6. $0 \le m \le 10^4$

两棵带标号的树相同,当且仅当对于所有边 $(x, y)$,满足 $[(x, y) \in S] \leftrightarrow [(x, y) \in T]$。

样例

输入 1

5
1 2
2 3
3 4
2 5
1 2
2 3
3 4
1 5

输出 1

YES
2
5 2 3 4
2 3 3 5 3 4
1 2 3 5
1 5 1 2 2 3

输入 2

4
1 2
1 3
1 4
1 2
2 3
3 4

输出 2

NO

输入 3

4
1 2
1 3
1 4
1 2
1 3
1 4

输出 3

YES
0

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.