PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000043. Tổ tiên chung của một đoạn chỉ số (VO 15 — 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
Tổ tiên chung của một đoạn chỉ số (VO 15 — Tree)
VO 15 — Tree
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices numbered from to and is rooted at vertex . For each query with , find the deepest vertex that is an ancestor of every vertex with index .
This is a range of vertex indices, not the path between vertices and . When , the answer is .
Input
- First line: .
- Next lines contain undirected edges .
- Last lines contain index ranges with .
Output
Print the lowest common ancestor of all vertices in each index range.
Subtasks
General constraints (all subtasks): ; .
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
5 3
1 2
2 3
3 4
3 5
2 5
1 3
4 5
Output
2
1
3
Explanation
Vertices are all in the subtree of , so query returns . Vertices have LCA .