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

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

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. hdu 4315 Climbing the Hill 博弈论

    题意:有n个人爬山,山顶坐标为0,其他人按升序给出,不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面的人,且不能和前面一个人在相同的位置.现在有一个 ...

  2. 【转载】关于ActionContext.getContext().getParameters()获值问题

    ActionContext.getContext().getParameters():一个学员问题的解答 2012-11-12 15:12:05|  分类: 默认分类 |  标签:struts2   ...

  3. sql语句面试总结

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name   kecheng   fenshu 张三     语文       81张三     数学       75李四     语文   ...

  4. VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)

    原文地址: VS2010/MFC编程入门之一(VS2010与MSDN安装过程图解)-软件开发-鸡啄米 http://www.jizhuomi.com/software/139.html   上一讲中鸡 ...

  5. UNIX相关知识

    UNIX UNIX的设计目标是小而美:希望能在任何小系统上执行,而核心只提供必不可少的一些功能,其他的则根据需要加上去.这已经成为操作系统的一种设计哲学. The Open Group持有UNIX商标 ...

  6. 248. Strobogrammatic Number III

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

  7. linux 进入包含空格文件名的文件夹

    可以使用加英文的引号cd  /usr/chen/java/"MyeClips\ 如果"\"在行末,是说明本行还未结束,下面一行和本行是一起的意思. 一般短格式的选项(一个 ...

  8. 使用Maven创建一个Spring MVC Web 项目

    使用Maven创建java web 项目(Spring MVC)用到如下工具: 1.Maven 3.2 2.IntelliJ IDEA 13 3.JDK 1.7 4.Spring 4.1.1 rele ...

  9. selenium--大家庭介绍

    安装好配置环境后,开始我的selenium之旅.简单的了解一下色,selenium大家庭的组成. Selenium是ThoughtWorks专门为 Web 应用而开发的功能测试工具.Selenium使 ...

  10. jdbc知识问答 分类: 面试 2015-07-10 22:05 5人阅读 评论(0) 收藏

    1 JDBC连接数据库6步 Load the JDBC Driver Establish the Database Connection Create a Statement Object Execu ...