Codeforces Round 359 (Div. 1)
B
Kay and Snowflake
Codeforces
1900
原题
← AC →
翻译中…

After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.

翻译中…

Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of $*n*$ nodes. The root of tree has index $1$. Kay is very interested in the structure of this tree.

翻译中…

After doing some research he formed $*q*$ queries he is interested in. The $*i*$-th query asks to find a centroid of the subtree of the node $*v*_{*i*}$. Your goal is to answer all queries.

翻译中…

Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node $*v*$ is formed by nodes $*u*$, such that node $*v*$ is present on the path from $*u*$ to root.

翻译中…

Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).

翻译中…

Input

翻译中…

The first line of the input contains two integers $*n*$ and $*q*$ ($2 ≤ *n* ≤ 300 000$, $1 ≤ *q* ≤ 300 000$) — the size of the initial tree and the number of queries respectively.

翻译中…

The second line contains $*n* - 1$ integer $*p*_{2}, *p*_{3}, ..., *p*_{*n*}$ ($1 ≤ *p*_{*i*} ≤ *n*$) — the indices of the parents of the nodes from $2$ to $*n*$. Node $1$ is a root of the tree. It's guaranteed that $*p*_{*i*}$ define a correct tree.

翻译中…

Each of the following $*q*$ lines contain a single integer $*v*_{*i*}$ ($1 ≤ *v*_{*i*} ≤ *n*$) — the index of the node, that define the subtree, for which we want to find a centroid.

翻译中…

Output

翻译中…

For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.

翻译中…

Note

翻译中…

image

翻译中…

The first query asks for a centroid of the whole tree — this is node $3$. If we delete node $3$ the tree will split in four components, two of size $1$ and two of size $2$.

翻译中…

The subtree of the second node consists of this node only, so the answer is $2$.

翻译中…

Node $3$ is centroid of its own subtree.

翻译中…

The centroids of the subtree of the node $5$ are nodes $5$ and $6$ — both answers are considered correct.

翻译中…

样例

Input #1
7 4
1 1 3 3 5 3
1
2
3
5
Output #1
3
2
3
6
InputCopy OutputCopy
题目信息
题号
685B
时间限制
3 s
内存限制
256 MB
标签
数据结构
DFS 及类似
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。