求所有子树的重心,利用重心性质合并子树重心

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:

1
2
3
4
5
6
7
8
9
10
11
12
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 size 1 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<=300000,朴素的求重心把所有点跑一遍显然是不可能的。所以我们考试求出子树的重心,再推出当前重心。

对于当前点u,假设我们已经求出它各个子树x,y,z的重心,会有一下结论:
重心一定在 u到最大的子树(x)的重心 的路径上。
那么就可以从x的重心上出发,一步一步向父亲跳,直到到u,期间每步比较一下跳到父亲节点是否更优。所谓更优可以比较siz[v](表示v子树大小)是否小于1/2*siz[u],因为在向上跳的过程中siz[v]一直增大,我们知道树的重心满足最大的子树siz < 1/2整个树,故最大的一个小于1/2siz[u]的siz[v]必是重心。
故在dfs时求出每个子树的siz,最大的儿子,在回溯时更新父亲的重心即可。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
const int N=300010;
int head[N],nxt[N<<1],pnt[N<<1],E;
void add(int u,int v) {
E++;pnt[E]=v;nxt[E]=head[u];head[u]=E;
}
int n,q;
int sz[N],Z[N],fa[N];
void dfs(int u,int f) {
cout<<u<<" "<<f<<endl;
sz[u]=1;
Z[u]=u;
int mxs=0;
for(int i=head[u];i;i=nxt[i]) {
int v=pnt[i];
if(v==f) continue;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[mxs]) mxs=v;
}
if(mxs==0) return ;
int v=Z[u]=Z[mxs];
while(v!=u) {
int fv=fa[v];
if(2*sz[v]-sz[u]<0) {
Z[u]=v=fv;
}
else {
break;
}
}
}
int main() {
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++) {
int x;
scanf("%d",&x);
fa[i]=x;
add(x,i);add(i,x);
}
dfs(1,0);
while(q--) {
int x;
scanf("%d",&x);
printf("%d\n",Z[x]);
}
}