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 ...
随机推荐
- 标准库errno.h 查看错误代码编号,errno:4 与error:2
log 里报错,errno:4 与errno:2 查了一下 errno.h --------下文来自百度百科 errno 编辑 errno 是记录系统的最后一次错误代码.代码是一个int型的值 ...
- dedecms升级后报错
DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- ThinkPHP邮件发送函数示例
ThinkPHP邮件发送函数示例详解 /** * 发送邮件 * @param $tomail * @param $subject * @param $body * @param string $con ...
- Swig 使用指南
如何使用 API swig.init({ allowErrors: false, autoescape: true, cache: true, encoding: 'utf8', filters: { ...
- kindle paperwhite折腾记
在亚马逊官网上买了一个kindle paperwhite 一代(849元) , 打算再买个皮套, 淘宝店 http://detail.tmall.com/item.htm?spm=a230r.1.1 ...
- 微信用户量破6.5亿 首超移动QQ
腾讯控股有限公司昨日公布了微信和WeChat合并月活跃用户量达到6.5亿,同比再涨39%.QQ在移动智能终端月活为6.39亿,同比增长18%,尽管势头也不错,但这是第一次,微信干掉了移动QQ! 201 ...
- LVS负载均衡集群服务搭建详解(一)
LVS概述 1.LVS:Linux Virtual Server 四层交换(路由):根据请求报文的目标IP和目标PORT将其转发至后端主机集群中的某台服务器(根据调度算法): 不能够实现应用层的负载均 ...
- ios socket(基础demo)
http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html clinetSocket 1.viewcontroller.h @interface View ...
- 服务器部署之 cap deploy:setup
文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com $ cap deploy:setup 执行到这一步的时候会时间较长,可以直接中断 * executing &q ...