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

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

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. 用 EasyBCD 在 Win7/8 中硬盘安装 Ubuntu

    写在前面: 1. 我装的是ubuntu 13.10 64位,不一样的地方是,从casper文件夹复制出来的文件不是vmlinuz,而是vmlinuz.efi,相应的,menu.lst里也要将vmlin ...

  2. dbgrid显示access备注信息

    procedure TfrmAllFind.DBGrid6DrawColumnCell(Sender: TObject; const Rect: TRect;  DataCol: Integer; C ...

  3. 【Linux高频命令专题(17)】head

    概述 head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 命令格式 hea ...

  4. 1.Spring IoC简单例子

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

  5. CAD导入ArcScene中线被打断 求解决方案

    cad中是这样 但在arcscene里中是这样

  6. Linux下jvm、tomcat、mysql、log4j优化配置

    小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...

  7. Android开发之ActivityManager获取系统信息

    1.判断指定的service是否在运行 public static boolean isServiceRunning(Context ctx, String serviceName) { Activi ...

  8. C++ string类的学习

    string类对于处理字符串的一些应用非常的方便,我个人感觉,string和字符数组const char *很像,而且又比字符数组用起来方便的多. 注意其删除,取子串,插入等函数里面都有一个重载版本是 ...

  9. SQL全文搜索

    ( select dd.*,t.RANK from crm_CustomerAnalyzeDetails dd ) as t on dd.ID = t.[key] ) union all ( sele ...

  10. UWP:本地应用数据

    获取应用的设置和文件容器 使用 ApplicationData.LocalSettings 属性可以获取 ApplicationDataContainer 对象中的设置. 注意:每个设置的名称最长可为 ...