| Soy Cup #2: Vivian |
|---|
| Finished |
As her assignment, Riri's given two permutations$$$^{\text{∗}}$$$ of length $$$n$$$. Determine if there exists a rooted tree such that the first permutation is one of its DFS orders and the second permutation is one of its BFS orders.
As a reminder, the DFS order is generated by the following pseudocode:
dfs_order = []
def dfs(v):
dfs_order.append(v)
pick an arbitrary permutation s of children of v
for child in s:
dfs(child)
dfs(root)
and the BFS order is generated by the following pseudocode:
bfs_order = []
queue = [root]
while len(queue):
u = queue[0]
bfs_order.append(u)
queue = queue[1:]
pick an arbitrary permutation s of children of u
for child in s:
queue.append(child)
However, Riri is addicted to kemurikusa and is not interested in these boring assignment, so she forwards the assignment to you. Can you do it well?
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains one integer $$$n$$$ ($$$1\le n\le 2\cdot10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1\le a_i\le n$$$, $$$a_i\neq a_j$$$) — the first permutation.
The third line of each test case contains $$$n$$$ integers $$$b_1,b_2,\dots,b_n$$$ ($$$1\le b_i\le n$$$, $$$b_i\neq b_j$$$) — the second permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.
For each test case, output "YES" if such a tree exists; otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
221 21 221 22 1
YES NO
| Name |
|---|


