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+20,inf = 2e9, mod = 1e9+7; 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]=1; ans[u]=u; mxs[u]=u; int mx=0; for(int i=0;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]/2) ans[u] = fa[ans[u]]; } int main() { scanf("%d%d",&n,&m); for(int i=2;i<=n;i++) { scanf("%d",&x); G[x].push_back(i); G[i].push_back(x); fa[i]=x; } dfs(1,0); for(int i=1;i<=m;i++) { scanf("%d",&x); printf("%d\n",ans[x]); } return 0; }