比赛 20120711 评测结果 WWWWWWWWWTTTWTT
题目名称 路由器 最终得分 0
用户昵称 fuhao 运行时间 6.321 s
代码语言 Pascal 内存使用 74.65 MiB
提交时间 2012-07-11 11:55:41
显示代码纯文本
const maxn=100001; maxm=3000001;
var
 ans,s,t,i,n,k,tot,tt,x,y,min:longint; found:boolean;
 aa,step,f:array[0..maxn] of longint;
 linkk,link,nextt,next,c,un:array[0..maxm] of longint;
 h,hnum,a:array[0..4*maxn+2] of longint;
 v:array[0..maxn] of boolean;
 procedure insert(x,y,z:longint);
 begin
  inc(tot); next[tot]:=a[x]; a[x]:=tot;
  link[tot]:=y; c[tot]:=z; un[tot]:=tot+1;
  inc(tot); next[tot]:=a[y]; a[y]:=tot;
  link[tot]:=x; c[tot]:=0; un[tot]:=tot-1;
 end;
 procedure bfs(s:longint);
 var h,t,i:longint;
 begin
  h:=0; t:=1;
  fillchar(v,sizeof(v),#0);
  f[1]:=s; v[s]:=true; step[s]:=0;
  repeat
   inc(h);
   i:=aa[f[h]];
   while i<>0 do
    begin
     if (not v[linkk[i]]) and (linkk[i]>s) then
      begin
       v[linkk[i]]:=true;
       if step[f[h]]+1<=k then
        begin
         inc(t);
         f[t]:=link[i];
         step[link[i]]:=step[f[h]]+1;
         insert(s,linkk[i]+n,1);
         insert(linkk[i],s+n,1);
         insert(s+2*n,linkk[i]+3*n,1);
         insert(linkk[i]+2*n,s+3*n,1);
        end;
      end;
     i:=nextt[i];
    end;
  until h>=t;
 end;
 procedure sap(k:longint);
 var i,hmin,temp:longint;
 begin
  if k=t then
   begin
    found:=true;
    ans:=ans+min;
    exit;
   end;
  hmin:=t; temp:=min; i:=a[k];
  while i<>0 do
   begin
    if c[i]>0 then
     begin
      if h[link[i]]+1=h[k] then
       begin
        if c[i]<min then min:=c[i];
        sap(link[i]);
        if found then break;
        if h[s]>=t+1 then exit;
        min:=temp;
       end;
      if h[link[i]]<hmin then hmin:=h[link[i]];
     end;
    i:=next[i];
   end;
  if found then
   begin
    dec(c[i],min);
    inc(c[un[i]],min);
    if k=0 then v[link[i]]:=true;
   end else
   begin
    dec(hnum[h[k]]);
    if hnum[h[k]]=0 then h[s]:=t+1;
    h[k]:=hmin+1;
    inc(hnum[h[k]]);
   end;
 end;
 procedure init(x,y:longint);
 begin
  inc(tt); nextt[tt]:=aa[x]; aa[x]:=tt;
  linkk[tt]:=y;
 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(x,y);
   init(x,y);
   init(y,x);
  end;
 for i:=1 to n do bfs(i);
 s:=0;
 for i:=1 to n do
  begin
   insert(s,i,maxlongint div 2);
   insert(i+n,i+2*n,1);
  end;
 t:=4*n+1;
 fillchar(v,sizeof(v),#0);
 for i:=1 to n do insert(i+3*n,t,1);
 hnum[0]:=4*n+2;
 while h[s]<=4*n do
  begin
   found:=false;
   min:=maxlongint;
   sap(s);
  end;
 ans:=0;
 for i:=1 to n do if v[i] then ans:=ans+1;
 writeln(ans);
 close(input); close(output);
end.