问题 I: Tree

内存限制:512 MB 时间限制:10 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

There is a rooted tree of n vertices with the root in vertex 1, each vertex has zero or one heavy child linked with a heavy edge, a set of maximal connected heavy edges form a heavy chain. A single vertex can also form a heavy chain, so each vertex belongs to exactly one heavy chain. Now we want to build a search tree to maintain some information of the original tree. For each heavy chain with k vertices, we construct a segment tree of depth dlog2 2ke to maintian it, with each leaf of the segment tree indicating a vertex on the heavy chain, and the parent of the segment tree’s root is the parent of the top vertex of the heavy chian. Note that in our variant of the segment tree, every leaves have the same depth. For instance, this is a possible original tree, note that a double slash indicating a heavy edge.
                    1
                  // \
                  2   7
                //    \\
                3       8
              //        \\
              4           9
            // \
            5  10
           /
          6
There are 4 heavy chain in this tree. They are: 1 → 2 → 3 → 4 → 5 , 6 , 7 → 8 → 9, 10 This is the search tree of the previous origin tree, note that the double slash here has no special meaning, just make the search tree looks more beautiful.
                    o
                  // \\
                 o     o
               // \\   //
              o    o   o
             / \  / \ /
            1  2 3  4 5
            |       | |
            o      10 6
           / \
          o   o
         / \  /
        7   8 9
Given such a original tree, your task is to calculate the depth of the search tree. The depth of the root is 1.

输入格式

The first line of the input contains an integer T (1 ≤ T ≤ 20) , indicating the number of the test cases. For each test case: The first line of the input contains an interger n (1 ≤ n ≤ 106 ) , indicating the number of the vertices in the original tree. The next lines has n integers, indicating the parent of each vertex. Note that 0 indicating don’t have a parent. The next lines has n integers, indicating the heavy child of each vertex. Note that 0 indicating don’t have a heavy child. It’s guaranteed that Pn ≤ 107 .

输出格式

T lines, each line has one interger, indicating the answer.

输入样例 复制

1
10
0 1 2 3 4 5 1 7 8 4
2 3 4 5 0 0 8 9 0 0

输出样例 复制

7

数据范围与提示

Note that the input scale is extremely large.