CF685B.Kay and Snowflake

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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).

当一块魔鬼镜子的碎片击中凯的眼睛后,他对玫瑰的美丽便不再感兴趣了。如今,他喜欢观赏雪花。

从前,他发现了一片巨大的雪花,其形状是一棵由 nn 个节点构成的树(即连通无环图)。该树的根节点编号为 11。凯对这棵树的结构非常感兴趣。

经过一番研究,他提出了 qq 个他所关心的查询。其中第 ii 个查询要求找出节点 viv_i 的子树的一个重心。你的任务是回答所有这些查询。

一个节点的子树是指由该节点及其所有后代(直接或间接)所组成的树的一部分。换言之,节点 vv 的子树由所有满足“节点 vv 出现在从 uu 到根节点的路径上”的节点 uu 构成。

一棵树(或一个子树)的重心是一个节点,使得若将该节点从树中移除,则剩余各连通分量中最大者的大小至多为原树(或该子树)大小的一半(即至少减小为原来的 12\frac{1}{2} 倍)。

输入格式

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.

输入的第一行包含两个整数 nn 和 qq(2≤n≤300 0002 \leq n \leq 300\,000,1≤q≤300 0001 \leq q \leq 300\,000),分别表示初始树的大小和查询次数。

第二行包含 n−1n-1 个整数 p2, p3, …, pnp_2,\,p_3,\,\dots,\,p_n(1≤pi≤n1 \leq p_i \leq n),表示节点 22 到 nn 的父节点编号。节点 11 是树的根节点。保证 pip_i 构成一棵合法的树。

接下来的 qq 行中,每行包含一个整数 viv_i(1≤vi≤n1 \leq v_i \leq n),表示我们要求其子树重心的节点编号。

输出格式

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.

对于每个查询,输出对应子树的一个重心的编号。如果存在多个符合条件的节点,输出任意一个即可。题目保证每个子树至少存在一个重心。

输入输出样例

  • 输入#1

    7 4
    1 1 3 3 5 3
    1
    2
    3
    5

    输出#1

    3
    2
    3
    6

说明/提示

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.

第一个查询要求整棵树的重心——即节点 3。若删除节点 3,树将分裂为四个连通分量,其中两个大小为 1,另外两个大小为 2。

第二个节点的子树仅包含该节点自身,因此答案为 2。

节点 3 是其自身子树的重心。

节点 5 的子树的重心是节点 5 和节点 6——两个答案均视为正确。

输入解题思路,AI测评打分。不知道怎么写?

首页