Cod sursa(job #1777724)

Utilizator giotoPopescu Ioan gioto Data 12 octombrie 2016 20:35:59
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <cstdio>
using namespace std;

long long n, m, a[10005], i1[100005], i2[100005], d[100005];
inline long long cmmdc(long long x, long long y){
    while(y > 0){
        long long r = x % y;
        x = y; y = r;
    }
    if(x == 0) return 1;
    return x;
}
int main()
{
    freopen("oz.in", "r", stdin);
    freopen("oz.out", "w", stdout);
    scanf("%lld%lld", &n, &m);

    for(int i = 1; i <= n; ++i)
        a[i] = 1;

    for(int i = 1; i <= m; ++i){
        scanf("%d%d%lld", &i1[i], &i2[i], &d[i]);

        a[i1[i]] = a[i1[i]] * d[i] / cmmdc(a[i1[i]], d[i]);
        a[i2[i]] = a[i2[i]] * d[i] / cmmdc(a[i2[i]], d[i]);
    }
    for(int i = 1; i <= m; ++i)
        if(cmmdc(a[i1[i]], a[i2[i]]) != d[i]){
            printf("-1");
            return 0;
        }
    for(int i = 1; i <= n; ++i)
        printf("%lld ", a[i]);
    return 0;
}