#P9103. BST Transformation via Restricted Rotations
BST Transformation via Restricted Rotations
BST Transformation via Restricted Rotations
In this problem, you are given two binary search trees (BSTs) with n nodes. The trees are labelled with distinct integers from 1 to n and satisfy the BST property (i.e. the in‐order traversal of both trees is sorted in increasing order).
The trees are represented in a straightforward way. The input first contains an integer n. Then follow n lines describing the first tree, and then another n lines describing the second tree. The i
-th line (1-indexed) for each tree contains two integers: the index of the left child and the index of the right child of node i
, using 0 to denote a null pointer.
A BST is defined as follows: for every node with value v, all the node values in its left subtree are less than v and all the node values in its right subtree are greater than v.
Bytie, our protagonist, recalls how to perform rotations on BSTs – but only the following restricted rotations are allowed in his memory. In a left rotation on a node v (with its right child w), he performs the following only if w's left subtree is empty:
[
\begin{aligned}
&\text{if } v\text{ has a parent then}
&\quad \text{replace } v \text{ in its parent with } w,\
&w\text{ becomes the child of } v\text{’s parent},\
&v\text{ becomes the left child of } w,\
&v\text{'s right child is set to null.}
\end{aligned}
]
Similarly, a right rotation on node v (with its left child w) is done only if w's right subtree is empty:
[
\begin{aligned}
&\text{if } v\text{ has a parent then}
&\quad \text{replace } v \text{ in its parent with } w,\
&w\text{ becomes the child of } v\text{'s parent},\
&v\text{ becomes the right child of } w,\
&v\text{'s left child is set to null.}
\end{aligned}
]
The rotations are applied only if no subtree gets dropped (i.e. the problematic subtree is empty). Bytie wants to know whether it is possible to transform the first BST into the second BST using a sequence of these restricted rotations. If possible, compute the minimum number of rotations required, and output the result modulo \(10^9+7\). Otherwise, output -1
.
Note: Even though standard rotations are reversible and maintain the BST invariant, the restriction here (that the inner subtree must be empty) severely limits the allowed operations. Only when a rotation does not cause any loss of nodes (i.e. the subtree in question is already empty) may you perform the operation.
inputFormat
The input is given as follows:
n // Description of the first BST: for i = 1 to n: L[i] R[i] // Description of the second BST: for i = 1 to n: L[i] R[i]
Here, n
(1 ≤ n ≤ small number) is the number of nodes. For each node i
(1-indexed), L[i]
and R[i]
denote the indices of its left and right children respectively (use 0 to represent a null pointer).
outputFormat
Output a single integer – the minimum number of restricted rotations required to transform the first BST into the second BST modulo \(10^9+7\). If the transformation is impossible, output -1
.
sample
1
0 0
0 0
0