QOJ.ac

QOJ

时间限制: 1 s 内存限制: 1024 MB 总分: 100

#6396. 谜题:楔子

统计

Grammy 是一位解谜大师。今天,她正在玩一种“楔”谜题的变体。在这个变体中,有一棵带有若干汉字的根树。树的根节点是顶点 1,该顶点未被标记。被标记的顶点上可能带有“长”、“短”或“同”符号。目标是将所有被标记的顶点两两配对,使得:

  • 每个被标记的顶点通过标记它们之间最短路径上的每一条边,与恰好另一个被标记的顶点相连。
  • 带有“长”字符的顶点到根节点的距离必须比其配对顶点更远。
  • 带有“短”字符的顶点到根节点的距离必须比其配对顶点更近。
  • 带有“同”字符的顶点到根节点的距离必须与其配对顶点相同。
  • 树上的每条边最多只能被标记一次。

左图展示了一个仅包含线索的谜题示例,右图展示了该谜题的一种可能解法。

Grammy 当然知道如何解这个谜题,但她决定考考你。请解决这个谜题。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 10^5$),表示树的顶点数。

接下来的 $n - 1$ 行,每行包含两个整数 $i, p_i$ ($1 \le p_i < i \le n$) 和一个字符串 $t_i$ ($t_i \in \{\text{"Chang"}, \text{"Duan"}, \text{"Tong"}, \text{"-"}\}$),表示 $p_i$ 和 $i$ 之间有一条边,且顶点 $i$ 的类型为 $t_i$(“-”表示顶点 $i$ 未被标记)。保证 $i$ 按递增顺序给出。同时保证至少存在一个被标记的顶点。

输出格式

如果不存在解,输出一行 “NO”。

否则,第一行输出 “YES”,然后输出若干行,每行包含两个整数 $u_i, v_i$,表示你解法中一对相连的顶点。如果存在多种解,输出任意一种即可。

样例

样例输入 1

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

样例输出 1

YES
6 8
5 4

样例输入 2

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

样例输出 2

YES
9 8
3 10
2 6
7 5

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.