您的位置:首页 > 其它

Kay and Snowflake CodeForces - 686D(树形dp,树的重心)

2017-08-07 14:20 417 查看
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 thatpi 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
题目大意:求任意一个子树的重心节点编号;
题目思路:
一开始的话我们都会想到根据暴力求每一个子树的重心位置,但是显然是不行的,不仅因为查询次数过多会导致超时,而且会非常麻烦,下面介绍关于重心定义与性质:
定义:1,大家都知道的以某一个点为根节点时,最大子树节点个数最小,则这个节点为树的重心。
2,另一种定义,本题将用到,以某个子树为根节点时,最大子树节点个数不超过总结点个数的一 半;
性质:
1.全部节点都某个点的距离和,到重心的距离和最小。
2.两个树通过一条边连接成一棵树后,新的重心在两棵树的重心之间的路径中
3.添加或者删除一个节点的时候,重心的距离最多移动一条边的距离。
这道题的话,我们可以想到利用第二个性质,从叶子节点回溯的时候,当成两个树的合并,去寻找新的树的重心,对于每一次回溯,我们找到最多节点数的树,如果大于当前树的一半,那么新的节点一定存在于这个最大子树中,这样的话,我们从这个子树的重心位置依次从上找,知道找到新的重心 复杂度最多为2*n
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<iostream>
#include<sstream>
#include<cmath>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 3e5+10;
int son[maxn];
int ans[maxn];
int par[maxn];
int head[maxn];
int tot;
int n,q;
struct node
{
int u,v;
int net;
}E[maxn*2];
void __init__()
{
memset(head,-1,sizeof(head));
tot = 0;
memset(ans,0,sizeof(ans));
memset(par,0,sizeof(par));
memset(son,0,sizeof(son));
}
void build(int u,int v)
{
E[tot].u = u;
E[tot].v = v;
E[tot].net = head[u];
head[u] = tot++;
}
void dfs(int u)
{
ans[u] = u;
son[u] = 1;
int mx = 0;
for(int i = head[u];~i;i = E[i].net){
int to = E[i].v;
dfs(to);
son[u]+=son[to];
if(son[to]>son[mx])
mx = to;
}
if(son[mx]*2>son[u]){
int now = ans[mx];
while((son[u]-son[now])*2>son[u])
{
now = par[now];
}
ans[u] = now;
}
}
int main()
{
while(~scanf("%d%d",&n,&q)){
__init__();
for(int i = 2;i<=n;i++){
scanf("%d",&par[i]);
build(par[i],i);
}
dfs(1);
while(q--){
int x;
scanf("%d",&x);
printf("%d\n",ans[x]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM dp