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

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 0001 ≤ 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值