Cod sursa(job #772682)
Program ciclueuler;
type lista=^celula;
celula=record
nod,c:longint;
next:lista;
end;
var g:array [1..100001] of lista;
v:lista;
b1,b2:array [1..1 shl 17] of char;
aux:array [1..100001] of longint;
i,j,n,m,x,y,nr:longint;
ok:boolean;
b:array [1..100001] of longint;
fi,fo:text;
procedure introdu(x,y:longint);
begin
v:=g[x];
while v<>nil do begin
if v^.nod=y then begin inc(v^.c); break; end;
v:=v^.next;
end;
if v=nil then begin new(v); v^.nod:=y; v^.c:=1; v^.next:=g[x]; g[x]:=v; end;
end;
procedure dfs(nod,prec:longint);
var p,v:lista;
begin
p:=g[nod]; if nr<m then write(fo,nod,' ');
v:=g[nod]; while v<>nil do begin if v^.nod=prec then begin dec(v^.c); break; end; v:=v^.next; end;
while p<>nil do begin
if p^.c>0 then begin dec(p^.c); inc(nr); dfs(p^.nod,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); ok:=true;
for i:=1 to m do begin
readln(fi,x,y);
if x<>y then begin
introdu(x,y);
introdu(y,x);
inc(b[x]); inc(b[y]);
end
else begin inc(nr); aux[nr]:=x; end;
end;
for i:=1 to nr do begin introdu(aux[nr],aux[nr]); b[aux[nr]]:=b[aux[nr]]+2; end;
nr:=0;
for i:=1 to n do
if b[i] mod 2=1 then ok:=false;
if ok then dfs(1,0) else write(fo,'-1');
close(fo);
end.