Cod sursa(job #3308904)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 29 august 2025 21:23:53
Problema Oz Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

void solve()
{
    int n, m;
    fin >> n >> m;
    vector<int> a(n, -1);
    vector<tuple<int, int, int>> tuples(m);
    for (auto& [x, y, z] : tuples) {
        fin >> x >> y >> z;
        --x, --y;
        if (a[x] == -1) {
            a[x] = z;
        }
        if (a[y] == -1) {
            a[y] = z;
        }
        a[x] = a[x] * z / __gcd(a[x], z);
        a[y] = a[y] * z / __gcd(a[y], z);
    }
    for (auto& [x, y, z] : tuples) {
        if (__gcd(a[x], a[y]) != z) {
            fout << -1 << '\n';
            return;
        }
    }
    for (auto val : a) {
        if (val == -1) {
            val = 1;
        }
        fout << val << ' ';
    }
    fout << '\n';

    return;
}

int main()
{
    usain_bolt();

    int tt;

    tt = 1;
    while (tt--) {
        solve();
    }
    return 0;
}