QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#8636. 伸展树

Statistics

题目描述

伸展树 (Splay)是一种平衡二叉搜索树。也就是说,伸展树首先是一棵二叉树:每个节点至多有两个孩子(左孩子、右孩子)。

伸展树中有两种基本操作:左旋和右旋。其中,左旋是把节点的右孩子上提,右旋是把节点的左孩子上提。

img img

你现在有两棵有标号的、节点数为 $n$ 的伸展树,你需要用至多 $2n$ 次左旋或右旋操作把初始状态变成目标状态。

输入格式

从标准输入读入数据。

第一行一个正整数 $n$ 表示节点个数。

接下来 $n-1$ 行,每行 $3$ 个数字 $x,y,c$,表示 $x$ 节点有孩子节点 $y$,如果 $c$ 为 $0$,则 $y$ 为左孩子,否则为右孩子。这棵树是初始状态。

再接下来 $n-1$ 行,每行 $3$ 个数字 $x,y,c$,表示 $x$ 节点有孩子节点 $y$,如果 $c$ 为 $0$,则 $y$ 为左孩子,否则为右孩子。这棵树是目标状态。

输出格式

输出到标准输出。

首先第一行输出 NoYesNo 表示不能通过至多 $2n$ 次操作得到目标状态,否则输出 Yes

如果输出 Yes,则接下来一行输出一个整数 $times$,表示你的构造方案中所用的操作次数。

接下来 $times$ 行,每行两个整数 $x,c$,如果 $c$ 为 $0$,那么表示对节点 $x$ 左旋,否则是右旋。

保证输入的树合法。

样例

输入

3
3 1 0
1 2 1
1 3 1
3 2 0

输出

Yes
3
1 0
2 1
3 1

样例

输入

2
1 2 0
2 1 0

输出

No

解释

子任务

保证 $1\le n \le 10^6$。

子任务 1(30 分):$n\le 10$。

子任务 2(30 分):$n\le 1000$。

子任务 3(40 分):$n\le 10^6$。

Discussions

About Discussions

The discussion section is only for posting: Editorials, 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. Submitting multiple issues may cause your account to be banned.
  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.