Codeforces Round #359 (Div. 2) 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 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个点),找到一个点,使得删除这个点后剩余连通块点数的最大值*2小于n.

暂且称这给点为ans,对于可一棵树p1,ans必在p1根节点或p1最大子树p2的根节点或最p2最大子树p3的根节点......

在考虑p1最大子树p2根节点是否ans时,不要考虑(p1的节点-p2的节点)*2是否大于p1的节点数,因为如果大于的话,就没答案了,但题目中说必存在至少一个ans.

#include <bits/stdc++.h>
#define maxn 300005
using namespace std;

int root[maxn], ans[maxn], d[maxn];
vector<int> v[maxn];
void dfs(int p, int f){
	
	d[p] = 1;
	int x = p, k = 0;
	ans[p] = p;
	for(int i = 0; i < v[p].size(); i++){
		int h = v[p][i];
		if(h != f){
			dfs(h, p);
			d[p] += d[h];
			if(k < d[h]){
				k = d[h];
				x = h;
			}
		}		
	}
	ans[p] = ans[x];
	while(ans[p] != p && d[p] > d[ans[p]] * 2)
	     ans[p] = root[ans[p]];
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, q, a;
	
	scanf("%d%d", &n, &q);
	for(int i = 2; i <= n; i++){
	    scanf("%d", &a);
	    root[i] = a;
	    v[i].push_back(a);
	    v[a].push_back(i);
	}
	dfs(1, -1);
	while(q--){
		scanf("%d", &a);
		printf("%d\n", ans[a]);
	}
	return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值