#abc267f. F - Exactly K Steps
F - Exactly K Steps
Score : points
问题描述
给定一棵包含 个顶点的树。顶点编号为 ,第 个()边连接顶点 和 。
我们定义此树中顶点 和顶点 之间的 距离 为从顶点 到顶点 的最短路径上的边数。
你将得到 个查询。在第 个查询()中,给定整数 和 ,输出距离顶点 正好为 的任意一个顶点的索引。如果没有这样的顶点,则输出 -1
。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a tree with vertices. The vertices are numbered , and the -th () edge connects Vertices and .
We define the distance between Vertices and on this tree by the number of edges in the shortest path from Vertex to Vertex .
You are given queries. In the -th () query, given integers and , print the index of any vertex whose distance from Vertex is exactly . If there is no such vertex, print -1
.
Constraints
- The given graph is a tree.
- 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 index of any vertex whose distance from Vertex is exactly if such a vertex exists; if not, it should contain -1
. If there are multiple such vertices, you may print any of them.
Sample Input 1
5
1 2
2 3
3 4
3 5
3
2 2
5 3
3 3
Sample Output 1
4
1
-1
- Two vertices, Vertices and , have a distance exactly from Vertex .
- Only Vertex has a distance exactly from Vertex .
- No vertex has a distance exactly from Vertex .
Sample Input 2
10
1 2
2 3
3 5
2 8
3 4
4 6
4 9
5 7
9 10
5
1 1
2 2
3 3
4 4
5 5
Sample Output 2
2
4
10
-1
-1
update @ 2024/3/10 11:17:06