Topic Link: Portal
The main idea: to give you n points, n-1 bar to connect all points constitute a tree, 1 is the root, there is M inquiry, for each inquiry point x, in the X and X sub-tree to find a point, so that the deletion of the point, the resulting inclusion of the most elements of the Unicom sub-block
The number of points contained in the <= is 1/2 of the sum of the points of the original X and the X sub-tree. Output this point.
Topic thinking: The game thought of a method, recursive to find the connectivity of each point and then look for the largest link in the sub-tree points up constantly updated, but the method is wrong, such as to a chain, asked the root 1, the answer is wrong
Later when the problem is always considered how to delete a sub-tree in a point can produce the maximum number of connected blocks to save and update up, in this is stuck. After reading this blog, it dawned on me, actually
Recursive to the leaf node, then the answer to all the leaf nodes is it itself, since the boundary condition is out, we just need to update up. (The real tag is the tree-shaped DP)
Before that, there is another conclusion: for any x, the answer must be in it and its subtree.
We will finally get a link with the largest number of points from X, and at the same time get a sub-node that is directly connected to this Unicom block, since it is recursive,
Then the number of points to pass up the Unicom block, while the answer is also passed up at the same time, then the answer to X is the child node's answer (there may be an answer to the X is not legal), if the answer is not legal, use
The above conclusion is to find the parent node of the answer node until the condition is met.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<cstring>#include<stack>#include<cctype>#include<queue>#include<string>#include<vector>#include<Set>#include<map>#include<climits>#defineLson Root<<1,l,mid#defineRson Root<<1|1,mid+1,r#defineFi first#defineSe Second#definePing (x, y) ((x-y) * (x-y))#defineMST (x, y) memset (x,y,sizeof (x))#defineMCP (x, y) memcpy (x,y,sizeof (y))using namespacestd;#defineGamma 0.5772156649015328606065120#defineMOD 1000000007#defineINF 0x3f3f3f3f#defineN 300005#defineMAXN 1005typedef pair<int,int>Pii;typedefLong LongLL;intn,m,k,x,num,y;intPre[n],ans[n],size[n];structnode{intTo,next;} Node[n<<1];intHcnt,head[n];inlinevoidAddintXinty) {node[hcnt].to=y;node[hcnt].next=Head[x]; HEAD[X]=hcnt++;}voidDfsintXintFA) { intT=x;///T represents the point in which the connected component with the highest number of points is directly connected to xsize[x]=1;///A size indicates how many points the current node contains altogether intmx=0; for(intI=head[x];~i;i=Node[i].next) { intE=node[i].to; if(E==FA)Continue; DFS (E,X); SIZE[X]+=Size[e]; if(size[e]>MX) {MX=Size[e]; T=e; }} Ans[x]=t==x?X:ans[t]; while(ans[x]!=x&&size[x]-size[ans[x]]>size[x]/2) {Ans[x]=Pre[ans[x]]; }}intMain () {inti,j,group,v; scanf ("%d%d",&n,&m); MST (Head,-1); for(i=2; i<=n;++i) {scanf ("%d",&x); Add (i,x); add (x,i); Pre[i]=x; } DFS (1,-1); while(m--) {scanf ("%d",&x); printf ("%d\n", ans[x]); } return 0;}
Codeforces Round #359 (Div. 2) D. Kay and Snowflake