atcoder#ABC267F. [ABC267F] Exactly K Steps
[ABC267F] Exactly K Steps
Score : points
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.
5
1 2
2 3
3 4
3 5
3
2 2
5 3
3 3
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 .
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
2
4
10
-1
-1