Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP
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.
7 4
1 1 3 3 5 3
1
2
3
5
3
2
3
6
The first query asks for a centroid of the whole tree — this is node 3. If we delete node 3 the tree will split in four components, two of size1 and two of size 2.
The subtree of the second node consists of this node only, so the answer is 2.
Node 3 is centroid of its own subtree.
The centroids of the subtree of the node 5 are nodes 5 and 6 — both answers are considered correct.
题意:
给你一棵树,n点n-1边,m个询问,每次询问你x点及其子树中选择一个点删除,最后这个子树的形成的树枝中节点树不超过siz[x]/2,输出这个点
题解:
假设x点的答案是ans[x];
对于新的点x我们会在那个儿子的子树中选点去删除或者本身呢,一定是在siz[son]最大的儿子里面选点
假设新x点的答案不能构成<=siz[x]/2 那么加我们选的点想起父亲节点走上去再更新就好了,直到有解
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+,inf = 2e9, mod = 1e9+;
typedef long long ll; vector<int > G[N];
int n,m,x,siz[N],mxs[N],ans[N],fa[N];
void dfs(int u,int f)
{
siz[u]=;
ans[u]=u;
mxs[u]=u;
int mx=;
for(int i=;i<G[u].size();i++)
{
int to= G[u][i];
if(to==f) continue;
dfs(to,u);
siz[u]+=siz[to];
if(mx<siz[to])
{
mx=siz[to];
mxs[u]=to;
}
}
ans[u]=ans[mxs[u]];
while(ans[u]!=u&&siz[u]-siz[ans[u]] > siz[u]/) ans[u] = fa[ans[u]];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
G[x].push_back(i);
G[i].push_back(x);
fa[i]=x;
}
dfs(,);
for(int i=;i<=m;i++)
{
scanf("%d",&x);
printf("%d\n",ans[x]);
}
return ;
}
Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP的更多相关文章
- 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. 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 #587 (Div. 3) F Wi-Fi(线段树+dp)
题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- 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 ...
随机推荐
- VirtualBox下安装rhel5.5 linux系统
以前也用过VMware server和VMware workstation虚拟机,现在使用了一段时间VirtualBox,感觉它比较轻巧,很适合我,在Win7系统下用起来很方便.下面详细介绍下在Vir ...
- js控制固定div和随屏滚动div兼容多浏览器和纯css控制(来自网络)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- httpd/php/mysql的安装-1
rely: 依赖: relier, reliance , reliable : i don't think he is a reliable man . we must lay stress on ...
- 前端框架react研究
摘要: 最近公司要做一个嵌套在app中的应用,考虑着用Facebook的react来开发view,所以就研究了下.下面是我在开发中遇到的坑,希望能给你帮助. 项目地址:https://github.c ...
- iOS7: 如何获取不变的UDID
如何使用KeyChain保存和获取UDID 本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不变UDID的问题.并给出一个获取UDID的工具类,使 ...
- spring 注解的总结
一.java内置注解 1.@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: ElemenetType.CONSTRUCTOR 构造器声明 ElemenetTyp ...
- Unity3d与iOS交互开发——接入平台SDK必备技能
原地址:http://www.2cto.com/kf/201401/273337.html# 前言废话:开发手机游戏都知道,你要接入各种平台的SDK.那就需要Unity3d与iOS中Objective ...
- easyui-datagrid 两次请求
原因分析及解决方案 html代码中利用class声明了datagrid,导致easyUI解析class代码的时候先解析class声明中的datagrid,这样组件就请求了一次url:然后又调用js初始 ...
- calico for kubernetes
(这一篇中很多错误,勿参考!) The reference urls: https://github.com/kubernetes/kubernetes/blob/master/docs/gettin ...
- JQuery $.ajax()方法详解
jQuery Ajax 参考手册 实例 通过 AJAX 加载一段文本: jQuery 代码: $(document).ready(function(){ $("#b01").cli ...