Cod sursa(job #772711)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 30 iulie 2012 16:08:36
Problema Ciclu Eulerian Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 1.21 kb
Program ciclueuler;
 type lista=^celula;
      celula=record
       nod,pos:longint;
       next:lista;
       end;
var g:array [1..100001] of lista;
    much:array [1..500001] of boolean;
    b:array [1..500001] of longint;
    v:lista;
    b1,b2:array [1..1 shl 17] of char;
    ok:boolean;
    s:string;
    i,n,m,x,y,j,nr:longint;
    fi,fo:text;
procedure dfs(nod:longint);
 var p:lista;
begin
 p:=g[nod];
  while p<>nil do begin
   if much[p^.pos]=false then begin much[p^.pos]:=true; dfs(p^.nod); inc(nr); b[nr]:=nod; end;
    p:=p^.next;
   end;
end;
begin
 assign(fi,'ciclueuler.in');
  assign(fo,'ciclueuler.out');
 settextbuf(fi,b1); settextbuf(fo,b2);
 reset(fi); rewrite(fo); readln(fi,n,m);
  for i:=1 to m do begin
   readln(fi,s); x:=0; y:=0;
    for j:=1 to length(s) do begin x:=x*10+ord(s[j])-48; if s[j+1]=' ' then break; end;
    for j:=j+2 to length(s) do y:=y*10+ord(s[j])-48;
   inc(b[x]); inc(b[y]);
    new(v); v^.nod:=y; v^.pos:=i; v^.next:=g[x]; g[x]:=v;
    new(v); v^.nod:=x; v^.pos:=i; v^.next:=g[y]; g[y]:=v;
   end;
  for i:=1 to n do if b[i] mod 2=1 then ok:=true;
   if ok then write(fo,'-1') else dfs(1);
    for i:=1 to nr do write(fo,b[i],' ');
  close(fo);
end.