记录编号 39477 评测结果 AAAAAAAAAAAAAAA
题目名称 路由器 最终得分 100
用户昵称 Gravatarfuhao 是否通过 通过
代码语言 Pascal 运行时间 0.214 s
提交时间 2012-07-11 21:15:09 内存使用 9.03 MiB
显示代码纯文本
program Routea;
const maxn=100001; maxm=1000001;
var
 n,k,i,x,y,ans,tot:longint;
 needhelp,givehelp,a:array[0..maxn] of longint;
 next,link:array[0..maxm] of longint;
 v:array[0..maxn] of boolean;
 procedure insert(x,y:longint);
 begin
  inc(tot); next[tot]:=a[x]; a[x]:=tot;
  link[tot]:=y;
 end;

 procedure tree_dp(x:longint);
 var leaf:boolean;  i:longint;
 begin
  if v[x] then exit;
  v[x]:=true;
  i:=a[x];
  needhelp[x]:=0; givehelp[x]:=maxlongint;
  leaf:=true;
  while i<>0 do
   begin
    if not v[link[i]] then
     begin
      leaf:=false;
      tree_dp(link[i]);
      if needhelp[x]<needhelp[link[i]] then
       needhelp[x]:=needhelp[link[i]];
      if givehelp[x]>givehelp[link[i]] then
       givehelp[x]:=givehelp[link[i]];
     end;
    i:=next[i];
   end;
  if leaf then
   begin
    needhelp[x]:=1; givehelp[x]:=maxlongint div 2;
    if needhelp[x]>k then
     begin
      ans:=ans+1;
      givehelp[x]:=1; needhelp[x]:=0;
     end;
    exit;
   end;
  if givehelp[x]+needhelp[x]<=k then
   begin
    needhelp[x]:=0; inc(givehelp[x]);
    exit;
   end;
  if needhelp[x]>=k then
   begin
    ans:=ans+1;
    givehelp[x]:=1; needhelp[x]:=0;
    exit;
   end;
  inc(givehelp[x]); inc(needhelp[x]);
  if x=1 then ans:=ans+1;
 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
   read(x,y);
   insert(x,y);
   insert(y,x);
  end;
 tree_dp(1);
 writeln(ans);
 close(input); close(output);
end.