Cod sursa(job #290544)

Utilizator mariuscris90giuroiu marius mariuscris90 Data 28 martie 2009 03:14:54
Problema Oz Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.04 kb
{$n+}
program ozz;
type vect=array[1..10000] of longint;
var  t:boolean;
     v:vect;
     f,g:text;
     i,j,m,n,x,y:longint;
     z,cmm:longint;
function cmmdc(a,b:longint):longint;
var r,c:longint;
begin
while a mod b<>0 do begin
r:=a mod b;
a:=b;
b:=r;
end;
cmmdc:=r;
end;
begin
assign(f,'oz.in');assign(g,'oz.out');reset(f);rewrite(g);
readln(f,n,m);t:=true;
for i:=1 to n do v[i]:=1;
for i:=1 to m do begin
readln(f,x,y,z);cmm:=cmmdc(v[x],z);
if cmm=1 then if v[x]*z<=2000000000 then v[x]:=v[x]*z
                                            else t:=false
                   else if v[x]*(z div cmm)<=2000000000 then v[x]:=v[x]*(z div cmm)
                   else t:=false;
cmm:=cmmdc(v[y],z);
if cmm=1 then if v[y]*z<=2000000000 then v[y]:=v[y]*z
                                            else t:=false
                   else if v[y]*(z div cmm)<=2000000000 then v[y]:=v[y]*(z div cmm)
                   else t:=false;


end;
if t then for i:=1 to n do write(g,v[i],' ')
     else write(g,-1);
close(f);close(g);end.