Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心
题目链接:
题目
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
问题描述
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.
After doing some research he formed q queries he is interested in. The i-th query asks to find a centroid of the subtree of the node vi. Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node v is formed by nodes u, such that node v is present on the path from u to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
输入
The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively.
The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the indices of the parents of the nodes from 2 to n. Node 1 is a root of the tree. It's guaranteed that pi define a correct tree.
Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.
输出
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
样例
input
7 4
1 1 3 3 5 3
1
2
3
5
output
3
2
3
6
题意
求需要查询的子树的重心。
题解
对于点u,它的重心会在它的以重儿子为根的子树的重心和u的路径上。
可以用dfs直接暴力递归。(如果u的重儿子子树的重心深度比较高,那么u的重心深度也会比较高,严格的时间证明也不懂。。)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 3e5 + 10;
int n, m;
int siz[maxn], ans[maxn],f[maxn];
vector<int> G[maxn];
void dfs(int u) {
siz[u] = 1; ans[u] = u;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
siz[u] += siz[v];
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (siz[v] * 2 > siz[u]) {
ans[u] = ans[v];
break;
}
}
while ((siz[u] - siz[ans[u]]) * 2>siz[u]) ans[u] = f[ans[u]];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 2; i <= n; i++) {
scanf("%d", f + i);
G[f[i]].push_back(i);
}
dfs(1);
while (m--) {
int v; scanf("%d", &v);
printf("%d\n", ans[v]);
}
return 0;
}
Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心的更多相关文章
- 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. 1) B. Kay and Snowflake dfs
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...
- Codeforces Round #359 (Div. 2) D - Kay and Snowflake
D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...
- Codeforces Round #359 (Div. 1)
A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- D. Kay and Snowflake 树的重心
http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Codeforces Round #359 (Div. 2)C - Robbers' watch
C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)
题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...
随机推荐
- 使用jquery插件报错:TypeError:$.browser is undefined的解决方法
关于$.browser browser就是用来获取浏览器基本信息的. jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.sup ...
- 小菜鸟带着梦想学chromium
风雨送春归, 飞雪迎春到. 已是悬崖百丈冰, 犹有花枝俏. 俏也不争春, 只把春来报. 待到山花烂漫时, 她在丛中笑. 这首卜算子·咏梅可是应了我的心情了.最近换工作,受到频频打击,面试过程中发现满世 ...
- 利用ExcelDataReader封装类 导入表格数据
nuget 添加Install-Package ExcelDataReader
- 模拟n步一维随机游走的情况。
package randomWalk; import java.util.Random; import java.util.Scanner; public class RandomWalk { pub ...
- POJ1056 IMMEDIATE DECODABILITY【数据结构】
题目地址:http://poj.org/problem?id=1056 Description An encoding of a set of symbols is said to be immedi ...
- Git的学习总结和使用时遇到的问题。
git 是一款非常强大的版本控制工具,现在市场占有率应该是一家独大了,以前用svn的童鞋估计都转投git阵营了吧 加上很多公司也用git管理自己的项目,所以 ...
- VMware Workstation中linux(centos)与windows7共享文件夹
引用网站有: http://www.jb51.net/LINUXjishu/161994.html http://www.cnblogs.com/xiehy/archive/2011/12/19/22 ...
- Ubuntu16.04.1 安装MyCat
Mycat是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而Mycat并没有存储引擎,所以并不是完全意义的分布式数据库系统. 安装Java环境,配置全局环境变量 MyCAT是使用JAV ...
- mongodb 入门笔记
选择Mongo的关键是:这是一个 JSON 文档数据库. 1. Mongo 的术语 文档:一条完整的数据就是一个文档(对应于 MySQL 的一行). 集合:一组文档构成一个集合.类似 MySQL 中表 ...
- ros的源码阅读
测试代码,使用xmlrpc与roscore通信 ros的框架是使用rpc与server端通信,server维护topic的publisher,subscriber,param server,servi ...