codeforces685B
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.
输入输出样例
7 4
1 1 3 3 5 3
1
2
3
5
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的更多相关文章
- 2019.01.14 codeforces685B. Kay and Snowflake(树形dp)
传送门 题意简述:给出一棵树,求每个子树的重心. 首先通过画图可以观察出一个性质,我们从叶子结点向根节点递推重心的话重心的位置是不会下降的. 然后由于一个点的重心要么是自己,要么在重儿子子树内,因此如 ...
随机推荐
- 一文让你明白Redis持久化
网上虽然已经有很多类似的介绍了,但我还是自己总结归纳了一下,自认为内容和细节都是比较齐全的. 文章篇幅有 4k 多字,货有点干,断断续续写了好几天,希望对大家有帮助.不出意外地话,今后会陆续更新 Re ...
- 我的第一个python web开发框架(2)——第一个Hello World
小白中午听完老菜讲的那些话后一直在思考,可想来想去还是一头雾水,晕晕呼呼的一知半解,到最后还是想不明白,心想:老大讲的太高深了,只能听懂一半半,看来只能先记下来,将明白的先做,不明白的等以后遇到再学. ...
- 利用element-ui封装地址输入的组件
我们前端做项目时,难免会遇到地址输入,多数情况下,我们都是提供一个省市三级联动,加上具体地址输入的Input输入框给用户,用以获取用户需要输入的真实地址.在需要对用户输入的数据进行校验的时候,我们会单 ...
- 【原创】大叔经验分享(76)confluence和jira配置
一 下载 confluence https://product-downloads.atlassian.com/software/confluence/downloads/atlassian-conf ...
- 关于SpringMVC拦截器和异常
一.文件上传 1.文件上传 SpringMVC为文件上传提供了直接的支持,这种类型是通过即插即用的MultipartResolver技术的.Spring用Jakarta Commons FileUpl ...
- SVG学习之stroke-dasharray 和 stroke-dashoffset 详解
本文适合对SVG已经有所了解,但是对stoke-dasharray和stroke-dashoffset用法有疑问的童鞋 第一:概念解释 1. stroke意思是:画短线于,在...上划线 2. str ...
- LLVM新建全局变量
在LLVM中,有原生的AST Clone,却没有一个比较好的Stmt copy功能,基于Scout在LLVM上进行的修改,我们实现了自己的Stmt Clone功能. 要进行Stmt Clone,肯定需 ...
- ASE19团队项目alpha阶段model组 scrum3 记录
本次会议于11月5日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- 不创建父窗体的情况下他其他窗体的信息显示在第一个打开的窗体!(winfrom)
公司使用vs2008做的东西,用vs2017都打不开了(编译错误) 叫我更新一下,我看了一下,08的项目 和 winform 差不多 如何就用winfrom来做了 (winform 很久没碰了,, ...
- Java中各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分
PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...