1131: [Poi2008]sta time limit:10 Sec Memory limit:162 MB
submit:783 solved:235
[Submit] [Status] Description
Give a tree of n points, find a point to the tree with this point as root, and the sum of the depths of all points is the maximum
Input
gives a number n, which represents n points. n<=1000000 below N-1 edges.
Output
Output the points you find, and if you have multiple solutions, output the one with the smallest number.
Sample Input8
1 4
5 6
4 5
6 7
6 8
2 4
3 4Sample Output7HINT Source
The following: A tree-like DP, first casually make a point then build, find out the depth of each point, and then sum, and then in this you establish a root tree branches of the DP, at the root of the various points of the depth is not difficult to maintain--set the current sum of x, you want to transfer to the sub-node total y node (count yourself) The entire tree N nodes, then the sum of the transfer after the x-y+ (n-y), the other is not ... For P Party beware 202 (Burst stack!!!) )
1 {$M 10000000,0,maxlongint}2 type3Point=^node;4Node=Record5 G:longint;6 Next:point;7 End;8 9 varTen I,j,k,l,m,n:longint; One A1,a2:int64; AA:Array[0..2000000] ofPoint ; -B,c,d,e:Array[0..2000000] ofInt64; - procedureAdd (x,y:longint); inline; the varP:point; - begin - ifX=y Thenexit; - new (p); +p^.g:=y; -p^.next:=A[x]; +a[x]:=p; A End; at procedurebuilt (x:longint), inline; - varP:point; - begin -p:=A[x]; -c[x]:=1; - whileP<>Nil Do in begin - ifd[p^.g]=0 Then to begin +d[p^.g]:=1; -b[p^.g]:=b[x]+1; the built (P^.G); *c[x]:=c[x]+C[P^.G]; $ End;Panax Notoginsengp:=P^.next; - End; the End; + procedurerun (x:longint); inline; A varP:point; the begin +p:=A[x]; - whileP<>Nil Do $ begin $ ifd[p^.g]=0 Then - begin -d[p^.g]:=1; theE[p^.g]:=e[x]+int64 (n)-(2*C[P^.G]); - run (P^.G);Wuyi End; thep:=P^.next; - End; Wu End; - About begin $ READLN (n); - fori:=1 toN Doa[i]:=Nil; - fori:=1 toN-1 Do - begin A readln (j,k); + Add (j,k); add (k,j); the End; -Fillchar (b,sizeof (b),0); $Fillchar (C,sizeof (c),0); theFillchar (d,sizeof (d),0); thed[1]:=1; theBuilt1); theFillchar (d,sizeof (d),0); -Fillchar (E,sizeof (E),0); ind[1]:=1; the fori:=1 toN Doe[1]:=e[1]+B[i]; theRun1); Abouta1:=-1; a2:=0; the fori:=1 toN Do the begin the ifE[i]>a1 Then + begin -a1:=E[i]; thea2:=i;Bayi End; the End; the Writeln (A2); - Readln; - End.
1131: [Poi2008]sta