#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.
- 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 .