PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000033. Thư viện truy vấn LCA tốc độ cao (Lowest Common Ancestor)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Thư viện truy vấn LCA tốc độ cao (Lowest Common Ancestor)
Lowest Common Ancestor
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem Statement
A tree rooted at vertex has vertices indexed from to . Every vertex has a parent .
Answer many lowest-common-ancestor queries LCA. This is a library-style performance test with a potentially large number of nodes and queries. Each query satisfies .
Input
- First line: .
- Second line: parents .
- Next lines: query vertices with .
Output
Print each LCA in query order.
Subtasks
General constraints (all subtasks): ; ; .
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
5 5
0 0 2 2
0 1
0 4
1 2
2 3
3 4
Output
0
0
0
2
2
Explanation
Query has LCA ; lies within the subtree rooted at and also has LCA .