P3019 [USACO11MAR] Meeting Place S
Description
Bessie and Jonell are great friends. Since Farmer John scrambles where the cows graze every day, they are sometimes quite far from each other and can't talk.
The pastures and paths on FJ's farm form a 'tree' structure. Each pasture has exactly one distinct path to any other pasture, and each pasture (except pasture #1, the 'root') also has a single parent node.
Bessie and Jonell have decided that they will always meet at the closest pasture that that is both an ancestor of Jonell's pasture and of Bessie's pasture.
FJ created a map of his N (1
Input Format
\* Line 1: Two space-separated integers: N and M
\* Lines 2..N: Line i contains a single integer that describes the parent of pasture i: P\_i
\* Lines N+1..N+M: Line k+N describes Bessie and Jonell's respective pastures with two space-separated integers: B\_k and J\_k
Output Format
\* Lines 1..M: Line j contains the meeting place Bessie and Jonell would use for line j+N of the input