Cod sursa(job #2497467)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 22 noiembrie 2019 18:52:55
Problema Oz Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

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

struct chestie
{
    int x, y, d;
} v[10005];

int cmmmc[100005];

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

    for(int i = 1; i <= n; ++i)
        cmmmc[i] = 1;
    for(int i = 1; i <= m; ++i)
    {
        fin >> v[i].x >> v[i].y >> v[i].d;

        cmmmc[v[i].x] = (cmmmc[v[i].x] * v[i].d) / __gcd(cmmmc[v[i].x], v[i].d);
        cmmmc[v[i].y] = (cmmmc[v[i].y] * v[i].d) / __gcd(cmmmc[v[i].y], v[i].d);
    }

    for(int i = 1; i <= m; ++i)
        if(__gcd(cmmmc[v[i].x], cmmmc[v[i].y]) != v[i].d)
        {
            fout << -1 << '\n';
            return 0;
        }

    for(int i = 1; i <= n; ++i)
        fout << cmmmc[i] << ' ';
    fout << '\n';
    return 0;
}