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 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).
Input
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.
Output
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.
Sample Input
7 4
1 1 3 3 5 3
1
2
3
5
Sample Output
3
2
3
6
Hint
给你一棵树,Q次询问,每次询问这个点的中心是啥
题解:
离线做
重心是啥?就是砍掉这个点之后,剩下的树的大小都小于原树大小的1/2
那么我把这个点的所有子树都看一下,把最接近1/2的那个子树剁掉就好了,剩下的肯定满足……
这个启发式合并一下,nlognlogn很容易实现
但是其实,直接dfs一波就好了,递归处理,那个儿子肯定是在他的重儿子那儿,然后不停往上爬就好饿了
复杂度均摊一下,其实没有多大。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
vector<int>E[maxn];
int n,q,sz[maxn],ans[maxn],f[maxn];
void dfs(int x){
ans[x]=x;
sz[x]=1;
for(int i=0;i<E[x].size();i++){
int v=E[x][i];
dfs(v);
sz[x]+=sz[v];
}
for(int i=0;i<E[x].size();i++){
if(sz[E[x][i]]*2>sz[x])
ans[x]=ans[E[x][i]];
}
while((sz[x]-sz[ans[x]])*2>sz[x])
ans[x]=f[ans[x]];
}
int main(){
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++){
scanf("%d",&f[i]);
E[f[i]].push_back(i);
}
dfs(1);
for(int i=1;i<=q;i++){
int x;scanf("%d",&x);
printf("%d\n",ans[x]);
}
}
Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs的更多相关文章
- 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,将重心去掉了以后所有子树的顶点 ...
- 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 ...
- 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. 1)
A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...
- Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力
A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...
- Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...
随机推荐
- MVC中检测到有潜在危险的 Request.Form 值
在做mvc项目时,当使用xhedit or.ueditor编辑器时,点击提交时,编辑器中的内容会带有html标签提交给服务器,这时就是会报错,出现如下内容: “/”应用程序中的服务器错误. 从客户端( ...
- java四舍五入BigDecimal和js保留小数点两位
java四舍五入BigDecimal保留两位小数的实现方法: // 四舍五入保留两位小数System.out.println("四舍五入取整:(3.856)=" + ne ...
- 你的组织使用了 windows defender 应用程序控制来阻止此应用
=============================================== 2018/2/8_第1次修改 ccb_warlock === ...
- No.20 selenium学习之路之文件读写
1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.tx ...
- python随笔(三)
在对字符串的操作中,s[::-1]表示将字符串逆序输出. 字符串本身不能改变(管理者而非所有者) 列表的内容是可以改变的,且列表的内容可以不仅仅是字符串.对于一个列表,注意b=a和b=a[:]的区别. ...
- python3 安装
Centos7 安装python3 #安装sqlite-devel yum -y install sqlite-devel #安装依赖 yum -y install make zlib zlib-de ...
- Codeforces 332B Maximum Absurdity(DP+前缀和处理)
题目链接:http://codeforces.com/problemset/problem/332/B 题目大意:给你n个数和一个整数k,要求找到不相交的两个长度为k的区间,使得区间和最大,输出这两个 ...
- 向SQL Server 现有表中添加新列并添加描述.
注: sql server 2005 及以上支持. 版本估计是不支持(工作环境2005,2008). 工作需要, 需要向SQL Server 现有表中添加新列并添加描述. 从而有个如下存储过程. (先 ...
- 解决mysql不能远程登入的问题
mysql远程不能登入,问题就在于当时设置的账号只限制本地访问,mysql默认也只是本地访问. 之前的设置: 通过命令行登录管理MySQL服务器(提示输入密码时直接回车): mysql> /us ...
- Ionic入门四:卡片
近年来卡片(card)的应用越来越流行,卡片提供了一个更好组织信息展示的工具. 针对移动端的应用,卡片会根据屏幕大小自适应大小. 我们可以很灵活的控制卡片的显示效果,甚至实现动画效果. 卡片一般放在页 ...