Cod sursa(job #2066497)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 15 noiembrie 2017 02:09:24
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <cstdlib>

using namespace std;

ifstream fin("oz.in");
ofstream fout("oz.out");

const int LIM = 2 * 1000 * 1000 * 1000, MAXN = 10 * 1000, MAXM = 100 * 1000;

long long ans[MAXN + 1];
int a[MAXM], b[MAXM], c[MAXM];

inline void gata() {
    fout << "-1";
    exit(0);
}

inline int cmmdc(int a,int b) {
    int r;
    while (b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main() {
    int n, m;
    fin >> n >> m;

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

    for (int i = 0; i < m; i++) {
        int x, y, z;
        fin >> x >> y >> z;

        ans[x] = 1LL * ans[x] * z / cmmdc(ans[x], z);
        if (ans[x] > LIM) gata();
        ans[y] = 1LL * ans[y] * z / cmmdc(ans[y], z);
        if (ans[x] > LIM) gata();

        a[i] = x;
        b[i] = y;
        c[i] = z;
    }

    for (int i = 0; i < m; i++)
        if (cmmdc(ans[a[i]], ans[b[i]]) != c[i])
            gata();

    for (int i = 1; i <= n; i++)
        fout << ans[i] << ' ';

    return 0;
}