B. Kay and Snowflake

题目连接:

http://www.codeforces.com/contest/685/problem/B

Description

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.

Sample Input

7 4

1 1 3 3 5 3

1

2

3

5

Sample Output

3

2

3

6

Hint

给你一棵树,Q次询问,每次询问这个点的中心是啥

题解:

离线做

重心是啥?就是砍掉这个点之后,剩下的树的大小都小于原树大小的1/2

那么我把这个点的所有子树都看一下,把最接近1/2的那个子树剁掉就好了,剩下的肯定满足……

这个启发式合并一下,nlognlogn很容易实现

但是其实,直接dfs一波就好了,递归处理,那个儿子肯定是在他的重儿子那儿,然后不停往上爬就好饿了

复杂度均摊一下,其实没有多大。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
vector<int>E[maxn];
int n,q,sz[maxn],ans[maxn],f[maxn];
void dfs(int x){
ans[x]=x;
sz[x]=1;
for(int i=0;i<E[x].size();i++){
int v=E[x][i];
dfs(v);
sz[x]+=sz[v];
}
for(int i=0;i<E[x].size();i++){
if(sz[E[x][i]]*2>sz[x])
ans[x]=ans[E[x][i]];
}
while((sz[x]-sz[ans[x]])*2>sz[x])
ans[x]=f[ans[x]];
}
int main(){
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++){
scanf("%d",&f[i]);
E[f[i]].push_back(i);
}
dfs(1);
for(int i=1;i<=q;i++){
int x;scanf("%d",&x);
printf("%d\n",ans[x]);
}
}

Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs的更多相关文章

  1. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP

    D. Kay and Snowflake     After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...

  2. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心

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

  3. Codeforces Round #359 (Div. 2) D - Kay and Snowflake

    D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...

  4. Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)

    题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...

  5. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  6. Codeforces Round #359 (Div. 2)C - Robbers' watch

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces Round #359 (Div. 1)

    A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...

  8. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  9. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

随机推荐

  1. ASP.NET 实现Base64文件流下载PDF

    因为业务需要调用接口获取的是 Base64文件流 需要提供给客户下载PDF文档 源码部分借鉴网上,具体地址忘记了. //Base64文件流 byte[] buffer = Convert.FromBa ...

  2. Windows版Oracle重建EM---备注

    前提条件添加环境变量 ORACLE_HOSTNAME=<主机名:如:DESKTOP-P6J1a>ORACLE_SID=orclORACLE_UNQNAME=orcl 执行删除命令 C:\U ...

  3. python网络编程-动态导入和断言

    一:动态导入importlib 在程序运行的过程中,根据变量或者配置动态的决定导入哪个模块,可以使用模块importlib importlib使用示例 二:断言assert 如果接下来的程序依赖于前面 ...

  4. 移动端,PC端,微信等常用平台和浏览器判断

    var wzw={ //浏览器相关信息 //android webview 需要app进行支持,Android web view初始化时,在navigator中添加标识 browser:{ versi ...

  5. IntelliJ IDEA 把Json字符串 增加到IDE里 用windows记事本 能自动转换(自动增加转义字符)

  6. TypeScript 2 : 获取当前日期及前后范围日期【Array】

    前言 今天有个接口字段需求,要写一个今天及前几天的日期传过去: 在网上找了下都木有什么比较好的方案:就自己写了一个. 因为技术栈就是NG2+TS2+WEBPACK,这里的代码需要一定的TS2及ES6的 ...

  7. Java集合类 课后练习

    1.Pg235--2分别向Set集合以及List集合中添加“A”,“a” , "c" , "C" , "a"  5个元素,观察重复值“a”能 ...

  8. Asp.net MVC4 +EF6开发的个人网站源码和介绍(仅供新手学习)

    本项目是我去年利用业余时间开发的,采用的是asp.net mvc 4 +EF6+三层架构,适合新手进行学习,高手就没有什么价值了,可以直接跳过. 源码和数据库下载(已上传到git):https://g ...

  9. 6-14 Inspector s Dilemma uva12118(欧拉道路)

    题意:给出一个国家城市个数n   所需走过道路个数e   每条道路长t   该国家任意两个城市之间都存在唯一道路长t     要求 :找一条最短的路遍历所有所需走过的路 一开始以为是图的匹配  但是好 ...

  10. Ionic Js七:手势事件

    1.on-hold 长按的时间是500毫秒. HTML 代码 <button on-hold="onHold()" class="button">长 ...