您的位置:首页 > 其它

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

2016-08-26 23:08 429 查看
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 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个点,以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]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 686 D