题目链接:

题目

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).

输入

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.

输出

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.

样例

input

7 4

1 1 3 3 5 3

1

2

3

5

output

3

2

3

6

题意

求需要查询的子树的重心。

题解

对于点u,它的重心会在它的以重儿子为根的子树的重心和u的路径上。

可以用dfs直接暴力递归。(如果u的重儿子子树的重心深度比较高,那么u的重心深度也会比较高,严格的时间证明也不懂。。)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int maxn = 3e5 + 10; int n, m; int siz[maxn], ans[maxn],f[maxn];
vector<int> G[maxn];
void dfs(int u) {
siz[u] = 1; ans[u] = u;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
siz[u] += siz[v];
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (siz[v] * 2 > siz[u]) {
ans[u] = ans[v];
break;
}
}
while ((siz[u] - siz[ans[u]]) * 2>siz[u]) ans[u] = f[ans[u]];
} int main() {
scanf("%d%d", &n, &m);
for (int i = 2; i <= n; i++) {
scanf("%d", f + i);
G[f[i]].push_back(i);
}
dfs(1);
while (m--) {
int v; scanf("%d", &v);
printf("%d\n", ans[v]);
}
return 0;
}

Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心的更多相关文章

  1. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP

    D. Kay and Snowflake     After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...

  2. Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs

    B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...

  3. Codeforces Round #359 (Div. 2) D - Kay and Snowflake

    D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...

  4. Codeforces Round #359 (Div. 1)

    A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...

  5. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  6. D. Kay and Snowflake 树的重心

    http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...

  7. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  8. Codeforces Round #359 (Div. 2)C - Robbers' watch

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)

    题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...

随机推荐

  1. IOS 模仿TableView封装

    一.先贴一下未封装的代号,好跟后面的对比 @interface MTHomeDropdown : UIView + (instancetype)dropdown; @property (nonatom ...

  2. PHP调用WebService

    1.  环境配置 配置php.ini,把php_soap.dll前面的分号去掉, 配置完成,需要重启. 2.  PHP调用代码,如下 <?php try { $soap = new SoapCl ...

  3. 20150311—html中iframe(转发)

    JS实现iframe框架页面跳转和刷新 一.js方式的页面跳转 1.window.location.href方式 <script language="javascript" ...

  4. 20171107--SQL变量,运算符,存储过程

    create database shujuku03 go use shujuku03 go create table jiaoshi--创建jiaoshi表-- ( code int primary ...

  5. [javascript|基本概念|Unll]学习笔记

    Uull类型的值:null(只有一个)|空对象指针|typeof操作符返回object 作用:一般用于对即将保存对象但不知具体对象的引用 特殊:null == undefined --> tru ...

  6. 8款超绚丽的jQuery焦点图动画

    随着前端技术和浏览器技术的不断发展,人们开始对网页视觉效果的要求越来越高.我们经常会在页面中看到很多炫酷的图片焦点图播放控件,有些甚至是大屏的焦点图占用大片的页面空间,从而吸引用户的眼球.本文要分享的 ...

  7. Linux5.8下安装PhpMyadmin无法关联php-mcrypt问题

    一.yum安装php-mcrypt   ##发现没办法安装 原来CentOS 官方默认不在对mcrypt模块 进行支持,所以必须另想办法折腾了2个小时总算搞定,这里主要使用了Fedora的扩展库, E ...

  8. 【转载】DataGridView 使用集合作为数据源,并同步更新

    原文地址:http://hi.baidu.com/netyro/item/7340640e36738a813c42e239 今天做项目时遇到一个挠头的问题,当DataGridView的数据源为泛型集合 ...

  9. Ubuntu14.04忘记root密码的解决方法

    电脑20多天没用忘记密码了,下面是在网上找到的一个解决办法,其它的和这个也大概相同.因为其中有些缺漏,没能给我解决问题.通过分析最终问题还是解决了,现解决方案的关键点记录一下.希望能方便到其它人. 1 ...

  10. sql语句中like匹配的用法详解

    在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...