fence.pas

来自「Magio牛的usaco源代码」· PAS 代码 · 共 75 行

PAS
75
字号
{
ID:maigoak1
PROG:fence
}

program fence;
const
  maxv=500;
  maxe=10000;
var
  fin,fout:text;
  adj:array[1..maxv,1..maxv]of integer;
  deg:array[1..maxv]of integer;
  s,c:array[0..maxe]of integer;
  n,min,max,x,y,t,l,i,j:integer;
procedure out;
  var
    i:integer;
  begin
    assign(fout,'fence.out');
    rewrite(fout);
    for i:=n downto 0 do
      writeln(fout,c[i]);
    close(fout);
    halt;
  end;
begin
  fillchar(adj,sizeof(adj),0);
  fillchar(deg,sizeof(deg),0);
  min:=maxv;max:=1;
  assign(fin,'fence.in');
  reset(fin);
  readln(fin,n);
  for i:=1 to n do begin
    readln(fin,x,y);
    inc(adj[x,y]);
    inc(adj[y,x]);
    inc(deg[x]);
    inc(deg[y]);
    if x<min then min:=x;
    if x>max then max:=x;
    if y<min then min:=y;
    if y>max then max:=y;
  end;
  close(fin);

  s[0]:=0;
  for i:=min to max do
    if odd(deg[i]) then begin
      s[0]:=i;
      break;
    end;
  if s[0]=0 then s[0]:=min;

  t:=0;l:=-1;
  for i:=1 to n do begin
    for j:=min to max do
      if adj[s[t],j]>0 then begin
        dec(adj[s[t],j]);
        dec(adj[j,s[t]]);
        dec(deg[s[t]]);
        dec(deg[j]);
        inc(t);
        s[t]:=j;
        break;
      end;
    while deg[s[t]]=0 do begin
      inc(l);
      c[l]:=s[t];
      if l=n then out;
      dec(t);
    end;
  end;
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?