codeforces 686D D. Kay and Snowflake(dfs)
题目链接:
3 seconds
256 megabytes
standard input
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.
7 4
1 1 3 3 5 3
1
2
3
5
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)的更多相关文章
- 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 686 D - Kay and Snowflake
D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心 ...
- 【24.63%】【codefroces 686D】Kay and Snowflake
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- Kay and Snowflake CodeForces - 686D
Kay and Snowflake CodeForces - 686D 题意:给一棵有根树,有很多查询(100000级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心 ...
- 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,将重心去掉了以后所有子树的顶点 ...
- B. Kay and Snowflake 解析(思維、DFS、DP、重心)
Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...
- CF685B Kay and Snowflake 贪心
CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更 ...
随机推荐
- POJ3037 Skiing
Skiing 题目大意: 给定一个M*N的网格,已知在每个网格中的点可以向上下左右四个方向移动一个单位,每个点都有一个高度值. 从每个点开始移动时存在一个速度值,从A点移动到B点,则此时B点的速度为& ...
- 【转】关于大型网站技术演进的思考(二十)--网站静态化处理—web前端优化—中(12)
Web前端很多优化原则都是从如何提升网络通讯效率的角度提出的,但是这些原则使用的时候还是有很多陷阱在里面,如果我们不能深入理解这些优化原则背后所隐藏的技术原理,很有可能掉进这些陷阱里,最终没有达到最佳 ...
- SPOJ LCS2 多个串的最长公共子串
这里串最多有10个,找所有串的最长公共子串 这里后缀自动机做,以第一个串建立后缀自动机,后面的串一个个去匹配,每次得到当前串在可到达状态上所能得到的最长后缀长度 拿所有串匹配后得到的结果进行计算 #i ...
- hdu 3371
#include<stdio.h> #include<stdlib.h> #define N 501 struct node { int x,y,dis; }road[N*N] ...
- WordPress升级错误:class-wp-filesystem-direct.php on line 122
错误描述:WordPress在后台进行版本升级时,出错,之后进入前台或者后台,都无法访问进入,错误信息如下:Warning: copy(/home/xxx/public_html/wordpress/ ...
- Fedora20 安装 MySQL
参考资料: http://www.cnblogs.com/focusj/archive/2011/05/09/2057573.html http://linux.chinaunix.net/techd ...
- 通过继承Thread类来创建多线程 关键词 start
// 代码 public class MyThread extends Thread { public void run(){ for(int i=0;i<10;i++){ try { Thre ...
- Docker安装redis操作命令
最近学习了redis,那么今天我们来学习以下在Docker上安装我们的redis,并查看有关redis的一系列命令 查找redis docker search redis 拉取redis docker ...
- Eclipse同时显示多个控制台项目的输出
操作步骤: 1.运行项目1,运行项目2 2.在Exlipse中选择这两个的控制台进行切换
- Linux中断处理驱动程序编写
本章节我们一起来探讨一下Linux中的中断 中断与定时器:中断的概念:指CPU在执行过程中,出现某些突发事件急待处理,CPU暂停执行当前程序,转去处理突发事件,处理完后CPU又返回原程序被中断的位置继 ...