PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000051. Đỉnh thứ k trên đường đi (Jump on Tree)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đỉnh thứ k trên đường đi (Jump on Tree)
Jump on Tree
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree with vertices indexed from to . On the unique path from to , the starting vertex occupies index , the next vertex index , and so on.
For each query , return the vertex at zero-based position . If the path contains fewer than vertices, print -1.
Input
- The first line contains .
- The following lines contain edges .
- Each of the last lines contains .
Output
Print one vertex index or -1 per query.
Subtasks
General constraints: (giới hạn PHOJ); ; .
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 4
0 1
1 2
1 3
3 4
2 4 0
2 4 2
2 4 3
2 4 5
Output
2
3
4
-1
Explanation
The path contains vertices at indices respectively. Queries for positions return . Position does not exist, so the answer is -1.