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. 使用Eclipse Memory Analyzer 进行JAVA内存泄露分析

    一,安装 Eclipse Memory Analyzer 在Memory Analyzer的官网找到 update site的地址:

  2. 重定向printf

    #include "stdio.h" #ifdef __GNUC__#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else# ...

  3. offset[Parent/Width/Height/Top/Left] 、 client[Width/Height/Top/Left] 、 Element.getBoundingClientRect()

    开篇提示:以下内容都经个人测试,参考API文档总结,但还是不能保证完全正确,若有错误,还请留言指出___________________________________________________ ...

  4. CentOS6.8安装配置sonarqube6.4

    下载最新版本的sonar(现在改名叫sonarqube) https://www.sonarqube.org/downloads/ 我下载的版本是Sonarqube6.4        1 使用前需要 ...

  5. Node、PHP、Java 和 Go 服务端 I/O 性能PK

    http://blog.csdn.net/listen2you/article/details/72935679

  6. 解决bootstrap-table多次请求只触发一次的问题

    在初始化table之前,要将table销毁,否则会保留上次加载的内容 1 $("#table").bootstrapTable('destroy'); 2 $("#tab ...

  7. 用Quartz 2D画小黄人

    第一步: 先创建一个OneView类,并在storyboard里边拖拽一个UIview,将这个UIview的类改成OneView.如图所示: 第二步: 在新创建的Oneview里,补齐下列代码: // ...

  8. 带事件的Bootstrap模态框的使用2

    模态框中显示一些基本的数据以及触发一些基本的JS函数 <%@ page language="java" contentType="text/html; charse ...

  9. pandas 定位 loc,iloc,ix

    In [114]: df Out[114]: A B C D 2018-06-30 0.318501 0.613145 0.485612 0.918663 2018-07-31 0.614796 0. ...

  10. IDEA常用快捷键和常用插件集成,持续更新......

    用习惯了eclipse,不容易转过来,记一下! 快捷键 psvm: main 方法快捷键 sout :syso快捷键 CTRL+O: 重写父类方法 Ctrl+Alt+V :自动补全返回值 Ctrl+S ...