1131: [POI2008]Sta

时间:2023-03-09 08:34:39
1131: [POI2008]Sta

1131: [POI2008]Sta

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 783  Solved: 235
[Submit][Status]

Description

给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

Input

给出一个数字N,代表有N个点.N<=1000000 下面N-1条边.

Output

输出你所找到的点,如果具有多个解,请输出编号最小的那个.

Sample Input

8
1 4
5 6
4 5
6 7
6 8
2 4
3 4

Sample Output

7

HINT

Source

题解:一道萌萌哒树状DP,先是随便弄一个点然后建树,求出各点的深度,然后求和,然后在这个你建立的有根树上各个分支进行DP,以各个点为根时的深度并不难维护——设当前总和为X,你想转移到的子节点下属共计Y个节点(算自己),整个树N个节点,那么这次转移后的总和为X-Y+(N-Y),别的没了。。。对于P党小心202(爆栈!!!)
 {$M 10000000,0,maxlongint}
type
point=^node;
node=record
g:longint;
next:point;
end; var
i,j,k,l,m,n:longint;
a1,a2:int64;
a:array[..] of point;
b,c,d,e:array[..] of int64;
procedure add(x,y:longint);inline;
var p:point;
begin
if x=y then exit;
new(p);
p^.g:=y;
p^.next:=a[x];
a[x]:=p;
end;
procedure built(x:longint);inline;
var p:point;
begin
p:=a[x];
c[x]:=;
while p<>nil do
begin
if d[p^.g]= then
begin
d[p^.g]:=;
b[p^.g]:=b[x]+;
built(p^.g);
c[x]:=c[x]+c[p^.g];
end;
p:=p^.next;
end;
end;
procedure run(x:longint);inline;
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if d[p^.g]= then
begin
d[p^.g]:=;
e[p^.g]:=e[x]+int64(n)-(*c[p^.g]);
run(p^.g);
end;
p:=p^.next;
end;
end; begin
readln(n);
for i:= to n do a[i]:=nil;
for i:= to n- do
begin
readln(j,k);
add(j,k);add(k,j);
end;
fillchar(b,sizeof(b),);
fillchar(c,sizeof(c),);
fillchar(d,sizeof(d),);
d[]:=;
built();
fillchar(d,sizeof(d),);
fillchar(e,sizeof(e),);
d[]:=;
for i:= to n do e[]:=e[]+b[i];
run();
a1:=-;a2:=;
for i:= to n do
begin
if e[i]>a1 then
begin
a1:=e[i];
a2:=i;
end;
end;
writeln(a2);
readln;
end.