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)
传送门 题意简述:给出一棵树,求每个子树的重心. 首先通过画图可以观察出一个性质,我们从叶子结点向根节点递推重心的话重心的位置是不会下降的. 然后由于一个点的重心要么是自己,要么在重儿子子树内,因此如 ...
随机推荐
- pt-online-schema-change使用
MySQL ddl 的问题现状 在 运维mysql数据库时,我们总会对数据表进行ddl 变更,修改添加字段或者索引,对于mysql 而已,ddl 显然是一个令所有MySQL dba 诟病的一个功能,因 ...
- 第八章 ZYNQ-MIZ701 软硬调试高级技巧
软件和硬件的完美结合才是SOC的优势和长处,那么开发ZYNQ就需要掌握软件和硬件开发的调试技巧,这样才能同时分析软件或者硬件的运行情况,找到问题,最终解决.那么本章将通过一个简单的例子带大家使用v ...
- MyBatis MyBatis Generator入门
一.MGB功能简介 MyBatis Generator是一个代码生成工具. MBG是如何运行的呢?它会检查所连接到的数据库的一个或者多个table,然后生成可用来访问这些table的构建(Java代码 ...
- js实现div转图片并保存
最近工作中遇到的需求,将div转成图片并保存. 1.准备需要用到的js插件jquery-1.8.2.js,html2canvas.min.js(将div转换为canvas),bluebird.js(用 ...
- Dual 表
我们先从名称来说,dual不是缩写词,本身就是完整的单词.dual名词意思是对数,做形容词时是指二重的,二元的. Oracle中的dual表是一个单行单列的虚拟表. Dual表是oracle与数据字典 ...
- VBA精彩代码分享-3
在开发VBA程序中,我们可能会需要用代码处理VBA工程,包括启用VBA工程访问,启用所有宏,动态插入代码,动态删除代码,动态添加引用和自动创建模块等等,本次的分享内容便以这些为主. 启用VBA工程访问 ...
- python3 datetime 时间格式相减 计算间隔
info_rent = MysqlUtils.select_yezhu_rent() info_sale = MysqlUtils.select_yezhu_sale() now_time = dat ...
- Arcgis js之web墨卡托(3857)转经纬度坐标(4326)
Arcgis js之web墨卡托(3857)转经纬度坐标(4326) 手动转化方法: function mercatorTolonlat(mercator){ var lonlat={x:0,y:0} ...
- 系统模块 sys 函数的调用
系统模块 sys 运行时系统相关的信息 sys模块的数据 数据 描述 sys.path 模块搜索路径 path[0] 是当前脚本程序的路径名,否则为 '' sys.modules 已加载模块的字典 s ...
- shiro系列三、定义Realm
自定义realm: /** * 认证和授权 */ @Component public class UserRealm extends AuthorizingRealm { @Autowired pri ...