time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 vi. 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 p2, p3, …, pn (1 ≤ pi ≤ 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 pi define a correct tree.

Each of the following q lines contain a single integer vi (1 ≤ vi ≤ 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.

Example

Input

7 4

1 1 3 3 5 3

1

2

3

5

Output

3

2

3

6

Note



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.

【题解】



这个点就是一棵树的“重心”;

考虑每个节点x。找到它的儿子节点里面以该儿子节点为根的子树的大小最大的点记big;

如果size[big]>(size[x]/2);

则这个子树的重心在big节点下面。

否则这个子树的重心就为x;

->有个性质;

两棵树;

如果通过一条边连成了一棵树;那么新的树的重心一定在原来的两棵树的重心的在新的树的路径上。

则我们找到以big为根的节点的子树的重心,然后不断往上走。找到以x为根节点的树的新的重心。(判断依据看代码);

#include <cstdio>
#include <vector> using namespace std; const int MAXN = 4e5; int cnt[MAXN], ans[MAXN],fa[MAXN];
int n, q;
vector <int> a[MAXN]; void dfs(int x)
{
cnt[x] = 1;
ans[x] = x;
int len = a[x].size();
int big = 0;
for (int i = 0; i <= len - 1; i++)
{
int y = a[x][i];
dfs(y);
cnt[x] += cnt[y];
if (cnt[y] > cnt[big])
big = y;
}
if (cnt[big] > cnt[x] / 2)
{
int now = ans[big];
int temp = cnt[x] - cnt[now];
while (temp > (cnt[x] / 2))
{
now = fa[now];
temp = cnt[x] - cnt[now];
}
ans[x] = now;
}
} int main()
{
//freopen("D:\\rush.txt", "r", stdin);
scanf("%d%d", &n, &q);
for (int i = 2; i <= n; i++)
{
int pa;
scanf("%d", &pa);
fa[i] = pa;
a[pa].push_back(i);
}
dfs(1);
for (int i = 1; i <= q; i++)
{
int x;
scanf("%d", &x);
printf("%d\n", ans[x]);
}
return 0;
}

【24.63%】【codefroces 686D】Kay and Snowflake的更多相关文章

  1. codeforces 686D D. Kay and Snowflake(dfs)

    题目链接: D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  2. 【题解】【网络流24题】航空路线问题 [P2770] [Loj6122]

    [题解][网络流24题]航空路线问题 [P2770] [Loj6122] 传送门:航空路线问题 \([P2770]\) \([Loj6122]\) [题目描述] 给出一张有向图,每个点(除了起点 \( ...

  3. 【题解】【网络流24题】汽车加油行驶问题 [P4009] [Loj6223]

    [题解][网络流24题]汽车加油行驶问题 [P4009] [Loj6223] 传送门:汽车加油行驶问题 \([P4009]\) \([Loj6223]\) [题目描述] 给出一个 \(N \times ...

  4. 【资料下载区】【iCore4相关代码、资料下载地址】更新日期2018/02/24

    [iCore4相关文档][更新中...] iCore4原理图(PDF)下载iCore4引脚注释(PDF)下载iCore4机械尺寸(PDF)下载 [iCore4相关例程代码][ARM] DEMO测试程序 ...

  5. 【Android】【录音】Android录音--AudioRecord、MediaRecorder

    [Android][录音]Android录音--AudioRecord.MediaRecorder Android提供了两个API用于实现录音功能:android.media.AudioRecord. ...

  6. 【剑指Offer学习】【全部面试题汇总】

    剑指Offer学习 剑指Offer这本书已经学习完了.从中也学习到了不少的东西,如今做一个总的文件夹.供自已和大家一起參考.学如逆水行舟.不进则退.仅仅有不断地学习才干跟上时候.跟得上技术的潮流! 全 ...

  7. 【CODECHEF】【phollard rho + miller_rabin】The First Cube

    All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...

  8. 【剑指Offer学习】【所有面试题汇总】

    剑指Offer学习 剑指Offer这本书已经学习完了,从中也学习到了不少的东西,现在做一个总的目录,供自已和大家一起参考,学如逆水行舟,不进则退.只有不断地学习才能跟上时候,跟得上技术的潮流! 所有代 ...

  9. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理

    [微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...

随机推荐

  1. 2013腾讯编程马拉松||HDU 4505 小Q系列故事——电梯里的爱情 水水水

    http://acm.hdu.edu.cn/showproblem.php?pid=4505 题目大意: 电梯最开始在0层,并且最后必须再回到0层才算一趟任务结束.假设在开始的时候已知电梯内的每个人要 ...

  2. (转)kvm虚拟机中,如何给子系统更换光盘

    转自:http://www.cnblogs.com/york-hust/archive/2012/06/12/2546334.html 启动kvm后,在kvm窗口中,按下CTRL+ALT+2,切换至q ...

  3. 【AtCoder Beginner Contest 074 D】Restoring Road Network

    [链接]h在这里写链接 [题意] 给你任意两点之间的最短路. 让你求出原图. 或者输出原图不存在. 输出原图的边长总和的最小值. [题解] floyd算法. 先在原有的矩阵上. 做一遍floyd. 如 ...

  4. 【SPOJ 694】Distinct Substrings

    [链接]h在这里写链接 [题意]     给你一个长度最多为1000的字符串     让你求出一个数x,这个x=这个字符串的不同子串个数; [题解]     后缀数组题.     把原串复制一份,加在 ...

  5. libSVM介绍(二)

    鉴于libSVM中的readme文件有点长,并且,都是採用英文书写,这里,我把当中重要的内容提炼出来,并给出对应的样例来说明其使用方法,大家能够直接參考我的代码来调用libSVM库. 第一部分,利用l ...

  6. js实现第一次打开网页弹出指定窗口(常用功能封装很好用)

    js实现第一次打开网页弹出指定窗口(常用功能封装很好用) 一.总结 1.常用功能封装:之前封装的cookie的操作函数非常好用,我自己也可以这么搞 二.js实现第一次打开网页弹出指定窗口 练习1:第一 ...

  7. stm32的ADC外设地址设置算法

    摘自:https://wenku.baidu.com/view/e3ce2a215901020207409c15.html### /////////////////////////////////// ...

  8. 【t069】奇怪的迷宫

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] Mini现在站在迷宫的原点处,公主在[N,N],为了能最快地到达公主处救出公主,Mini希望能走一条最 ...

  9. [RxJS] Use takeUntil instead of manually unsubscribing from Observables

    Manually unsubscribing from subscriptions is safe, but tedious and error-prone. This lesson will tea ...

  10. TEMPDB

    TEMPDB暴涨 阅读目录 前言 正文 原因 解决 补充 回到顶部 前言   tempdb暴增,造成磁盘空间不足,甚至影响业务运行.     回到顶部 正文   如图,tempdb log文件从7.4 ...