Cod sursa(job #186909)
#include <iostream>
#include <fstream>
#define LL long long
using namespace std;
LL cmmdc(int a,int b){
if (b == 0) return a;
return cmmdc(b,a%b);
}
LL A[10000];
LL X[100000],Y[10000],Z[10000];
LL N,M;
int main(void){
ifstream in("oz.in");
ofstream out("oz.out");
LL a,b,c;
in >> N >> M;
for (int i=0;i<N;i++)
A[i] = 1;
for (int i=0;i<M;i++){
in >> a >> b >> c;
a--;
b--;
X[i] = a;
Y[i] = b;
Z[i] = c;
LL k;
k = (A[a]*c)/cmmdc(A[a],c);
A[a] = k;
k = (A[b]*c)/cmmdc(A[b],c);
A[b] = k;
}
for (int i=0;i<M;i++)
if ((cmmdc(A[X[i]],A[Y[i]]) != Z[i]) ||
(A[X[i]] >2000000) || (A[Y[i]] >2000000))
{
out << -1;
in.close();
out.close();
return 0;
}
for (int i=0;i<N;i++){
out << A[i] << " ";
}
in.close();
out.close();
return 0;
}