Universal Cup Judging System

Universal Cup

Time Limit: 1.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓
Statistics

给定两棵分别有 $n$ 个和 $m$ 个顶点的有根树。顶点的编号分别为 $1 \dots n$(对应第一棵树)和 $1 \dots m$(对应第二棵树),且根节点分别为 $n$ 和 $m$。两棵树都有 $k$ 个叶子节点,且在这两棵树中,叶子节点恰好都是编号为 $1 \dots k$ 的顶点。在此处,树的根节点不被视为叶子节点,即使它只有一个邻居。

对于每个 $i \in 1 \dots k$,你需要选择红色或蓝色。然后,你需要将两棵树中编号为 $i$ 的顶点涂上所选的颜色。

在对叶子节点进行涂色后,两棵树必须满足以下条件: * 对于每个顶点 $u$,其子树中红色叶子节点的数量与蓝色叶子节点的数量之差的绝对值不超过 1。

输入格式

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

第一行包含两个整数 $n$ 和 $m$ ($3 \le n, m \le 10^5$)。

第二行包含 $n-1$ 个整数 $p_1, \dots, p_{n-1}$ ($i < p_i \le n$);其中第 $i$ 个数表示第一棵树中 $i$ 与 $p_i$ 之间的一条边。

第三行包含 $m-1$ 个整数 $q_1, \dots, q_{m-1}$ ($i < q_i \le m$);其中第 $i$ 个数表示第二棵树中 $i$ 与 $q_i$ 之间的一条边。

保证在两棵树中,恰好顶点 $1 \dots k$ 为叶子节点。保证所有测试用例中 $n+m$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,按以下方式在一行中打印答案:

  • 如果不存在满足条件的方案,打印 IMPOSSIBLE。
  • 否则,打印一个长度为 $k$ 的字符串。字符串的第 $i$ 个字符表示第 $i$ 个叶子节点的颜色,B 代表蓝色,R 代表红色。

样例

样例输入 1

2
7 7
5 5 6 6 7 7
5 6 5 6 7 7
5 4
4 4 5 5
4 4 4

样例输出 1

RBBR
RBB

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.