链接:戳这里
D. Kay and Snowflake
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
Note
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.
题意:
给出一棵树,要求找出节点的中心
这个中心的定义是 对于查询的节点u。找出u的一颗子树v,切掉v这颗子树,使得剩下子树的大小的最大值都<=原u树的二分之一。
思路:
查询u的中心,那么切除的肯定是size最大的子树,那么递归子树的过程我们只需要存下每颗子树的大小。
再遍历一遍子树,存下最大的满足条件的中点。
但是可能由于中心点还是小了点,那就再往上切一点,直到<=原树的二分之一为止
(真的无法理解写代码的时候他们竟然在发包子 mdzz
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int a[500100];
int head[500100];
struct edge{
int v,next;
}e[600100];
int n,q,tot=0;
void Add(int u,int v){
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
int num[500100],anw[500100];
void DFS(int u,int fa){
num[u]=1;
anw[u]=u;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
DFS(v,u);
num[u]+=num[v];
}
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
if(num[v]*2>=num[u]){
anw[u]=anw[v];
}
}
while((num[u]-num[anw[u]])*2>num[u]){
anw[u]=a[anw[u]];
}
}
int main(){
mst(head,-1);
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++) {
scanf("%d",&a[i]);
Add(a[i],i);
Add(i,a[i]);
}
DFS(1,0);
for(int i=1;i<=q;i++){
int x;
scanf("%d",&x);
printf("%d\n",anw[x]);
}
return 0;
}