Pagini recente » Cod sursa (job #1791495) | Cod sursa (job #2945691) | Cod sursa (job #1343913) | Cod sursa (job #41656) | Cod sursa (job #166170)
Cod sursa(job #166170)
type vector=array[1..10000] of longint;
vector2=array[1..2000000] of boolean;
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;
d:vector2;
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 ----- }
{ ----- Functie Ciur ----- }
procedure ciur(var a:vector2;n:longint);
var i,j:longint;
begin
a[1]:=true;
i:=2;
while i<=trunc(sqrt(n)) do
begin
j:=sqr(i);
while j<=n do
begin
a[j]:=true;
j:=j+i;
end;
if i=2 then i:=3
else i:=i+2;
end;
end;
{ ----- Sfarsit functie Ciur ----- }
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 ----- }
ciur(d,p);
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 repeat k:=k+2; until d[k]=false;
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 repeat k:=k+2; until d[k]=false;
b:=false; ok:=false;
end;
end;
end;
until b or not(c);
if c then for i:=1 to n do write(g,v[i],' ')
else write(g,-1);
close(f); close(g);
END.