Codeforces Round 1077 (Div. 1)
F1
Al Fine (Maximizing Version)
Codeforces
3100
原题
← EF2 →
翻译中…

This is the maximizing version of the problem. The difference between the versions is that in this version, you need to find the maximum possible depth among all common trees. Also, the constraint on $n$ in this version is larger. You can hack only if you solved all versions of this problem.

翻译中…

For Christmas, Tortinita and Arietta each get a Christmas tree. Coincidentally, their trees share the same properties: both have $n+1$ vertices and are rooted at vertex $0$. In their Christmas card to you, each of them appends a DFS order sequence of their tree after their greetings. Recall that a DFS order sequence 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)

翻译中…

Let the DFS order sequence of Tortinita's tree be $a_0, a_1, \ldots, a_n$ and that of Arietta's tree be $b_0, b_1, \ldots, b_n$. You observe that $a_0 = b_0 = 0$, and since they are twin sisters, you wonder if they could have the exact same tree. You also feel that for their Christmas tree, the sisters would not choose a trivial structure. Therefore, you want to find the maximum possible depth$^{\text{∗}}$ among all common trees for both sequences.

翻译中…

$^{\text{∗}}$The depth of a tree is defined as the maximum number of edges on a simple path from any node to the root.

翻译中…

Input

翻译中…

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$ ($2 \le n \le 10^6$).

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \cdots, a_n$ ($1 \le a_i \le n$).

翻译中…

The third line of each test case contains $n$ integers $b_1, b_2, \cdots, b_n$ ($1 \le b_i \le n$).

翻译中…

It is guaranteed that neither sequence $a$ nor $b$ contains duplicates.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each test case, output one integer — the maximum possible depth among all common trees.

翻译中…

Note

翻译中…

In the first test case, $a$ and $b$ are the same. Consider a link tree with edges $(0,1)$ and $(1,2)$; it can give either DFS order sequence, and its depth is $2$. It can be shown that $2$ is the maximum depth among all possible common trees.

翻译中…

In the second test case, consider a star-shaped tree with edges $(0,1)$ and $(0,2)$, whose depth is $1$. It can be shown that $1$ is the maximum depth among all possible common trees.

翻译中…

In the third test case, the figure shows one of the trees with the maximum depth of $3$.

翻译中…

image

翻译中…

样例

Input #1
4
2
1 2
1 2
2
1 2
2 1
5
3 1 5 2 4
4 3 2 1 5
5
2 3 4 1 5
3 2 1 4 5
Output #1
2
1
3
1
InputCopy OutputCopy
题目信息
题号
2187F1
时间限制
3 s
内存限制
512 MB
标签
二分
数据结构
分治
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。