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$。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.