QOJ.ac

QOJ

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

#8636. Splay Tree

统计

A Splay tree is a type of balanced binary search tree. That is, a Splay tree is first and foremost a binary tree: each node has at most two children (a left child and a right child).

There are two basic operations in a Splay tree: left rotation and right rotation. A left rotation promotes a node's right child, while a right rotation promotes a node's left child.

img img

You are given two labeled Splay trees, each with $n$ nodes. You need to transform the initial state into the target state using at most $2n$ left or right rotation operations.

Input

The input is read from standard input.

The first line contains a positive integer $n$, representing the number of nodes.

The next $n-1$ lines each contain three integers $x, y, c$, indicating that node $x$ has a child node $y$. If $c$ is $0$, then $y$ is the left child; otherwise, $y$ is the right child. This tree represents the initial state.

The next $n-1$ lines each contain three integers $x, y, c$, indicating that node $x$ has a child node $y$. If $c$ is $0$, then $y$ is the left child; otherwise, $y$ is the right child. This tree represents the target state.

Output

The output is written to standard output.

The first line should output No or Yes. No indicates that the target state cannot be reached within $2n$ operations, otherwise output Yes.

If you output Yes, the next line should output an integer $times$, representing the number of operations used in your construction.

The following $times$ lines each contain two integers $x, c$. If $c$ is $0$, it represents a left rotation on node $x$; otherwise, it represents a right rotation.

It is guaranteed that the input trees are valid.

Examples

Input 1

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

Output 1

Yes
3
1 0
2 1
3 1

Input 2

2
1 2 0
2 1 0

Output 2

No

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.