题目链接:

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).

 
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.

 
Example
 
input
7 4
1 1 3 3 5 3
1
2
3
5
output
3
2
3
6 题意: 给一棵树,有q个询问,询问以x为根的子树的质心是哪个节点; 思路: son[x]表示根为x的子树的节点个数;dfs的过程中可以计算son;
每次可以发现一个子树的质心要么在根上(x所有子节点的son都小于son[x]/2),要么在节点数最多的子树上;
在子树上时可以按最大的子节点的质心到x找到质心;最后就是O(1)的询问;
给一个关于树的重心的博客:转送门 AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e5+;
const int maxn=; int n,q,son[N],ans[N],root[N];
vector<int>ve[N];
void dfs(int x,int fa)
{
int len=ve[x].size(),mmax=,pos=x;
son[x]=;
ans[x]=x;
for(int i=;i<len;i++)
{
int y=ve[x][i];
if(y==fa)continue;
dfs(y,x);
son[x]+=son[y];
if(son[y]>mmax)
{
mmax=son[y];
pos=y;
}
}
ans[x]=ans[pos];
while(ans[x]!=x&&son[x]>*son[ans[x]])
ans[x]=root[ans[x]];
}
int main()
{
read(n);read(q);
int x;
for(int i=;i<=n;i++)
{
read(x);
root[i]=x;
ve[x].push_back(i);
ve[i].push_back(x);
}
dfs(,-);
while(q--)
{
read(x);
printf("%d\n",ans[x]);
}
return ;
}

codeforces 686D D. Kay and Snowflake(dfs)的更多相关文章

  1. 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 ...

  2. Codeforces 686 D - Kay and Snowflake

    D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心 ...

  3. 【24.63%】【codefroces 686D】Kay and Snowflake

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Kay and Snowflake CodeForces - 686D

    Kay and Snowflake CodeForces - 686D 题意:给一棵有根树,有很多查询(100000级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #359 (Div. 2) D - Kay and Snowflake

    D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点 ...

  8. B. Kay and Snowflake 解析(思維、DFS、DP、重心)

    Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...

  9. CF685B Kay and Snowflake 贪心

    CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更 ...

随机推荐

  1. spring boot学习02【如何在spring boot项目中访问jsp】

    1.配置application.properties文件 打开application.properties追加 spring.mvc.view.prefix=/WEB-ROOT/ spring.mvc ...

  2. python+RobotFramework

    今天有人问我,她想在在robot里面用到数据库的一个值的随机数,但是不知道怎么实现,我用python写了一段代码链接数据库给表中所需的字段的值取随机数,代码如下: import random,pymy ...

  3. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)

    传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...

  4. TOYS(poj 2318)

    题意:就是给了m个点,落在n+1个区域中,问各个区域有多少个点. /* 对于每个玩具,二分其所在的区间,然后用叉积判断. 但是我觉得枚举好像时间复杂度也可以. */ #include<cstdi ...

  5. poj1330+hdu2586 LCA离线算法

    整整花了一天学习了LCA,tarjan的离线算法,就切了2个题. 第一题,给一棵树,一次查询,求LCA.2DFS+并查集,利用深度优先的特点,回溯的时候U和U的子孙的LCA是U,U和U的兄弟结点的子孙 ...

  6. React学习及实例开发(二)——用Ant Design写一个简单页面

    本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.引入Ant Design 1.安装antd yarn add antd 2.引入 react-app-re ...

  7. Hive安装中遇到过的坑

    实现说明每一个用户的环境都有细微的不一致,所以这里只是个人经过这些坑的处理,但是不意味着所有处理都是这样的操作,仅作为参考. 第一个坑 数据库安装,数据库最好装在Linux上,一直出了很多错,这里有一 ...

  8. jQuery的一些总结(持续更新中...)

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6693399.html 1. $.expr[':']  过滤 ...

  9. datasnap中间件如何控制长连接的客户端连接?

    ActiveConnections: TClientDataSet; ... 有客户端连接上来的时候 procedure TForm8.DSServer1Connect(DSConnectEventO ...

  10. Jackson说明

    Jackson说明 package com.stono.sboot2_chp4_jackson.controller; import com.fasterxml.jackson.annotation. ...