比赛 2008haoi模拟训练4 评测结果 AAAAAATTTA
题目名称 遗传密码 最终得分 70
用户昵称 梦里醉逍遥 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-04-24 15:26:02
显示代码纯文本
program pie;
const
  fi='pie.in';  fo='pie.out';
type
  arr1=array[1..1000] of longint;
  arr2=array[1..1000,1..1000] of longint;
var
  fin,fout:text;  num,n,sp,ans,max,m:longint;
  p,l,r,stack,stackp,ts,ps,v:arr1;  d:arr2;

  procedure init;
  var
    i,j,k:longint;
  begin
    assign(fin,fi);  reset(fin);
    readln(fin,n);
    for i:=1 to n do begin
      readln(fin,j,k);
      if j>num then num:=j;
      if k>num then num:=k;
      v[j]:=1;  v[k]:=1;
      inc(p[j]);  inc(l[j]);
      inc(r[k]);  d[j,p[j]]:=k;
    end;
    close(fin);
  end;

  procedure init_dfs;
  begin
    sp:=0;  max:=0;
  end;

  procedure push(x,y:longint);
  begin
    sp:=sp+1;
    ts[sp]:=x;
    ps[sp]:=y;
  end;

  procedure pop;
  begin
    sp:=sp-1;
  end;

  procedure dfs(x,y:longint);
  var
    i,j:longint;
  begin
    if l[x]=0 then begin
      if y>max then begin
        max:=y;
        for i:=1 to y do begin
          stack[i]:=ts[i];
          stackp[i]:=ps[i];
        end;
      end;
      exit;
    end;
    for i:=1 to p[x] do
      if d[x,i]<>0 then begin
        dec(l[x]);  j:=d[x,i];
        dec(r[j]);  push(j,i);
        d[x,i]:=0;  dfs(j,y+1);
        pop;  inc(l[x]);
        inc(r[j]);  d[x,i]:=j;
      end;
  end;

  procedure makeans;
  var
    i:longint;
  begin
    for i:=2 to max do begin
      d[stack[i-1],stackp[i]]:=0;
      dec(l[stack[i-1]]);
      dec(r[stack[i]]);
      if stackp[i]=p[stack[i-1]] then
        dec(p[stack[i-1]]);
    end;
    ans:=ans+max;
    m:=m+max-1;
  end;

  procedure main;
  var
    i:longint;
  begin
    for i:=1 to num do
      if (r[i]=0) and (v[i]=1) and (l[i]<>0) then begin
        init_dfs;
        push(i,1);
        dfs(i,1);
        pop;
        makeans;
      end;
    while m<>n do begin
      init_dfs;
      for i:=1 to num do
        if l[i]<>0 then begin
          sp:=0;  push(i,1);
          dfs(i,1);  pop;
        end;
      makeans;
    end;
  end;

  procedure print;
  begin
    assign(fout,fo);  rewrite(fout);
    writeln(fout,ans);  close(fout);
  end;

begin
  init;
  main;
  print;
end.