PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000053. Đúng k bước (Exactly K Steps)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đúng k bước (Exactly K Steps)
Exactly K Steps
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Given an undirected tree with vertices, every edge counts as one step. For each query , find any vertex whose distance from is exactly edges.
If multiple such vertices exist, any one is accepted. If none exists, print -1.
Input
- The first line contains .
- Each of the next lines contains an edge .
- The next line contains the number of queries .
- Each of the last lines contains .
Output
Print one valid vertex per query or -1 if impossible. More than one output may be correct.
Subtasks
General constraints: ; ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5
1 2
2 3
3 4
3 5
3
2 2
5 3
3 3
Output
4
1
-1
Explanation
From vertex , both vertices and are two edges away; the sample chooses . From vertex , the route is three edges long, so the answer can be . No vertex is three edges away from , hence -1.