D - Kay and Snowflake

思路:

树的重心

利用重心的一个推论,树的重心必定在子树重心的连线上。

然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心一定在这颗子树上。

利用以上两条,可知:

如果没有一颗子树的大小超过整棵树的大小的1/2,那么就可以以根节点为树的重心,

不然就从 超过1/2大小的子树 的重心 到 根节点 之间找整棵树的重心。

因为不会找重复的点,所以退化的复杂度为O(n)

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define LL long long
#define pb push_back
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a)) const int N = 3e5 + ;
vector<int>g[N];
int tot[N], ans[N], fa[N];
void dfs(int u) {
tot[u] = ;
for (int i = ; i < g[u].size(); i++) {
dfs(g[u][i]);
tot[u] += tot[g[u][i]];
}
ans[u] = u;
for (int i = ; i < g[u].size(); i++) {
if(tot[g[u][i]] * > tot[u]) ans[u] = ans[g[u][i]];
}
while((tot[u] - tot[ans[u]]) * > tot[u]) ans[u] = fa[ans[u]];
}
int main() {
int n, u, q;
scanf("%d%d", &n, &q);
for (int i = ; i <= n; i++) {
scanf("%d", &fa[i]);
g[fa[i]].pb(i);
}
dfs();
while (q--) {
scanf("%d", &u);
printf("%d\n",ans[u]);
}
return ;
}

Codeforces 686 D - Kay and Snowflake的更多相关文章

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

  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. CF685B Kay and Snowflake 贪心

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

  8. B. Kay and Snowflake 解析(思維、DFS、DP、重心)

    Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...

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

随机推荐

  1. gnats配置文件

    尽管NATS可以无配置的运行,但也可以使用配置文件配置NATS服务器 1. 配置项包括 客户端监听器端口 Client listening port HTTP监听器端口 HTTP monitoring ...

  2. oracle 11g AUTO_SAMPLE_SIZE动态采用工作机制

    Note that if you're interested in learning about Oracle Database 12c, there's an updated version of ...

  3. vfio

    VFIO简介 Documentation/vfio.txt 还是不理解IOMMU.北桥.PCI的MMIO和ioremap file:///C:/Users/shaohefe/Downloads/vt- ...

  4. linux判断日志文件大小进行清理

    脚本写了一个死循环,根据nohup产生的日志多大, 这里表示日志超过500M之后清理, 具体数字可自定义 睡眠数可自定义 #!/usr/bin/bash while true do s=`du -k ...

  5. socket之 select模型

    前段时间一直想学习网络编程的select模型,看了<windows网络编程>的介绍,参考了别人的博客. 这里的资料主要来自http://www.cnblogs.com/RascallySn ...

  6. ​DL_WITH_PY系统学习(第3章)

    本节提示: 1.DL的核心构建 2.Keras的简单介绍 3.搭建DL机器训练环境 4.使用DL模型解决基础问题 3.1 DL的基本构建:layer layer的定义:以1个或多个tensor作为输入 ...

  7. 20145325张梓靖 《网络对抗技术》 Web基础

    20145325张梓靖 <网络对抗技术> Web基础 实验内容 开启apahce,设计web前端HTML 设计web前端javascipt 设计web后端mysql 设计web后端php ...

  8. Ubuntu 18.04 休眠后无法唤醒的解决办法

    解决办法:安装laptop-mode-tools工具包. 1.检查是否安装了grep laptop-mode-tools 工具包 $ dpkg -l | grep laptop-mode-tools ...

  9. windows下如何安装vundle?

    参考: http://blog.csdn.net/zhuxiaoyang2000/article/details/8636472 vundle是gmarik 受 ruby的 bunler的启发开发的. ...

  10. utf-8并不"兼容" gb2312, gb18030

    注意 utf-8 并不是 向下 兼容"gb2312 gb18030"等编码, 也并不是说, utf-8就是比 gb2312等高级的编码! 比如在terminal中, 你开始使用的 ...