CF685B 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 nn nodes. The root of tree has index 11 . Kay is very interested in the structure of this tree.

After doing some research he formed qq queries he is interested in. The ii -th query asks to find a centroid of the subtree of the node v_{i}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 vv is formed by nodes uu , such that node vv is present on the path from uu 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 nn and qq ( 2<=n<=3000002<=n<=300000 , 1<=q<=3000001<=q<=300000 ) — the size of the initial tree and the number of queries respectively.

The second line contains n-1n−1 integer p_{2},p_{3},...,p_{n}p2​,p3​,...,pn​ ( 1<=p_{i}<=n1<=pi​<=n ) — the indices of the parents of the nodes from 22 to nn . Node 11 is a root of the tree. It's guaranteed that p_{i}pi​ define a correct tree.

Each of the following qq lines contain a single integer v_{i}vi​ ( 1<=v_{i}<=n1<=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.

输入输出样例

输入 #1复制

7 4
1 1 3 3 5 3
1
2
3
5
输出 #1复制

3
2
3
6

说明/提示

The first query asks for a centroid of the whole tree — this is node 33 . If we delete node 33 the tree will split in four components, two of size 11 and two of size 22 .

The subtree of the second node consists of this node only, so the answer is 22 .

Node 33 is centroid of its own subtree.

The centroids of the subtree of the node 55 are nodes 55 and 66 — both answers are considered correct.

sol:dfs下去,如果有儿子大于sz的一半,答案肯定在那个儿子里,暴力往上爬即可,应该是nlogn的

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,Q,fa[N],sz[N];
int tot=,Next[M],to[M],head[N];
int ans[N];
inline void Link(int x,int y)
{
Next[++tot]=head[x]; to[tot]=y; head[x]=tot;
}
inline void presz(int x)
{
// cout<<"x="<<x<<endl;
int e;
sz[x]=;
for(e=head[x];e;e=Next[e])
{
presz(to[e]); sz[x]+=sz[to[e]];
}
}
inline void dfs(int x)
{
// cout<<"x="<<x<<endl;
int e; ans[x]=x;
for(e=head[x];e;e=Next[e])
{
dfs(to[e]);
if((sz[to[e]]<<)>sz[x]) ans[x]=ans[to[e]];
}
if(ans[x]!=x) while((((sz[x]-sz[ans[x]])<<)>sz[x])&&(ans[x]!=x)) ans[x]=fa[ans[x]];
}
int main()
{
int i,x;
R(n); R(Q);
for(i=;i<=n;i++)
{
R(fa[i]); Link(fa[i],i);
}
presz();
dfs();
while(Q--)
{
R(x); Wl(ans[x]);
}
return ;
}

codeforces685B的更多相关文章

  1. 2019.01.14 codeforces685B. Kay and Snowflake(树形dp)

    传送门 题意简述:给出一棵树,求每个子树的重心. 首先通过画图可以观察出一个性质,我们从叶子结点向根节点递推重心的话重心的位置是不会下降的. 然后由于一个点的重心要么是自己,要么在重儿子子树内,因此如 ...

随机推荐

  1. vue中的键盘事件

    @keydown(键盘按下时触发),@keypress(键盘按住时触发),@keyup(键盘弹起) 获取按键的键码 e.keyCode @keyup.13     按回车键 @keyup.enter ...

  2. 如何爬取icourse163 中国慕课上课程信息(上),

    中国大学MOOC网上有着特别完善的课程信息,我觉得这是一份可以让我们充分利用的资源 那么,接下来的问题就是我们该如何爬取这里的资源 选择其中的计算机课程进行尝试 import requests fro ...

  3. 如何成为优秀的技术Leader

    技术主管,又叫技术经理,英文一般是 Tech Leader ,简称 TL.随着工作经验的不断积累,能力的不断提升,每个人都有机会成为 Team Leader. 然而在机会到来前,我们必须提前做好准备, ...

  4. 使用ABAP批量下载Markdown源文件里的图片到本地

    执行我github里的这个report: 选中一段markdown文档,ctrl C: 然后直接执行report: 执行完毕: 所有文件都下载到本地文件夹: 这个report使用到的工具类:zcl_c ...

  5. 20.SSM整合-全注解开发

    全注解开发 1.将SpringMVC改为注解 修改spring-mvc.xml 2.将Spring改为注解 将Service改为注解,完成Dao的注入 将事务以注解方式织入到Service 1.修改s ...

  6. 2.IOC 配置与应用(xml的方式)

    1.注入方式 a)setter(主要) b)构造方法(可以忘记) c)接口注入(可以忘记) 2.id  vs  name bean 标签中可以使用  name 属性 来完成 id 属性的功能,不过习惯 ...

  7. 利用django 实现个人博客 全记录(二)

    上一篇文章已经把基础环境搭建好了 一  创建app D:\学习\python3.7.3\python manage.py startapp blog 修改 博客的 models.py ) ) def ...

  8. dict 字典 函数值应用

    函数 说明 D代表字典对象   D.clear() 清空字典 D.pop(key) 移除键,同时返回此键所对应的值 D.copy() 返回字典D的副本,只复制一层(浅拷贝) D.update(D2) ...

  9. 第五章、Django之模型层----多表查询

    目录 第五章.Django之模型层----多表查询 一.一对多字段增删改查 1.增 2.查 3.改 4. 删除 二.多对多的增删改查 1. 增 2. 改 3. 删 三.ORM跨表查询 四.正反向的概念 ...

  10. 《设计模式之美》 <03>面向对象、设计原则、设计模式、编程规范、重构,这五者有何关系?

    面向对象 现在,主流的编程范式或者是编程风格有三种,它们分别是面向过程.面向对象和函数式编程.面向对象这种编程风格又是这其中最主流的.现在比较流行的编程语言大部分都是面向对象编程语言.大部分项目也都是 ...