Cod sursa(job #308650)

Utilizator kolapsysPostelnicu Dan Marian kolapsys Data 28 aprilie 2009 01:22:41
Problema Oz Scor 100
Compilator fpc Status done
Runda tot Marime 1.39 kb
{ http://infoarena.ro/problema/oz }
type vector=array[1..10000] of int64;
     matrice=array[1..100000,1..3] of int64;
var f,g:text;
    a:matrice;
    v:vector;
    i,n,m:longint;
    j:shortint;
    q:boolean;
{ ----- Functie CMMDC ----- }
function cmmdc(a,b:int64):int64;
var r:int64;
begin
        repeat
           r:=a mod b;
           a:=b;
           b:=r;
        until r=0;
        cmmdc:=a;
end;
{ ----- Sfarsit functie ----- }
{ ----- Functie CMMMC ----- }
function cmmmc(a,b:int64):int64;
begin
    cmmmc:=a*b div cmmdc(a,b);
end;
{ ----- Sfarsit functie ----- }
BEGIN
   assign(f,'oz.in'); reset(f);
   assign(g,'oz.out'); rewrite(g);
{ ----- Citirea matricei si initializarea vectorului ----- }
   readln(f,n,m);
   for i:=1 to n do v[i]:=1;
   for i:=1 to m do
        for j:=1 to 3 do
                begin
                read(f,a[i,j]);
                if j=3 then begin
                            v[a[i,1]]:=cmmmc(v[a[i,1]],a[i,3]);
                            v[a[i,2]]:=cmmmc(v[a[i,2]],a[i,3]);
                            end;
                end;
{ ----- Sfarsit citire matrice + vector ----- }
   q:=true;
   for i:=1 to n do if v[i]>2000000000 then q:=false;
   for i:=1 to m do
       if cmmdc(v[a[i,1]],v[a[i,2]]) <> a[i,3] then q:=false;
   if q then for i:=1 to n do write(g,v[i],' ')
        else write(g,-1);
   close(f); close(g);
END.