Cod sursa(job #2533941)

Utilizator Mc_TaviMacovei Octavian-Cosmin Mc_Tavi Data 29 ianuarie 2020 21:21:44
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda bruschete Marime 1 kb
#include <bits/stdc++.h>

using namespace std;
#define NMAX 10005
#define MMAX 100005
typedef long long ll;

ll euclid(ll a, ll b) {
    if(!b)
        return a;
    return euclid(b, a%b);
}
ll cmmmc(ll a, ll b) {
    return a*b/euclid(a, b);
}

struct myType {
    ll i, j, d;
};
vector<myType> date;

int main()
{
    freopen("oz.in", "r", stdin);
    freopen("oz.out", "w", stdout);

    vector<ll> sir(NMAX, 1);

    ll n, m;
    scanf("%lld%lld", &n, &m);
    for(ll k = 1; k <= m; ++k) {
        myType temp;
        scanf("%lld%lld%lld", &temp.i, &temp.j, &temp.d);
        sir[temp.i] = cmmmc(sir[temp.i], temp.d);
        sir[temp.j] = cmmmc(sir[temp.j], temp.d);
        date.push_back(temp);
    }

    for(auto it = date.begin(); it != date.end(); ++it) {
        if(euclid(sir[(*it).i], sir[(*it).j]) != (*it).d) {
            printf("-1");
            return 0;
        }
    }

    for(ll i = 1; i <= n; ++i)
        printf("%lld ", sir[i]);
    return 0;
}