分析:就是找到以每个节点为根节点的树的重心

树的重心可以看这三篇文章:

1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63WtCFbg4RXHjERwk8piK3dgeKKvUBprOW8hJ7aN7h4ZC09QE9x6hYV3lD7bEvyOv_l1E-ucxjHJzqi

2:http://fanhq666.blog.163.com/blog/static/81943426201172472943638/

3:http://blog.csdn.net/acdreamers/article/details/16905653

注:其中强烈推荐第二篇论文

这个题用到两句话就够了

1:重心的定义是:(1)以这个点为根,那么所有的子树(不算整个树自身)的大小都不超过整个树大小的一半。

(2)找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重

心后,生成的多棵树尽可能平衡

注:          其实这两个定义基本上是等价的,但有些时候定义(1)囊括的点的范围更大一些(即符合重心定义的点更多)

但是定义(1)对于这个题可以完美契合

2:把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上。

知道这两个结论的话(基本上就是模板题了)

#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=3e5+;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
struct Edge{
int v,next;
}edge[N];
int head[N],tot,n,q;
void add(int u,int v){
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int fa[N],sum[N],mxson[N];
void dfs_pre(int u){
sum[u]=;mxson[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
dfs_pre(v);
sum[u]+=sum[v];
mxson[u]=max(mxson[u],sum[v]);
}
}
int ret[N];
bool is_ret(int u,int v){
if(mxson[v]*<=sum[u]&&*(sum[u]-sum[v])<=sum[u])return true;
return false;
}
void dfs_ret(int u){
if(sum[u]==){
ret[u]=u;
return;
}
int tmp=u;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
dfs_ret(v);
if(mxson[u]==sum[v])
tmp=ret[v];
}
while(!is_ret(u,tmp)){
tmp=fa[tmp];
}
ret[u]=tmp;
}
int main(){
scanf("%d%d",&n,&q);
memset(head,-,sizeof(head));
for(int i=;i<=n;++i){
int u,v=i;
scanf("%d",&u);
fa[i]=u;
add(u,v);
}
dfs_pre();
dfs_ret();
for(int i=;i<=q;++i){
int u;scanf("%d",&u);
printf("%d\n",ret[u]);
}
return ;
}

codeforces 685B Kay and Snowflake 树的重心的更多相关文章

  1. 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 ...

  2. D. Kay and Snowflake 树的重心

    http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...

  3. CodeForces 685B Kay and Snowflake

    树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans ...

  4. 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 ...

  5. 【CodeForces】708 C. Centroids 树的重心

    [题目]C. Centroids [题意]给定一棵树,求每个点能否通过 [ 移动一条边使之仍为树 ] 这一操作成为树的重心.n<=4*10^5. [算法]树的重心 [题解]若树存在双重心,则对于 ...

  6. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  7. Kay and Snowflake CodeForces - 686D (树的重心性质)

    After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of ...

  8. 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 ...

  9. Kay and Snowflake CodeForces - 686D

    Kay and Snowflake CodeForces - 686D 题意:给一棵有根树,有很多查询(100000级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心 ...

随机推荐

  1. 2013 ACM-ICPC长沙赛区全国邀请赛——Bottles Arrangement

    这题当时竟然没看啊…… 找规律:求和m+m+m-1+m-1+……前n项 ;}

  2. Ubuntu 14.04 + Linux 3.14.34 系统调用实现文件拷贝

    采用 64位系统, ubuntu 14.04 + 新内核linux-3.14.34 下载地址https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.1 ...

  3. 欧拉工程第57题:Square root convergents

    题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; impo ...

  4. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. 1.Spring IoC简单例子

    Spring IoC简单例子 1.IHelloMessage.java package com.tony.spring.chapter01; public interface IHelloMessag ...

  6. Android:让EditText不自动获取焦点

    解决方法: 在EditText的父级控件中加入属性: android:focusable="true" android:focusableInTouchMode="tru ...

  7. [iOS]解决模拟器无法输入中文问题

    第一步:设置schem 菜单项 -> Product-> Scheme -> Edit Scheme ->  然后在弹出的界面里 选择OPtion 项, 设置 Applicat ...

  8. 50. Pow(x, n)

    题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...

  9. Hibernate检索方式 分类: SSH框架 2015-07-10 22:10 4人阅读 评论(0) 收藏

    我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位.那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为Hi ...

  10. UIColor的用法

    UIColor,CGColor,CIColor的区别和联系 layer.shadowColor = [UIColor redColor].CGColor; 这个是今天用到的.顺便总结一下. 1.UIC ...