Universal Cup Judging System

Universal Cup

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

厌倦了数学方程的求解,DreamGrid 开始尝试解决与有根树相关的方程。

设 $A$ 和 $B$ 为两棵任意的有根树,$r(T)$ 表示树 $T$ 的根。DreamGrid 定义了两种基本运算:

  • 加法:$T = A + B$ 是通过将 $r(A)$ 和 $r(B)$ 合并为一个新根 $r(T)$ 构建的。也就是说,$A$ 和 $B$ 的子树(如果有的话)都成为 $r(T)$ 的子树。
  • 乘法:$T = A \cdot B$ 是通过将 $r(B)$ 与 $A$ 中的每个顶点 $x$ 合并构建的,使得 $r(B)$ 的所有子树都成为 $x$ 的新子树。

下图可能有助于你理解这些运算。

A B A + B A · B B · A

给定三棵有根树 $A$、$B$ 和 $C$,DreamGrid 想要找到两棵有根树 $X$ 和 $Y$,使得 $A \cdot X + B \cdot Y = C$。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含三个整数 $n_a, n_b$ 和 $n_c$ ($2 \le n_a, n_b \le n_c \le 10^5$),分别表示有根树 $A, B$ 和 $C$ 的顶点数。

第二行包含 $n_a$ 个整数 $a_1, a_2, \dots, a_{n_a}$ ($0 \le a_i < i$),其中 $a_i$ 是树 $A$ 中第 $i$ 个顶点的父节点。

第三行包含 $n_b$ 个整数 $b_1, b_2, \dots, b_{n_b}$ ($0 \le b_i < i$),其中 $b_i$ 是树 $B$ 中第 $i$ 个顶点的父节点。

第四行包含 $n_c$ 个整数 $c_1, c_2, \dots, c_{n_c}$ ($0 \le c_i < i$),其中 $c_i$ 是树 $C$ 中第 $i$ 个顶点的父节点。

注意,如果 $a_i = 0$(或 $b_i = 0$ 或 $c_i = 0$),则第 $i$ 个顶点是树 $A$(或 $B$ 或 $C$)的根。

保证所有 $n_c$ 的总和不超过 $2 \times 10^6$。

输出格式

对于每组测试数据,如果找不到解,请在第一行输出 “Impossible”(不含引号)。

否则,在第一行输出两个整数 $n_x$ 和 $n_y$ ($1 \le n_x, n_y \le 10^5$),表示有根树 $X$ 和 $Y$ 的顶点数。

然后在第二行输出 $n_x$ 个整数 $x_1, x_2, \dots, x_{n_x}$ ($0 \le x_i < i$),其中 $x_i$ 是树 $X$ 中第 $i$ 个顶点的父节点。

然后在第三行输出 $n_y$ 个整数 $y_1, y_2, \dots, y_{n_y}$ ($0 \le y_i < i$),其中 $y_i$ 是树 $Y$ 中第 $i$ 个顶点的父节点。

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

样例

样例输入 1

2
2 3 10
0 1
0 1 2
0 1 1 3 4 3 6 3 1 9
4 3 10
0 1 2 2
0 1 2
0 1 1 3 4 3 6 3 1 9

样例输出 1

Impossible
2 1
0 1
0

样例输入 2

1
5 5 49
0 1 1 3 1
0 1 2 1 2
0 1 2 3 4 1 6 7 8 9 1 11 12 13 14 11
16 17 18 19 1 21 22 23 24 1 26 26 1 1
30 31 31 30 30 35 36 36 35 30 40 41
41 40 1 45 46 46 45

样例输出 2

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

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.