Family Reunion
InterviewTime limit1sMemory limit1024 MB
Given a family tree as a parent array, find the closest common ancestor of M seated people, which may be one of them.
- Level
Easy3 of 10
- Topics
- Tree, Implementation
- Solved
- No attempts yet
Problem

A family reunion is held for the descendants of Ida-Ottilia Isaksson. For simplicity, a family tree has been drawn up. All descendants are numbered from to , and Ida-Ottilia herself is given the number .
Among the people at your table, a discussion starts about who their nearest common relative is (closest going up the tree). Write a program that computes this. Note that this sometimes can be someone sitting at the table.
Input
The first line contains the numbers and (). The second line contains numbers, the parent of each descendant (all between and ). The third line contains numbers, the people around the table (all between and , with no duplicates).
Output
Print a single number: the number of the nearest common relative of the people at the table.