atcoder#ABC202E. [ABC202E] Count Descendants
[ABC202E] Count Descendants
Score : points
Problem Statement
We have a rooted tree with vertices, numbered .
Vertex is the root, and the parent of Vertex is Vertex .
You are given queries. In the -th query , given integers and , find the number of vertices that satisfy all of the following conditions:
- Vertex is in the shortest path from to the root (including the endpoints).
- There are exactly edges in the shortest path from to the root.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the response to the -th query.
7
1 1 2 2 4 2
4
1 2
7 2
4 1
5 5
3
1
0
0
In the -st query, Vertices , , and satisfy the conditions. In the -nd query, only Vertices satisfies the conditions. In the -rd and -th queries, no vertice satisfies the conditions.