Cod sursa(job #3172991)

Utilizator AndreiDragosDavidDragos Andrei David AndreiDragosDavid Data 21 noiembrie 2023 17:44:29
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

int n,m;
vector<pair<pair<int,int>, int>> t;
// pair<int, int> (i,j) si int (d)

int main()
{
    in.tie(0);
    ios_base::sync_with_stdio(0);

    in >> n >> m;
    vector<ll> v(n, 1);
    t.resize(m);

    for (int i=0; i<m; ++i) {
        int x, y, d;
        in >> x >> y >> d;
        x--; y--;

        t[i] = {{x, y}, d};
        v[x] = v[x] * d / gcd(v[x], d);
        v[y] = v[y] * d / gcd(v[y], d);
    }

    for (auto& it : t) {
        int x = it.first.first;
        int y = it.first.second;
        int d = it.second;

        if (gcd(v[x], v[y]) != d) {
            out << -1;
            return 0;
        }
    }

    for(const auto& it : v)
        out << it << " ";


    return 0;
}