P27333 [AtCoder] Large Corporation
NOI
题目描述

Problem Description

A certain corporation has $N$ employees, numbered as employees $1, \ldots, N$. Among them, one is the president and has no supervisor. Each of the other employees has exactly one direct supervisor. According to your database, the direct supervisor of employee $i$ $(1 \leq i \leq N)$ is employee $p_i$ (except when employee $i$ is the president, in which case $p_i = -1$). There are no cycles in this hierarchy.

Using this data, you want to create a service that, upon inputting two employee numbers $a$ and $b$, determines if employee $a$ is a subordinate of employee $b$. Here, employee $a$ is considered a subordinate of employee $b$ if you can reach employee $b$ by following the direct supervisors from employee $a$.

You are given $Q$ pairs of employee numbers $(a_1, b_1), \ldots, (a_Q, b_Q)$ to be inputted into the service. Create a program that outputs Yes or No for each of these pairs.

Input Format

The input is provided from standard input in the following format:

$N$ $p_1$ $p_2$ $:$ $p_N$ $Q$ $a_1$ $b_1$ $a_2$ $b_2$ $:$ $a_Q$ $b_Q$

Output Format

Output $Q$ lines. The $i$-th line $(1 \leq i \leq Q)$ should be Yes if employee $a_i$ is a subordinate of employee $b_i$, and No otherwise.

Sample Input and Output

Sample Input #1

7
-1
1
1
2
2
3
3
6
7 1
4 1
2 3
5 1
5 2
2 5

Sample Output #1

Yes
Yes
No
Yes
Yes
No

Sample Input #2

20
4
11
12
-1
1
13
13
4
6
20
1
1
20
10
8
8
20
10
18
1
20
18 14
11 3
2 13
13 11
10 15
9 5
17 11
18 10
1 16
9 4
19 6
5 10
17 8
15 8
5 16
6 20
3 19
10 12
5 13
18 1

Sample Output #2

No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
Yes
No
Yes
No
No
No
Yes

Notes/Hints

Note

Discussion of this problem is prohibited until December 29, 2019, 05:00 JST. If mentioned, legal action may be taken.

It is okay to publish the total score and certification level after the exam, but please refrain from disseminating information about which problems were solved.

Constraints

  • $2 \leq N \leq 150,000$
  • For each $i = 1, \ldots, N$, $p_i = -1$ or $1 \leq p_i \leq N$
  • There is exactly one $i$ such that $p_i = -1$.
  • There are no cycles in the hierarchy among employees.
  • $1 \leq Q \leq 100,000$
  • $1 \leq a_i, b_i \leq N$
  • $a_i \neq b_i$

Sample Explanation 1

Employee $1$ (the president) has direct subordinates $2$ and $3$, employee $2$ has direct subordinates $4$ and $5$, and employee $3$ has direct subordinates $6$ and $7$. For example, for the first pair $(7, 1)$, since employee $1$ is the direct supervisor of employee $3$, who is the direct supervisor of employee $7$, the output is Yes.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
7 -1 1 1 2 2 3 3 6 7 1 4 1 2 3 5 1 5 2 2 5
样例输出
Yes Yes No Yes Yes No