QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#3487. 拥抱树

统计

曾经,有两棵树忘记了它们的位置,开始向彼此生长。其中一棵从左侧生长,另一棵从右侧生长。它们在 $n$ 个点上发生了碰撞。

将这些点从左到右编号为 $1, 2, \dots, n$,左侧的树最终将所有点连接成一棵以节点 $1$ 为根的子树,使得每个节点的子节点编号都大于该节点本身。我们可以用 $n - 1$ 条边来描述这棵子树。

类似地,右侧的树也将所有节点连接成一棵以节点 $n$ 为根的子树,其中每个节点的子节点编号都小于该节点本身。这产生了另外 $n - 1$ 条边。

现在,给定总共 $2(n - 1)$ 条边的列表,要分辨出哪条边属于哪棵树并不容易。你能找出一个可能的分配方案,或者确定这组边不可能由两棵树合并而成吗?

输入格式

第一行包含整数 $n$ ($2 \le n \le 10^5$)。接下来的 $2(n - 1)$ 行,每行包含两个整数 $u, v$ ($1 \le u < v \le n$),表示连接节点 $u$ 和 $v$ 的一条边。一对节点 $(u, v)$ 之间可能存在多条边。

输出格式

如果这些边可以由两棵分别从左向右生长和从右向左生长的树合并而成,则输出一个长度为 $2(n - 1)$ 的字符串,其中第 $i$ 个字符为 L 表示第 $i$ 条边来自左侧的树,为 R 表示来自右侧的树。否则,输出一行 impossible。如果存在多种解,你可以输出其中任意一种。

说明

在第一个样例中,有两种解:LLRRRRLLLLRLRRLR。 在第二个样例中,没有解。注意 LRLR 是无效的,因为它意味着右侧的树在向后生长(从左向右)。

样例

输入 1

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

输出 1

LLRRRRLL

输入 2

3
1 2
1 2
1 3
1 3

输出 2

impossible

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.