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. elasticsearch配置文件详解

    来自:http://www.searchtech.pro/articles/2013/02/18/1361194291548.html elasticsearch的config文件夹里面有两个配置文 ...

  2. Systen,IO

    private void CreateHtml(string sPath, string txt) { string currPath = @"C:\MyCodeHelper" + ...

  3. 【题解】Luogu P4679 [ZJOI2011]道馆之战

    原题传送门 码农题树剖好题,口袋妖怪是个好玩的游戏 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 下文左右就代表树的节点按dfs序后的左右,上.下分别表示每个节点的A.B区域 考虑在链上的情况, ...

  4. 【4OpenCV】OpenCV和RTSP的综合研究

    一.RTSP是什么?用来干什么? RTSP(Real Time Streaming Protocol),RFC2326,实时流传输协议,是TCP/IP协议体系中的一个应用层协议,由哥伦比亚大学.网景和 ...

  5. 20165310 NstSec2019 Week3 Exp1 逆向与Bof基础

    20165310 NstSec2019 Week3 Exp1 逆向与Bof基础 一.实验内容 实验目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用fo ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem A - B

    Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...

  7. topcoder srm 705 div1 -3

    1.设有一个字母表$T$,对于一个单词$w$,若对于任意的$0\leq i< |w|-1$,满足$w_{i}$在$T$中的排名小于等于$w_{i+1}$在$T$中的排名,则称$s$在$T$中是合 ...

  8. Android系统更新防互刷功能实现与分析【转】

    本文转载自:https://blog.csdn.net/huangyabin001/article/details/44465145 版权声明:本文为博主原创文章,未经博主允许不得转载.    htt ...

  9. CentOS7 时间设置与网络同步

    1.查看时区 [root@localhost /]# date -R Thu, Jul :: + +0800表示东八区,这边就不用再设置 时区中的CST表示中国标准时间. 时区相关共享文件在/usr/ ...

  10. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...