Codeforces 686 D Kay and Snowflake【树的重心】

D. Kay and Snowflake
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 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 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个点,以1为根,然后给你2-n的节点的父亲节点编号。Q个询问,问以询问的节点为根的重心编号。


思路:


1、思路来源自各博客:

1.树中所有点到某个点的距离和中,到重心的距离和是最小的;如果有两个重心,那么他们的距离和一样。 
2.把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上。 
3.把一个树添加或删除一个叶子,那么它的重心最多只移动一条边的距离。 


2、本题使用的就是第二条定理。如果深搜此树,在回溯的时候,遇到一个以u为节点的子树,如果其u的临接点v是u的重儿子(节点u的儿子中规模最大的子树的节点)。那么两个部分相连之后的新的重心在u到以v为根的子树的重心这条链上。


3、那么再根据一条就能枚举出这个新的重心:

①树的重心的定义:以这个点为根,那么其所有的子树的大小都不超过整个树的一半。

那么就有:

②以u为根的一颗树,如果有某儿子:该儿子的子树节点个数和>以u为根的树的节点的个数,那么重心在以v为根的子树之中。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,q;
vector<int >mp[300100];
int ans[300100];
int son[300100];
int fa[300100];
void Dfs(int u)
{
    son[u]=1;
    ans[u]=u;
    int maxn=0;
    for(int i=0; i<mp[u].size(); i++)
    {
        int v=mp[u][i];
        Dfs(v);
        son[u]+=son[v];
        if(son[v]>son[maxn])maxn=v;
    }
    if(son[maxn]*2>son[u])
    {
        int now=ans[maxn];
        while((son[u]-son[now])*2>son[u])
        {
            now=fa[now];
        }
        ans[u]=now;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=2; i<=n; i++)
        {
            scanf("%d",&fa[i]);
            mp[fa[i]].push_back(i);
        }
        Dfs(1);
        while(q--)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",ans[x]);
        }
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值