D. Kay and Snowflake
 
 

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

题意:

  给你一棵树,n点n-1边,m个询问,每次询问你x点及其子树中选择一个点删除,最后这个子树的形成的树枝中节点树不超过siz[x]/2,输出这个点

题解:

  假设x点的答案是ans[x];

  对于新的点x我们会在那个儿子的子树中选点去删除或者本身呢,一定是在siz[son]最大的儿子里面选点

  假设新x点的答案不能构成<=siz[x]/2 那么加我们选的点想起父亲节点走上去再更新就好了,直到有解

#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+,inf = 2e9, mod = 1e9+;
typedef long long ll; vector<int > G[N];
int n,m,x,siz[N],mxs[N],ans[N],fa[N];
void dfs(int u,int f)
{
siz[u]=;
ans[u]=u;
mxs[u]=u;
int mx=;
for(int i=;i<G[u].size();i++)
{
int to= G[u][i];
if(to==f) continue;
dfs(to,u);
siz[u]+=siz[to];
if(mx<siz[to])
{
mx=siz[to];
mxs[u]=to;
}
}
ans[u]=ans[mxs[u]];
while(ans[u]!=u&&siz[u]-siz[ans[u]] > siz[u]/) ans[u] = fa[ans[u]];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
G[x].push_back(i);
G[i].push_back(x);
fa[i]=x;
}
dfs(,);
for(int i=;i<=m;i++)
{
scanf("%d",&x);
printf("%d\n",ans[x]);
}
return ;
}

Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP的更多相关文章

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

  2. Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs

    B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...

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

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

  4. Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

    题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...

  5. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  6. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  7. Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp

    题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...

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

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

随机推荐

  1. WPF 动态布局Grid

    //开启线程加载 Action a = () => { ; ; var path = "../../face_img/"; var files = Directory.Get ...

  2. 如何使用Unix/Linux grep命令——磨刀不误砍柴工系列

     http://man.linuxde.net/grep ---------------------------------------------------- 如何使用Unix/Linux gre ...

  3. 微信稳居Android App排行榜4月份国内榜首

    根据某机构通过对Android样本访问行为的持续监测数据进行样本属性加权并根据iOS/Android用户调研数据建模推总得出中国移动互联网用户规模以及相应的用户结构数据显示,2015年4月份国内And ...

  4. 没玩过这些微信小游戏你就out了

    你确定没玩过下面这些微信小游戏?是不是有点out了?赶紧添加微信号kangfuyk,回复H5马上畅玩! 当然了,扫一下二维码关注后回复H5更快捷噢! 微信小游戏列表,持续更新中 辨色大比拼!心理游戏 ...

  5. cocos2d调度器(定时执行某函数)

    调度器(scheduler) 继承关系 原理介绍 Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或 ...

  6. Jackson 框架,轻易转换JSON

    Jackson 框架,轻易转换JSON Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 前面有介绍过json-lib这个框架,在 ...

  7. ubuntu硬盘安装卡在探测文件系统

    在硬盘安装ubuntu的时候,会出现这样的问题:安装程序一直卡在正在探测文件系统就不动了.解决的方法很简单.在安装之前要在终端输入sudo空格umount空格 -l空格 /isodevice 不能少一 ...

  8. FOJ 1075

    #include<stdio.h> #include<iostream> using namespace std; ]={,}; ]={}; void func() { ,fl ...

  9. js中的闭包之我理解

    闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实在是比较晦涩,对我来说也是一样. 但是他也是js能力提升中无法绕过的一环,几乎每次面试必问的问题,因为在回答的时候.你的答案的深度,对术语的 ...

  10. jquery优势

    1.轻量 2.开源 3.选择器出色 可以支持几乎 css1到css3 的所有选择器 4.简单的修改页面    不同的浏览器对于css的支持程度是不同的,jquery通过封装javascript的代码, ...