エイシングプログラミングコンテスト2021(AtCoder Beginner Contest 202)
E
Count Descendants
AtCoder
⌀ 1638
原题
← DF →
翻译中…

Problem Statement

翻译中…

We have a rooted tree with $N$ vertices, numbered $1, 2, \dots, N$.

翻译中…

Vertex $1$ is the root, and the parent of Vertex $i \, (2 \leq i \leq N)$ is Vertex $P_i$.

翻译中…

You are given $Q$ queries. In the $i$-th query $(1 \leq i \leq Q)$, given integers $U_i$ and $D_i$, find the number of vertices $u$ that satisfy all of the following conditions:

翻译中…
  • Vertex $U_i$ is in the shortest path from $u$ to the root (including the endpoints).
  • There are exactly $D_i$ edges in the shortest path from $u$ to the root.
翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq P_i < i$
  • $1 \leq Q \leq 2 \times 10^5$
  • $1 \leq U_i \leq N$
  • $0 \leq D_i \leq N - 1$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
P_2 P_3 \ldots P_N
Q
U_1 D_1
U_2 D_2
\vdots
U_Q D_Q

翻译中…

Output

翻译中…

Print $Q$ lines. The $i$-th line should contain the response to the $i$-th query.

翻译中…

样例

Input #1
7
1 1 2 2 4 2
4
1 2
7 2
4 1
5 5
Output #1
3
1
0
0
In the $1$-st query, Vertices $4$, $5$, and $7$ satisfy the conditions. In the $2$-nd query, only Vertices $7$ satisfies the conditions. In the $3$-rd and $4$-th queries, no vertice satisfies the conditions. ![sample](https://img.atcoder.jp/ghi/abc202_e_sample_00.jpg)
题目信息
题号
abc202_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。