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

时间:2022-11-08 07:35:03

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 &num;359 &lpar;Div&period; 2&rpar; D&period; 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 &num;359 &lpar;Div&period; 2&rpar; D&period; Kay and Snowflake 树的重心

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

  3. Codeforces Round &num;359 &lpar;Div&period; 2&rpar; D - Kay and Snowflake

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

  4. Codeforces Round &num;359 &lpar;Div&period; 2&rpar; C&period; Robbers&&num;39&semi; watch &lpar;暴力DFS&rpar;

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

  5. Codeforces Round &num;359 &lpar;Div&period; 2&rpar; A&period; Free Ice Cream 水题

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

  6. Codeforces Round &num;359 &lpar;Div&period; 2&rpar;C - Robbers&&num;39&semi; watch

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

  7. Codeforces Round &num;359 &lpar;Div&period; 1&rpar;

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

  8. Codeforces Round &num;359 &lpar;Div&period; 1&rpar; A&period; Robbers&&num;39&semi; watch 暴力

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

  9. Codeforces Round &num;359 &lpar;Div&period; 2&rpar; B&period; Little Robber Girl&&num;39&semi;s Zoo 水题

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

随机推荐

  1. GitHub注册账号

    username : hcloudypassword : hujunyun3174email : 370284221@qq.com

  2. apache和nginx

    虽然nginx使用较少 还是写写文章,记录下 nginx是异步非阻塞,apache是阻塞的. apache动态页面比nginx好. 由于nginx的高并发性(使用epoll模型),所以出来静态页面性能 ...

  3. hadoop rpc基础

    第一部分: hadoop rpc基础 RPC,远程程序调用,分布式计算中C/S模型的一个应用实例. 同其他RPC框架一样,Hadoop分为四个部分: 序列化层:支持多种框架实现序列化与反序列化 函数调 ...

  4. C&num;学习笔记-----C&num;枚举中的位运算权限分配

    一.基础知识 什么是位运算? 用二进制来计算,1&2:这就是位运算,其实它是将0001与0010做位预算   得到的结果是 0011,也就是3  2.位预算有多少种?(我们就将几种我们权限中会 ...

  5. Struts2 报 Result 错误

    写的时候犯了个低级错误  struts.xml中 配置result 的时候 没有配置type

  6. Mybatis学习记录(七)----Mybatis延迟加载

    1.什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: ...

  7. Exception与相关

    怎么写一个exception类, 直接抛出去,主要是写一个构造函数里面的Msg消息,这个可以提前写出来. try...catch..finally 一般都是一起的,try 中有异常执行语句, catc ...

  8. 【面试&amp&semi;笔试】ASP&period;NET的相关问题

    1.      介绍ASP.NET 答:ASP.NET不是一种语言,而是创建动态web页的一种强大的服务器端技术,它是Microsoft.NETFramework中一套用于生成Web应用程序和Web服 ...

  9. Python 字符串相关操作

    # 1 * 重复输出字符串 print('hello'*2) # 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表 print('helloworld'[2: ...

  10. vue 如何在循环中绑定v-model

    vue 如何在循环中绑定v-model 我现在有这么一个需求,页面上有多项输入框,但是具体有多少项,我也不知道,它是通过"新增一项"按钮点击事件,点击一下,就新增一项:如下图这个样 ...