sap

Time limit: 1s Memory limit: 128MB Input: Output:

You are given a tree with NN nodes numbered from 11 to NN, rooted at node 11, and an array VV, where ViV_i represents the value of node ii.

A sequence of length NN is called a permutation of order NN if it contains all numbers from 11 to NN in any order. For example, the sequences [1,2,3][1, 2, 3], [2,1,3][2, 1, 3], and [3,2,1][3, 2, 1] are permutations of order 33, but the sequences [1,4,3][1, 4, 3] and [1,2,2][1, 2, 2] are not.

Task

You are given QQ queries of the type: If we form a sequence with all the values of the nodes in the subtree of node XX, is this sequence a permutation of order KK, where KK represents the number of nodes in the subtree of node XX?

Answer these queries.

Input data

The first line contains the natural number NN. The next line contains NN natural numbers, separated by a space, representing the array VV. The next lines contain N1N - 1 pairs of natural numbers (a,b)(a, b), separated by a space, indicating that there is an edge between nodes aa and bb. The next line contains the natural number QQ. The following QQ lines contain the nodes XX, as described above.

Output data

The next QQ lines will contain the message DA or NU, representing the answers to the QQ queries.

Constraints and clarifications

  • 1N,Q500 0001 \leq N, Q \leq 500\ 000
  • 1a,bN1 \leq a, b \leq N
  • 1ViN1 \leq V_i \leq N, for 1iN1 \leq i \leq N
  • 1XN1 \leq X \leq N
    # Points Constraints
    0 0 Example
    1 21 N,Q100N, Q \leq 100
    2 23 The tree is a chain
    3 27 N,Q100 000N, Q \leq 100\ 000
    4 29 No additional constraints

Note

Due to the very large input data, the following instructions will be used at the beginning of the main() function in the C++ program:

ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

Example

stdin

8
6 5 4 7 8 2 1 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
6
1
2
6
3
5
7

stdout

DA
NU
DA
DA
NU
DA

Explanation

For the first query, we can obtain the sequence [1,2,3,4,5,6,7,8][1, 2, 3, 4, 5, 6, 7, 8], which is a permutation of order 88.

For the second query, we can obtain the sequence [5,7,8][5, 7, 8], which is not a permutation of order 33, because it does not contain all numbers from 11 to 33.

For the last query, we obtain the sequence [1][1], which is a permutation of order 11.

Log in or sign up to be able to send submissions!