比赛 20120711 评测结果 AAAWWWWWWWWWWWW
题目名称 路由器 最终得分 20
用户昵称 IMSL77 运行时间 0.632 s
代码语言 Pascal 内存使用 4.99 MiB
提交时间 2012-07-11 11:57:38
显示代码纯文本
program routea;
type
  integer=longint;
const
  maxn=110000;
var
  n,k:integer;
  head,tot,ans,min,tmin:integer;
  ptick:boolean;
  ver:array[1..maxn] of integer;
  en,next:array[1..maxn shl 1] of integer;
  pred,succ:array[1..maxn] of integer;
  mark,tick:array[1..maxn] of boolean;
  d:array[1..maxn,1..4] of integer;

  procedure Fopen;
  begin
    assign(input,'routea.in'); reset(input);
    assign(output,'routea.out'); rewrite(output);
  end;

  procedure Fclose;
  begin
    close(input); close(output);
  end;

  procedure addedge(u,v:integer);
  begin
    inc(tot);
    en[tot]:=v; next[tot]:=ver[u]; ver[u]:=tot;
  end;

  procedure SetGraph;
  var
    i:integer;
    u,v:integer;
  begin
    readln(n,k);
    fillchar(ver,sizeof(ver),0);
    fillchar(next,sizeof(next),0);
    tot:=0;
    for i:=1 to n-1 do
    begin
      readln(u,v);
      addedge(u,v); addedge(v,u);
    end;
  end;

  procedure Init;
  var
    i:integer;
  begin
    head:=1; for i:=2 to n do pred[i]:=i-1;
    for i:=1 to n-1 do succ[i]:=i+1; succ[n]:=0;
    fillchar(mark,sizeof(mark),true);
    fillchar(tick,sizeof(tick),true);
  end;

  function max(a,b:integer):integer;
  begin
    if a>b then exit(a) else exit(b);
  end;

  procedure Dp1(root:integer);
  var
    p,son:integer;
    r:integer;
  begin
    tick[root]:=not tick[root];
    d[root,1]:=0; d[root,2]:=0; d[root,3]:=0;
    p:=ver[root];
    while p>0 do
    begin
      son:=en[p];
      if mark[son] and (tick[son]=ptick) then
      begin
        Dp1(son);
        if d[son,3]+1>=d[root,3] then
        begin
          d[root,3]:=d[son,3]+1; d[root,1]:=son;
        end;
      end;
      p:=next[p];
    end;
    p:=ver[root]; r:=0;
    while p>0 do
    begin
      son:=en[p];
      if mark[son] and (tick[son]=ptick) then
        if (son<>d[root,1]) and (d[son,3]+1>=r) then d[root,2]:=son;
      p:=next[p];
    end;
  end;

  procedure Dp4(root:integer);
  var
    p,son:integer;
    r:integer;
  begin
    tick[root]:=not tick[root];
    p:=ver[root]; r:=0;
    while p>0 do
    begin
      son:=en[p];
      if mark[son] and (tick[son]=ptick) then
      begin
        Dp4(son);
        if (son<>d[root,1]) and (d[son,3]+1>=r) then d[root,2]:=son;
      end;
      p:=next[p];
    end;
  end;

  procedure Dp2(root:integer);
  var
    p,son:integer;
    r:integer;
  begin
    tick[root]:=not tick[root];
    p:=ver[root];
    while p>0 do
    begin
      son:=en[p];
      if mark[son] and (tick[son]=ptick) then
      begin
        d[son,4]:=d[root,4]+1;
        if son=d[root,1] then r:=d[root,2] else r:=d[root,1];
        if (r>0) and (d[r,3]+2>d[son,4]) then d[son,4]:=d[r,3]+2;
        Dp2(son);
      end;
      p:=next[p];
    end;
  end;

  procedure Dp3(root:integer);
  var
    p,son:integer;
  begin
    tick[root]:=not tick[root];
    if max(d[root,3],d[root,4])<tmin then
    begin
      tmin:=max(d[root,3],d[root,4]); min:=root;
    end;
    p:=ver[root];
    while p>0 do
    begin
      son:=en[p];
      if mark[son] and (tick[son]=ptick) then Dp3(son);
      p:=next[p];
    end;
  end;

  procedure delete(p:integer);
  var
    rp,rs:integer;
  begin
    rp:=pred[p]; rs:=succ[p];
    if rp=0 then
    begin
      head:=rs;
      if rs>0 then pred[rs]:=0;
    end else
    begin
      succ[rp]:=rs;
      if rs>0 then pred[rs]:=rp;
    end;
  end;

  procedure DFS(root,dep:integer);
  var
    p,son:integer;
  begin
    if dep>k then exit;
    mark[root]:=false; delete(root);
    p:=ver[root];
    while p>0 do
    begin
      son:=en[p];
      if mark[son] then DFS(son,dep+1);
      p:=next[p];
    end;
  end;

  procedure Solve;
  begin
    ans:=0;
    while head>0 do
    begin
      inc(ans);
      ptick:=true;  Dp1(head);
      ptick:=false; Dp4(head);
      d[head,4]:=0;
      ptick:=true; Dp2(head);
      tmin:=maxlongint;
      ptick:=false;  Dp3(head);
      DFS(min,0);
    end;
    writeln(ans);
  end;

begin
  Fopen;
  SetGraph;
  Init;
  Solve;
  Fclose;
end.