比赛 |
20120711 |
评测结果 |
AAWWWWWWWWAAWWW |
题目名称 |
路由器 |
最终得分 |
26 |
用户昵称 |
isabella |
运行时间 |
0.425 s |
代码语言 |
Pascal |
内存使用 |
9.03 MiB |
提交时间 |
2012-07-11 09:44:34 |
显示代码纯文本
type
point=^node;
node=record
data:longint;
next:point;
end;
var
f:array[1..100000,-10..10]of longint;
ch,map:array[1..100000]of point;
v:array[1..100000]of boolean;
n,k,i,j,l,a,b,ans:longint;
p:point;
procedure make(x:longint);
var p,po:point;
begin
v[x]:=true;
p:=map[x];
while p<>nil do
begin
if v[p^.data]=false then
begin
new(po);po^.data:=p^.data;po^.next:=ch[x];
ch[x]:=po;make(p^.data);
end;
p:=p^.next;
end;
end;
procedure dp(x:longint);
var p:point;
i,min,num:longint;
begin
p:=ch[x];
if p=nil then
begin
for i:=-k to -1 do f[x,i]:=0;
for i:=0 to k do f[x,i]:=1;
exit;
end;
while p<>nil do
begin dp(p^.data);p:=p^.next;end;
for i:=-k to 0 do
begin
p:=ch[x];f[x,i]:=0;
while p<>nil do
begin f[x,i]:=f[x,i]+f[p^.data,i+1];p:=p^.next;end;
end;
for i:=1 to k-1 do
begin
min:=maxlongint;f[x,i]:=0;
p:=ch[x];
while p<>nil do begin
f[x,i]:=f[x,i]+f[p^.data,-i];
if f[p^.data,i+1]<min then
begin min:=f[p^.data,i+1];num:=p^.data;end;
p:=p^.next;
end;
f[x,i]:=f[x,i]-f[num,-i]+min;
end;
f[x,k]:=1;p:=ch[x];
while p<>nil do
begin f[x,k]:=f[x,k]+f[p^.data,-k];p:=p^.next;end;
end;
begin
assign(input,'routea.in');reset(input);
assign(output,'routea.out');rewrite(output);
readln(n,k);
for i:=1 to n-1 do
begin
readln(a,b);
new(p);p^.data:=b;p^.next:=map[a];map[a]:=p;
new(p);p^.data:=a;p^.next:=map[b];map[b]:=p;
end;
fillchar(v,sizeof(v),0);
make(1); //writeln('fa');halt;
dp(1); //writeln('fa');halt;
{for i:=1 to n do
begin
p:=ch[i];
while p<>nil do
begin
write(p^.data,' ');
p:=p^.next;
end;
writeln;
end; }
ans:=maxlongint;
for i:=0 to k do
if f[1,i]<ans then ans:=f[1,i];
writeln(ans);
close(input);close(output);
end.