Cod sursa(job #169141)

Utilizator victor_bla_blaDumitrescu Victor victor_bla_bla Data 1 aprilie 2008 11:31:39
Problema Oz Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.86 kb
program oz;
var n,x,y: integer;
m,i,d: longint;
a: array[1..100000] of longint;
f: text;
t: 0..1;
function cmmdc(x,y: longint):longint;
begin
     if y=0 then cmmdc:=x else
     cmmdc:=cmmdc(y, x mod y);
end;
function at(k,h:longint):longint;
begin
     if k=1 then at:=h else
     if k mod h=0 then at:=k else
     at:=k*h div cmmdc(k,h);
end;
begin
     t:=1;
     assign(f,'oz.in');
     reset(f);
     readln(f,n,m);
     for i:=1 to n do
         a[i]:=1;
     for i:=1 to m do
     begin
         readln(f,x,y,d);
         if d<>1 then begin
         a[x]:=at(a[x],d);
         a[y]:=at(a[y],d); end;
         if cmmdc(a[x],a[y])=1 then
         begin t:=0; break; end;
     end;
     close(f);
     assign(f,'oz.out'); rewrite(f);
     if t=0 then write(f,-1) else
     for i:=1 to n do
         write(f,a[i],' ');
     close(f);
end.