Cod sursa(job #166175)

Utilizator kolapsysPostelnicu Dan Marian kolapsys Data 27 martie 2008 16:45:46
Problema Oz Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.93 kb
type vector=array[1..10000] of longint;
     matrice=array[1..100000,1..3] of longint;
var f,g:text;
    i,j,m,n,p,k,aux1,aux2:longint;
    a:matrice;
    v:vector;
    b,c,ok:boolean;
{ ----- Functie CMMDC ----- }
function cmmdc(a,b:longint):longint;
var r:longint;
begin
        repeat
           r:=a mod b;
           a:=b;
           b:=r;
        until r=0;
        cmmdc:=a;
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);
   p:=1;
   for i:=1 to m do
        for j:=1 to 3 do
                begin
                read(f,a[i,j]);
                if j=3 then begin
                            if a[i,3]>v[a[i,1]] then v[a[i,1]]:=a[i,3];
                            if a[i,3]>v[a[i,2]] then v[a[i,2]]:=a[i,3];
                            p:=p*a[i,3];
                            end;
                end;
{ ----- Sfarsit citire matrice + vector ----- }
   c:=true;
   repeat
        b:=true;
        for i:=1 to m do
                begin
                aux1:=v[a[i,1]];
                aux2:=v[a[i,2]];
                k:=2; ok:=false;
                while ok=false do
                        begin
                        ok:=true;
                        if not(cmmdc(v[a[i,1]],v[a[i,2]])=a[i,3]) then
                                if v[a[i,1]] mod a[i,3]=0 then if k * aux2 > p then c:=false
                                                                            else begin
                                                                                 v[a[i,2]]:= k*aux2;
                                                                                 if k=2 then k:=3
                                                                                        else k:=k+2;
                                                                                 b:=false; ok:=false;
                                                                                 end
                                                  else if k * aux1 > p then c:=false
                                                                            else begin
                                                                                 v[a[i,1]]:= k*aux1;
                                                                                 if k=2 then k:=3
                                                                                        else k:=k+2;
                                                                                 b:=false; ok:=false;
                                                                                 end;
                        end;
               end;
   until b or not(c);
   for i:=1 to n do if v[i]>2000000000 then b:=false;
   if c and b then for i:=1 to n do write(g,v[i],' ')
              else write(g,-1);
   close(f); close(g);
END.