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

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

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. POJ1426Find The Multiple

    http://poj.org/problem?id=1426 题意 : 输入一个数n,找n的倍数m,这个m所满足的条件是,每一位数只能由0或1组成,在题目的旁边用红色的注明了Special Judge ...

  2. 让DJANGO里的get_success_url定义的reverse_lazy带参数跳转

    按一般的CBVS实现,这个是编辑UPDATEVIEW完成之后,跳到LISTVIEW的. 但如果带跳到DETAILVIEW,则reverse_lazy需要带上参数进行跳转. 实现预定义的PK键跳转代码如 ...

  3. hdu 1370 Biorhythms

    中国剩余定理……. 链接http://acm.hdu.edu.cn/showproblem.php?pid=1370 /**************************************** ...

  4. lintcode:Unique Characters 判断字符串是否没有重复字符

    题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 ...

  5. lintcode:Palindrome Partitioning 分割回文串

    题目: 分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  6. 【mongoDB基础篇②】PHP-mongo扩展的编译以及使用

    安装PHP-mongo扩展 安装php-mongo扩展和安装其他php扩展的步骤一样: #1.首先上http://pecl.php.net上面搜索mongo,得到下载地址 wget http://pe ...

  7. 浅谈SQL语句优化经验

    (1) 选择最有效率的表名顺序(只在基于规则的seo/' target='_blank'>优化器中有效):ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后 ...

  8. Debug过程中的mock (及display窗口的使用)

    转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...

  9. [Unity菜鸟] Time

    1. Time.deltaTime 增量时间 以秒计算,完成最后一帧的时间(秒)(只读) 帧数所用的时间不是你能控制的.每一帧都不一样,游戏一般都是每秒60帧,也就是updata方法调用60次(假如你 ...

  10. 关于NGUI制作图集在低内存设备上的注意事项

    正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...