PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000054. Chỉ đường trên cây nhị phân (Step-By-Step Directions From a Binary Tree Node to Another)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chỉ đường trên cây nhị phân (Step-By-Step Directions From a Binary Tree Node to Another)

Step-By-Step Directions From a Binary Tree Node to Another

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A binary tree has nn distinctly indexed vertices, each with at most one left and one right child. Given a start vertex ss and a destination tt, produce directions along their shortest path:

  • U: move to the parent.
  • L: move to the left child.
  • R: move to the right child.

The direction string must list moves in their actual travel order. The original LeetCode function interface is adapted to standard PHOJ input using child indices.

Input

  • The first line contains n,rn,r: number of vertices and root index.
  • The next nn lines each contain li,ril_i,r_i: left and right child indices of vertex ii, with 00 denoting an absent child.
  • The last line contains distinct vertices s,ts,t.

Output

Print one string consisting only of U, L, and R describing the shortest path from ss to tt.

Subtasks

General constraints: 2≤n≤1052\le n\le10^5; 1≤r,s,t≤n1\le r,s,t\le n; s≠ts\ne t; 0≤li,ri≤n0\le l_i,r_i\le n; các cạnh con tạo thành đúng một cây nhị phân.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000.

Examples

Input

5 1
2 3
4 0
5 0
0 0
0 0
4 5

Output

UURL

Explanation

Starting at vertex 44, move U to 22 and U again to root 11. Then move R to vertex 33 and L to vertex 55, producing UURL.