D - Kay and Snowflake

题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点。

定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点的个数的两倍不会超过n。

性质 1 :树中所有点到某个点的距离和中,到重心的距离和是最小的,如果有两个距离和,他们的距离和一样。

性质 2 :把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上。

性质 3 :一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置。

 #include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define read(x) scanf("%d",&x)
#define sread(x) scanf("%s",x)
#define dread(x) scanf("%lf",&x)
#define lread(x) scanf("%lld",&x)
using namespace std; const int N=1e6+;
const int M=1e7+;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+; int n,q,sum[N],fa[N],ans[N];
vector<int> edge[N]; int dfs(int u)
{
sum[u]=; ans[u]=u;
for(int v:edge[u])
sum[u]+=dfs(v); for(int v:edge[u])
if(sum[v]*>sum[u])
ans[u]=ans[v]; while((sum[u]-sum[ans[u]])*>sum[u])
ans[u]=fa[ans[u]]; return sum[u];
} int main()
{
read(n); read(q);
for(int i=;i<=n;i++)
{
int pre; read(pre);
edge[pre].push_back(i);
fa[i]=pre;
}
dfs();
while(q--)
{
int u; read(u);
printf("%d\n",ans[u]);
}
return ;
}
/* */

Codeforces Round #359 (Div. 2) D - Kay and Snowflake的更多相关文章

  1. Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs

    B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...

  2. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP

    D. Kay and Snowflake     After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...

  3. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心

    题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  5. Codeforces Round #359 (Div. 2)C - Robbers' watch

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)

    题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...

  7. Codeforces Round #359 (Div. 1)

    A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...

  8. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  9. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

随机推荐

  1. vue自学入门-1(Windows下搭建vue环境)

    本人是一个喜欢动手的程序员,先跑起来个HelloWorld,增加感性认识,这三篇入门文章,花了不到一个小时,从网上找资料,程序跑通后,整理出来的,有的新人可能去哪找资料,运行代码都不知道,分享出来,大 ...

  2. C#复杂类型序列化

    [Serializable] public class CardItemInfo { private int lineWidth;//线宽 private CardItemInfo childCard ...

  3. linux 更改文件夹所有者

    更改“tp5”文件的所有者为”www” chown -R tp5/ www 修改目录及其子目录的用户组为“www” chgrp -R www tp5 同时更改文件或目录的所有者和用户组 chown - ...

  4. VS2012 安装番茄插件

    1.visual assist x 1929_大番茄.rar 下载地址: http://pan.baidu.com/s/1qXOUuJE 网盘提取密码: 3mka 文件解压密码: eg5p 2.安装完 ...

  5. mysql 架构~MGR监控手段

    一 简介 今天咱们来聊聊MGR的监控 二 监控 方面: 1 节点mysql进程监控                       2 节点mysql复制进程的监控                    ...

  6. JavaScript练习 - 模态对话框

    模态对话框练习 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. 深入解析Java AtomicInteger 原子类型

    深入解析Java AtomicInteger原子类型 在进行并发编程的时候我们需要确保程序在被多个线程并发访问时可以得到正确的结果,也就是实现线程安全.线程安全的定义如下: 当多个线程访问某个类时,不 ...

  8. CodeForces Contest #1137: Round #545 (Div. 1)

    比赛传送门:CF #1137. 比赛记录:点我. 每次都自闭的 div1 啊,什么时候才能上 IM 呢. [A]Skyscrapers 题意简述: 有一个 \(n\times m\) 的矩阵 \(a_ ...

  9. jquery开发自定义的插件总结

    1.第一种方式,有元素的插件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  10. Pytorch 资料汇总(持续更新)

    1. Pytorch 论坛/网站 PyTorch 中文网 python优先的深度学习框架 Pytorch中文文档 Pythrch-CN文档地址 PyTorch 基礎篇 2. Pytorch 书籍 深度 ...