
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of vertices.
The next ‘N-1’ lines contain two single space-separated integers ‘U’ and ‘V’, denoting an edge from vertex ‘U’ to vertex ‘V’.
The next line will contain a single integer ‘Q’ representing the number of queries.
The next ‘Q’ lines will contain queries in the form as shown.
For each test case, print “YES” if Alice can find Bob, print “NO” if Alice can not find Bob.
The output of each test case will be printed in a new line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= X and Y <= N
1 <= Q <= 10^5
1 <= U,V <= N
Time Limit: 1 sec
The idea is to check whether the first node falls in the subtree of the second node or not, here the first and second node will be determined based on the type of query.
If the query is of type ‘0’ then we will check whether ‘X’ is present in the subtree of ‘Y’ or not. If the query is of type ‘1’ then we will check whether ‘Y’ is present in the subtree of ‘X’ or not.
Now to find the subtrees of each node we will maintain two arrays which will store an in-time and an out-time during DFS calls. A node ‘A’ will be in the subtree of node ‘B’ if the in-time of ‘A’ is smaller than ‘B’ and the out-time of ‘A’ is greater than 'B’.