树的重心,树形$dp$。

记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$。

首先要知道一个结论:以$x$为$root$的子树的重心$ans[x]$,一定在$ans[son[x]]$到$x$的路径上,即以$x$的重儿子为根的子树的重心到$x$的路径上。

因此,只要从节点$ans[son[x]]$依次往$father$枚举就可以了.

如果枚举到节点$g$,发现$g$节点满足$sz\left[ {son\left[ g \right]} \right] ≤ \frac{{sz\left[ x \right]}}{2}\& \& sz\left[ x \right] - sz\left[ g \right] ≤ \frac{{sz\left[ x \right]}}{2}$,那么$ans[x]=g$。

时间复杂度$O(n\log n)$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int n,q,p[maxn],h[maxn],sz[maxn],son[maxn],ans[maxn],cnt;
struct Edge {int u,v,nx;}e[maxn]; void add(int u,int v)
{
e[cnt].u=u; e[cnt].v=v;
e[cnt].nx=h[u]; h[u]=cnt++;
} void dfs(int x)
{
sz[x]=; son[x]=;
for(int i=h[x];i!=-;i=e[i].nx)
{
dfs(e[i].v);
sz[x]=sz[x]+sz[e[i].v];
if(sz[e[i].v]>sz[son[x]]) son[x]=e[i].v;
} if(sz[x]==) { ans[x]=x; return; } int g=ans[son[x]];
while()
{
bool fail=;
if(sz[son[g]]>sz[x]/) fail=;
if(sz[x]-sz[g]>sz[x]/) fail=;
if(fail==) { ans[x]=g; break; }
g=p[g];
}
} int main()
{
//File();
memset(h,-,sizeof h); cnt=;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]);
add(p[i],i);
}
dfs();
for(int i=;i<=q;i++)
{
int x; scanf("%d",&x);
printf("%d\n",ans[x]);
}
return ;
}

CodeForces 685B Kay and Snowflake的更多相关文章

  1. codeforces 685B Kay and Snowflake 树的重心

    分析:就是找到以每个节点为根节点的树的重心 树的重心可以看这三篇文章: 1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63 ...

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

  3. Kay and Snowflake CodeForces - 686D

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

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

  6. Codeforces Round #359 (Div. 2) D - Kay and Snowflake

    D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...

  7. Codeforces 686 D - Kay and Snowflake

    D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心 ...

  8. codeforces 686D D. Kay and Snowflake(dfs)

    题目链接: D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  9. CF685B Kay and Snowflake 贪心

    CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更 ...

随机推荐

  1. tortoiseSVN 设置ignore

      *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store *.dll ...

  2. jquery实现城市选择器效果(二级联动)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [修]开启MySQL远程访问权限 允许远程连接

    原文地址:http://www.cnblogs.com/XL-Liang/archive/2012/05/03/2481310.html 这个地址也许更有帮助:http://www.cppblog.c ...

  4. VIM 多行输入 数字递增 新方法 循环记录法

    采用的是mario register这个方法,然后,把一段 auto-increament 操作记录下来,然后playback 循环往复多次.就达到了,每行都递增的目的. 我写的文字如下: vim 输 ...

  5. 【OpenMesh】创建一个正方体

    原文出处: http://openmesh.org/Documentation/OpenMesh-Doc-Latest/tutorial.html 这个例程演示了: 如何声明MyMesh 如何添加顶点 ...

  6. POJ 1915 经典马步 双向bfs

    拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...

  7. Jenkins+PMD构建自动化静态代码检测

    前言:软件缺陷是不可避免的,要尽量减少错误并提高软件质量,主要有两在类技术,即缺陷预防和缺陷检测 缺陷预防包括编写更好的设计规范.实施代码审核制度.运行代码静态分析工具.运行单元测试等 PMD是一种开 ...

  8. Web基础知识和技术

    WEB是一个外延广泛的概念,不单单指网站,乌徒帮专注拥有WEB界面的网站开发,帮助初学者或已经进入开发的朋友们提供参考讨论平台,然而并不一定能将所有的WEB知识讲全讲透,只是能满足初涉者的建站需求,能 ...

  9. Oracle笔记(六) 多表查询

    最近看了李兴华的oracle视频,这是网上别人做的笔记非常细致,分享给大家,第六篇 原创地址:http://www.cnblogs.com/mchina/archive/2012/09/07/2651 ...

  10. android电源“有毒”移动电源Android版的设计及其实现

    工作之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下android电源 报道http://www.cnbeta.com/articles/239726.htm称:安全研 ...