#LCA0000031. Tổ tiên chung từ danh sách con (Lowest Common Ancestor)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổ tiên chung từ danh sách con (Lowest Common Ancestor)
Lowest Common Ancestor
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a rooted tree with vertices numbered from to . For each vertex, the input directly lists its children; thus the root need not be vertex 1. The root is the unique vertex that is never listed as a child.
For each query , find the lowest common ancestor. Unlike the original SPOJ format with multiple cases and Case X: labels, the PHOJ format contains one dataset and prints only the query answers.
Input
- First line: .
- For each , a line starts with the number of children , then lists those child indices.
- Next line: .
- Last lines: query vertices .
Output
Print one LCA per query without any Case X: prefix.
Subtasks
General constraints (all subtasks): ; the sum of all child counts is .
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; .
- Subtask 3 (50 points): ; .
Examples
Input
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7
Output
3
1
Explanation
Vertices share parent , so the first answer is . Vertices are in different branches of root , so the second answer is .