Codeforces 686 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的更多相关文章
- 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 ...
- 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级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心 ...
- 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 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 ...
- Codeforces Round #359 (Div. 2) D - Kay and Snowflake
D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...
- CF685B Kay and Snowflake 贪心
CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更 ...
- B. Kay and Snowflake 解析(思維、DFS、DP、重心)
Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...
- 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 ...
随机推荐
- java读取文件和写入文件的方式
https://www.cnblogs.com/fnlingnzb-learner/p/6011324.html
- 在VS2010中使用Git
转载:https://www.cnblogs.com/oec2003/archive/2012/11/13/2768860.html 一. 安装Git命令行,下载地址:http://code.goog ...
- 【python54--爬虫2】
1.有道翻译 ''' |-- 代码思路解析: |-- 1.拿到网址首先查看network内Headers的:Request URL:User-Agent:From Data,这几个就是代码所需要的ur ...
- 2018年12月7日 字符串格式化2 format与函数1
tp7="i am \033[44;1m %(name)-25.6s\033[0m"%{"name":"sxj2343333"} print ...
- Spring 学习——Spring框架结构、概念
maven项目结构 记忆:在一个项目中,project下一层级时src,也就是源文件,所有需要进行编译的文件都是在这个目录下,其实也就是这一个目录,然后向下扩展.在src目录下,存在main文件夹,里 ...
- ODAC(V9.5.15) 学习笔记(九)TOraSQLMonitor
名称 类型 说明 Active Boolean 激活SQL跟踪 DBMonitorOptions 将跟踪信息发送到dbMonitor工具软件的选择项 Host IP地址 Port 端口号 Reconn ...
- How to search for ? (question mark) in Excel
The ? is a wildcard which represents a single character, and the * is a wildcard character that repr ...
- R class of subset of matrix and data.frame
a = matrix( c(2, 4, 3, 1, 5, 7), # the data elements nrow=2, # number of rows ...
- HDU 1465 不容易系列之一
扯淡 貌似有傻逼的做法XD 话说我没开long long,忘读入n,忘了清零ans WA了三遍是什么操作啊 傻了傻了 思路 显然是一个错排问题啊XD 但是我们不套公式,我们用一发二项式反演 二项式反演 ...
- 关于视觉跟踪中评价标准的相关记录(The Evaluation of Visual Tracking Results on OTB-100 Dataset)
关于视觉跟踪中评价标准的相关记录(The Evaluation of Visual Tracking Results on OTB-100 Dataset) 2018-01-22 21:49:17 ...