Cod sursa(job #391795)

Utilizator philipPhilip philip Data 6 februarie 2010 12:42:42
Problema Ciclu Eulerian Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.56 kb
type pedge=^edge;
     edge=record
       v:longint;
       next:pedge;
     end;

var i,x,y,n,m,u:longint;
    e:array[0..500005] of pedge;
    c:array[0..500005] of longint;
    grad:array[0..100005] of longint;
    nou,p,pp:pedge;

procedure citire;
  begin
    assign(input,'ciclueuler.in');
    reset(input);
    assign(output,'ciclueuler.out');
    rewrite(output);
    readln(n,m);
    for i:=1 to m do begin
      readln(x,y);
      new(nou);
      nou^.v:=y;
      nou^.next:=e[x];
      e[x]:=nou;
      new(nou);
      nou^.v:=x;
      nou^.next:=e[y];
      e[y]:=nou;
      inc(grad[x]);
      inc(grad[y]);
    end;
  end;


function eulerian:boolean;
  begin
    for i:=1 to n do
      if (grad[i] mod 2=1) or (grad[i]=0) then begin
        writeln(-1);
        close(output);
        halt;
      end;
    eulerian:=true;
  end;

procedure dfs;
  begin
    u:=1;
    c[u]:=1;
    while u>0 do begin
      x:=c[u];
      if e[x]<>nil then begin
        inc(u);
        c[u]:=e[x]^.v;

        y:=e[x]^.v;
        p:=e[y];
        if p^.v=x then begin
          e[y]:=e[y]^.next;
          dispose(p);
        end else begin
          while p^.next^.v<>x do p:=p^.next;
          pp:=p^.next;
          p^.next:=pp^.next;
          dispose(pp);
        end;


        p:=e[x];
        e[x]:=e[x]^.next;
        dispose(p);
      end else begin
        write(c[u],' ');
        dec(u);
      end;
    end;
  end;

begin
  citire;
  if eulerian then dfs;



  close(input);
  close(output);
end.