Parent
Very easy
Execution time limit is 1 second
Runtime memory usage limit is 128 megabytes
Write a program that determines for two nodes of a tree whether the first one is a parent of another.
Input
The first line contains the number of vertices in a tree. The second line contains numbers, the -th one gives the vertex number of direct ancestor for the vertex . If this value is zero, then the vertex is a root of a tree.
The third line contains the number of queries . Each of the next lines contains two different numbers and .
Output
For each of the queries print on a separate line number , if the vertex is one of the parents of a vertex , and otherwise.
Examples
Input #1
Answer #1
Submissions 6K
Acceptance rate 26%