You are given a tree with nodes numbered from to , rooted at node , and an array , where represents the value of node .
A sequence of length is called a permutation of order if it contains all numbers from to in any order. For example, the sequences , , and are permutations of order , but the sequences and are not.
Task
You are given queries of the type: If we form a sequence with all the values of the nodes in the subtree of node , is this sequence a permutation of order , where represents the number of nodes in the subtree of node ?
Answer these queries.
Input data
The first line contains the natural number . The next line contains natural numbers, separated by a space, representing the array . The next lines contain pairs of natural numbers , separated by a space, indicating that there is an edge between nodes and . The next line contains the natural number . The following lines contain the nodes , as described above.
Output data
The next lines will contain the message DA
or NU
, representing the answers to the queries.
Constraints and clarifications
- , for
-
# Points Constraints 0 0 Example 1 21 2 23 The tree is a chain 3 27 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 , which is a permutation of order .
For the second query, we can obtain the sequence , which is not a permutation of order , because it does not contain all numbers from to .
For the last query, we obtain the sequence , which is a permutation of order .