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. Go语言操作NoSql

    NSQ平台 NSQ是目前比较流行的一个分布式的消息队列,本文主要介绍了NSQ及Go语言如何操作NSQ. NSQ NSQ介绍 NSQ是Go语言编写的一个开源的实时分布式内存消息队列,其性能十分优异. N ...

  2. JS中json数组多字段排序方法(解决兼容性问题)(转)

    前端对一个json数组进行排序,用户需要动态的根据自己的选择来对json数据进行排序. 由于后台表设计问题所以不能用sql进行排序,这里用到了js的sort方法. 如果对单字段排序,那么很简单,一个s ...

  3. C# Math.Round()的银行家算法

    可能很多人都跟我一样,都只知道Math.Round()是C#中用来做四舍五入,保留指定小数位的 但实际上它并不是真正的四舍五入,而是银行家算法的四舍六入五取偶 事实上这也是IEEE的规范,因此所有符合 ...

  4. $.proxy的使用

    $.proxy()是jquery的一个方法.是一个改变this指向的方法. 在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: //正常的this使用 ...

  5. datagrid如何获取选中行的索引

    //datagrid获取选中行 var row =baseSelectgrid.datagrid('getSelected'); // 获取被选中行的索引 index var index=baseSe ...

  6. mysql 按月统计但是有几个月没有数据,需要变成0

    创建现在倒过去的12个月的视图 CREATE VIEW `past_12_month_view` AS SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS `month ...

  7. 2.XML语言

    XML语言 常见应用: XML技术除用于 /*保存有关系的数据*/之外,它还经常作软件配置文件,以描述程序模块之间的关系. 在一个系统软件中,为提高系统的灵活性,它所启动的模块通常由其配置文件决定 例 ...

  8. ASE19团队项目alpha阶段model组 scrum10 记录

    本次会议于11月14日,19时整在微软北京西二号楼sky garden召开,持续5分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...

  9. win10 增加一个新磁盘

    1.右键我的电脑,选择管理 可以看到C盘的空间相比较大,拿出来250G的空间做成E盘 2.选择OS(C:),右键,压缩卷,请稍后,点击压缩 3.此刻会看到,有一块黑色区域就是新建的未分配空间,这时我们 ...

  10. jumpserver跳板机docker安装小小趟坑

    最近日常运维的时候发现每次登陆服务器都要打开终端目录连接对应的服务器,闲暇的时候还好,运维任务很重的时候才发现这样的玩法很傻,浪费时间且一点儿都跟不上潮流,然后打开githup开始搞起来.docker ...