codeforces 685B Kay and Snowflake 树的重心
分析:就是找到以每个节点为根节点的树的重心
树的重心可以看这三篇文章:
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 树的重心的更多相关文章
- 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 ...
- D. Kay and Snowflake 树的重心
http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...
- CodeForces 685B Kay and Snowflake
树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans ...
- 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 ...
- 【CodeForces】708 C. Centroids 树的重心
[题目]C. Centroids [题意]给定一棵树,求每个点能否通过 [ 移动一条边使之仍为树 ] 这一操作成为树的重心.n<=4*10^5. [算法]树的重心 [题解]若树存在双重心,则对于 ...
- codeforces 701E E. Connecting Universities(树的重心)
题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 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 ...
- 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 ...
- Kay and Snowflake CodeForces - 686D
Kay and Snowflake CodeForces - 686D 题意:给一棵有根树,有很多查询(100000级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心 ...
随机推荐
- CF 86D Powerful array
离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号 ...
- 一个很好的php分词类库
PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档 原文连接地址:http://www.phpbone.co ...
- VIM Taglist + ctags
Windows下 进入http://ctags.sourceforge.net/ 下载ctags 把ctags58.zip解压,随便放个地方,我放到了Home\Vim\vim72下,在ctags58文 ...
- iOS Objective-C对象模型及应用
前言 原创文章,转载请注明出自唐巧的技术博客. 本文主要介绍Objective-C对象模型的实现细节,以及Objective-C语言对象模型中对isa swizzling和method swizzli ...
- Android ListView高度自适应和ScrollView冲突解决
在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查,S ...
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- 人脸识别必读的N篇文章
一,人脸检测/跟踪 人脸检测/跟踪的目的是在图像/视频中找到各个人脸所在的位置和大小:对于跟踪而言,还需要确定帧间不同人脸间的对应关系. 1, Robust Real-time Object Dete ...
- Zookeeper核心机制
(如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) Zookeeper是Hado ...
- AFNetworking使用
1.访问网络获取Json //Get方法 NSString *str = @"http://api.xxx.cc/product/found.jhtml"; NSDictionar ...
- java中的toString方法
对于我这种用惯了C++的人来说,突然见到有人写java程序的时候竟然将整数和String类型的变量使用+连接到一起,感到非常奇怪,追究了下原因. 原来所有的java对象都有toString()方法,而 ...