MEX on Trees

 Consider a tree with 

N nodes, rooted at node 1.
The value of the ith node (1iN) is denoted by Ai.

Chef defines the function MEX(u,v) as follows:
Let B denote the set of all the values of nodes that lie in the shortest path from u to v (including both u and v). Then, MEX(u,v) denotes the MEX of the set B.

Find the maximum value of MEX(1,i), where 1iN.

Input Format

  • The first line of input contains a single integer T, denoting the number of test cases. The description of the T test cases follows.
  • The first line of each test case contains a single integer N - the number of nodes in the tree.
  • The second line of each test case contains N space separated integers A1,A2,...,ANAi represents the value of the ith node.
  • The following N1 lines contain two space-separated integers X and Y, denoting that there exists an edge between the nodes X and Y.

Output Format

For each test case, output in a single line, the maximum value of MEX(1,i) where 1iN.

Constraints

  • 1T104
  • 1N105
  • 0AiN
  • 1X,YN
  • Sum of N over all test cases does not exceed 3105.

Sample Input 1 

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

Sample Output 1 

2
3
2

Explanation

Test case 1:


  • .
  • 1.
    Thus, the answer is 2.

Test case 2:

  • M
  • .
  • 1.
    Thus, the answer is 3.

Test case 3:


  • .
  • 0.
  • 1,0})=2.
    Thus, the answer is 2.
Previous
Next Post »