B. Kay and Snowflake

题目连接:

http://www.codeforces.com/contest/685/problem/B

Description

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.

Sample Input

7 4

1 1 3 3 5 3

1

2

3

5

Sample Output

3

2

3

6

Hint

给你一棵树,Q次询问,每次询问这个点的中心是啥

题解:

离线做

重心是啥?就是砍掉这个点之后,剩下的树的大小都小于原树大小的1/2

那么我把这个点的所有子树都看一下,把最接近1/2的那个子树剁掉就好了,剩下的肯定满足……

这个启发式合并一下,nlognlogn很容易实现

但是其实,直接dfs一波就好了,递归处理,那个儿子肯定是在他的重儿子那儿,然后不停往上爬就好饿了

复杂度均摊一下,其实没有多大。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
vector<int>E[maxn];
int n,q,sz[maxn],ans[maxn],f[maxn];
void dfs(int x){
ans[x]=x;
sz[x]=1;
for(int i=0;i<E[x].size();i++){
int v=E[x][i];
dfs(v);
sz[x]+=sz[v];
}
for(int i=0;i<E[x].size();i++){
if(sz[E[x][i]]*2>sz[x])
ans[x]=ans[E[x][i]];
}
while((sz[x]-sz[ans[x]])*2>sz[x])
ans[x]=f[ans[x]];
}
int main(){
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++){
scanf("%d",&f[i]);
E[f[i]].push_back(i);
}
dfs(1);
for(int i=1;i<=q;i++){
int x;scanf("%d",&x);
printf("%d\n",ans[x]);
}
}

Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs的更多相关文章

  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. 2) D. Kay and Snowflake 树的重心

    题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

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

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

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

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

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

  7. Codeforces Round #359 (Div. 1)

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

  8. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  9. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

随机推荐

  1. MVVM模式的 数据绑定

    数据绑定要达到的效果 数据绑定要达到什么效果呢,就是在界面中绑定了数据源之后,数据在界面上的修改能反映到绑定源,同时绑定源的修改也能反映到界面上.从界面反映到绑定的数据源是很容易理解的,因为在绑定过程 ...

  2. VM虚拟机和主机互传文件,使用xshell连接Ubuntu

    安装虚拟机后,有时需要在window和Ubuntu互传文件,安装VMwave tooles比较麻烦,干脆直接用xshell连接Ubuntu即可 1,已经安装Ubuntu和xshell 2,在Ubunt ...

  3. MySQL -- SQL 语句

    一. 数据库(Database)操作 创建数据库 create database 数据库名 create database 数据库名 character set 字符集 查看数据库 查看数据库服务器中 ...

  4. FM的推导原理--推荐系统

    FM:解决稀疏数据下的特征组合问题  Factorization Machine(因子分解机) 美团技术团队的文章,觉得写得很好啊:https://tech.meituan.com/deep-unde ...

  5. Oracle JDeveloper 10g 卡顿、花屏的解决方法

    1.JDeveloper 10g花屏的解决办法: 在Win7或WinXP环境下,JDeveloper10g可能产生花屏现象,给开发者造成困扰,解决方法如下: 打开{JDEV_HOME}\jdev\bi ...

  6. Little C Loves 3 I

    CF#511 div2 A 现场掉分赛(翻车),就是这道题被叉了...qwq 其实就是一道水题: 因为CF有spj,所以直接构建特殊情况就行了. 当 n 是3的倍数的时候,显然 1,1,(n-2) 显 ...

  7. SCTP客户端与服务器

    /** * @brief - Send a message, using advanced SCTP features * The sctp_sendmsg() function allows you ...

  8. JS Ajax异步请求发送列表数据后面多了[]

    还在苦逼的写代码,这里就不详细了,直接抛出问题: 如图所示: 前端ajax请求向后端发送数据的时候,给key添加了[]出现很多找不到原因, 后面在说 解决方法: 暂时先这样记录一下,下次方便查找,好了 ...

  9. pytest mark中的skip,skipif, xfail

    这些测试的过滤,或是对返回值的二重判断, 可以让测试过程更精准,测试结果更可控, 并可以更高层的应用测试脚本来保持批量化执行. import pytest import tasks from task ...

  10. CCF CSP 201512-2 消除类游戏

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201512-2 消除类游戏 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行 ...