#LCA0000076. Tổ tiên chung của nhiều đỉnh (Lowest Common Ancestor of a Binary Tree IV)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổ tiên chung của nhiều đỉnh (Lowest Common Ancestor of a Binary Tree IV)
Lowest Common Ancestor of a Binary Tree IV
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A rooted binary tree is given together with several queries. Each query specifies a set of distinct vertices. Find the deepest vertex that is an ancestor of every specified vertex. A vertex is its own ancestor. The PHOJ version uses integer left/right child indices rather than LeetCode TreeNode pointers.
Input
First line: . For each vertex read ( means no child). Next read . Each query contains followed by vertex indices.
Output
Print the requested LCA index on a separate line for each query.
Subtasks
General constraints: ; ; các đỉnh mỗi truy vấn phân biệt; tổng trên tất cả truy vấn không vượt ; chỉ số con thuộc và mô tả đúng một cây nhị phân.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 1
2 3
4 5
0 0
0 0
0 0
3
2 4 5
2 4 3
3 3 4 5
Output
2
1
1
Explanation
Vertices share LCA . Adding vertex to the set moves the joint LCA to vertex .