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. springMVC接受json类型数据

    springMVC接受json格式的数据很简单 使用@RequestBody 注解,标识从请求的body中取值 服务端示例代码 @RequestMapping(value = "/t4&qu ...

  2. Css解决表格超出部分用省略号显示

    小伙伴们有没有的遇到页面显示时,因为数据太长导致显示的表格某一列过长,从而导致页面的不美观,下面我们来看一看如何用Css样式解决表格超出部分用省略号显示的问题. 主要设置两个样式: table{ ta ...

  3. 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能

    第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...

  4. JavaScript随机验证码

    利用canvas制作一个随机验证码: 1.clearRect:context.clearRect(x,y,width,height);清空给定矩形内的指定像素 2.fillStyle:设置画笔的颜色 ...

  5. 复选框已经有checked,但是页面没有选中效果(解决)

    原代码: $("#checked").click(function(){ $(".input[name="checked"]").attr( ...

  6. 【Distributed】分布式Session一致性问题

    一.概述 1.1 什么是Session 1.2 Session实现原理 1.3 Session常见问题 Session 保证在那里? 关闭浏览器Session会失效吗 服务器集群之后,Session产 ...

  7. Jmeter服务器压力测试使用说明

    Jmeter服务器压力测试使用说明 Apache JMeter是Apache组织开发的基于Java的压力测试工具. 官方地址:http://jmeter.apache.org/download_jme ...

  8. Computer Vision_2_Active Shape Models:Active Shape Models-Their Training and Application——1995

    此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...

  9. 前端基础(二):CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  10. (十四)Linux kernel mmc 框架说明,包括mmc_test使用方法

    1.Linux 总线模型         Linux下的任何驱动在内核中最终都抽象为bus, driver以及device三者间的相互作用. 总线是处理器和一个或多个设备之间的通道,在设备模型中,所有 ...