{"problem":{"name":"Count Descendants","description":{"content":"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","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc202_e"},"statements":[{"statement_type":"Markdown","content":"We have a rooted tree with $N$ vertices, numbered $1, 2, \\dots, N$.\nVertex $1$ is the root, and the parent of Vertex $i \\, (2 \\leq i \\leq N)$ is Vertex $P_i$.\nYou 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:\n\n*   Vertex $U_i$ is in the shortest path from $u$ to the root (including the endpoints).\n*   There are **exactly** $D_i$ edges in the shortest path from $u$ to the root.\n\n## Constraints\n\n*   $2 \\leq N \\leq 2 \\times 10^5$\n*   $1 \\leq P_i < i$\n*   $1 \\leq Q \\leq 2 \\times 10^5$\n*   $1 \\leq U_i \\leq N$\n*   $0 \\leq D_i \\leq N - 1$\n*   All values in input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$N$\n$P_2$ $P_3$ $\\ldots$ $P_N$\n$Q$\n$U_1$ $D_1$\n$U_2$ $D_2$\n$\\vdots$\n$U_Q$ $D_Q$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc202_e","tags":[],"sample_group":[["7\n1 1 2 2 4 2\n4\n1 2\n7 2\n4 1\n5 5","3\n1\n0\n0\n\nIn 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.\n![image](https://img.atcoder.jp/ghi/abc202_e_sample_00.jpg)"]],"created_at":"2026-03-03 11:01:14"}}