#Q137. 「一本通 4.4 练习 2」祖孙询问
「一本通 4.4 练习 2」祖孙询问
Description
Given a rooted tree with nodes. There are queries, each query provides a pair of node numbers and , asking about the ancestor-descendant relationship between and .
Input Format
The first line of input contains an integer representing the number of nodes;
The next lines each contain a pair of integers and indicating there is an edge between and . If is , then is the root of the tree;
The -th line is an integer representing the number of queries;
The next lines each contain two positive integers and , representing a query.
Output Format
For each query, if is an ancestor of , output ; if is an ancestor of , output ; otherwise, output .
Sample 1
10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19
1
0
0
0
2
Data Range and Hints
For of the data, ;
For of the data, , and each node's number does not exceed .