#LCA0000052. Giấc ngủ của chú lười (Sloth Naptime)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Giấc ngủ của chú lười (Sloth Naptime)
Sloth Naptime
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
David's sloth lives on an unweighted tree. In each situation it starts at vertex and wants to travel to vertex , but has energy to traverse at most edges. It always moves along the unique shortest path toward and stops upon arriving or exhausting its movement allowance.
For every query, determine where the sloth stops. If , it stays put.
Input
- The first line contains .
- The next lines describe edges .
- The next line contains .
- Each of the final lines contains .
Output
Print the vertex where the sloth stops, one result per query.
Subtasks
General constraints: ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5
4 2
1 4
5 4
3 4
5
3 5 2
3 5 4
1 5 5
4 5 4
1 5 4
Output
5
5
5
5
5
Explanation
Vertex is adjacent to . The route takes two edges, so both movement allowances and reach . Every remaining query likewise allows enough steps to reach vertex , giving five identical answers.