#abc202e. E - Count Descendants
E - Count Descendants
Score : points
问题描述
我们有一棵包含 个顶点的有根树,顶点编号为 。
顶点 是树的根节点,且顶点 的父节点为顶点 。
你将得到 个查询。在第 个查询 中,给定整数 和 ,找出满足以下所有条件的顶点 的数量:
- 从顶点 到根节点的最短路径中包含顶点 (包括端点)。
- 从顶点 到根节点的最短路径中恰好包含 条边。
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
7
1 1 2 2 4 2
4
1 2
7 2
4 1
5 5
Sample Output 1
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.
update @ 2024/3/10 09:14:45