Cod sursa(job #2660351)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 18 octombrie 2020 23:37:35
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

struct query {
    int i, j, d;
};

int32_t main() {
    fin.sync_with_stdio(false);
    fout.sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);
    int N, M;
    fin >> N >> M;
    vector < int > a(N + 1, 1);
    vector < query > Q(M);
    for(query& q : Q) {
        fin >> q.i >> q.j >> q.d;
        int gcd1 = __gcd(a[q.i], q.d), gcd2 = __gcd(a[q.j], q.d);
        a[q.i] = a[q.i] * q.d / gcd1;
        a[q.j] = a[q.j] * q.d / gcd2;
    }
    bool flag = true;
    for(query q : Q)
        if(__gcd(a[q.i], a[q.j]) != q.d)
            flag = false;
    if(flag)
        for(int i = 1; i <= N; ++i)
            fout << a[i] << ' ';
    else
        fout << -1;
}