Cod sursa(job #3366733)

Utilizator TimofeiFilipTimofei Filip Emanuel TimofeiFilip Data 3 octombrie 2026 19:54:14
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<bits/stdc++.h>
using namespace std;

const int NMAX = 1e4 + 10;

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

typedef unsigned long long int ll;
const ll MAX_VALUE = 2000000000;

struct Update {
    int left, right, divisor;
};

vector<Update> up;

ll v[NMAX];

bool update(int position, ll divisor) {
    ll g = __gcd(v[position], divisor);
    ll next_val = v[position] / g;

    if (next_val > MAX_VALUE / divisor) {
        return false;
    }

    v[position] = next_val * divisor;
    return true;;
}
bool verify() {
    for (auto u : up) {
        if (__gcd(v[u.left], v[u.right]) != u.divisor) return false;
    }
    return true;
}
int main() {
    int n, m; fin >> n >> m;

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

    for (; m > 0; m--) {
        int i, j, d; fin >> i >> j >> d;
        if (!update(i, d) || !update(j, d)) {
            fout << -1;
            return 0;
        }
        up.push_back({i, j, d});
    }
    if (!verify()) {
        fout << -1 << '\n';
        return 0;
    }
    for (int i = 1; i <= n; i++)
        fout << v[i] << ' ';
    return 0;
}