PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000071. Truy vấn tổ tiên theo đoạn nhãn (LCA Range Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn tổ tiên theo đoạn nhãn (LCA Range Queries)
LCA Range Queries
Source: LQDOJ
Version: Phuoc Hung OJ Extended
Problem Statement
A tree of vertices labeled is rooted at vertex . Given , find the lowest common ancestor of all vertices with consecutive labels . This interval is over vertex labels, not path positions or DFS-entry times. When , the answer is vertex .
Input
First line: , then edges . Next line: , then queries .
Output
Print one integer (the LCA vertex label) per query.
Subtasks
General constraints: ; ; đầu vào là cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5
1 4
2 5
3 5
2 4
2
2 4
1 3
Output
4
1
Explanation
The LCA of vertices labeled is vertex . Since labels include the root itself, their LCA is .