Cod sursa(job #2819250)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 18 decembrie 2021 10:29:35
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

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

struct conditie
{
    int p1,p2,d;
};

conditie v[100005];
long long n,m,a[10005];

int cmmdc(int a,int b)
{
    if (a > b)
    {
        int aux = a;
        a = b;
        b = aux;
    }
    if (a == 0)
        return b;
    return cmmdc(a,b % a);
}

int cmmmc(int a,int b)
{
    int x = cmmdc(a,b);
    return 1ll * a * b / x;
}

int main()
{
    in >> n >> m;
    for (int i = 1; i <= n; i++)
        a[i] = 1;
    for (int i = 1; i <= m; i++)
    {
        int x,y,z;
        conditie cact;
        in >> x >> y >> z;
        cact.p1 = x;
        cact.p2 = y;
        cact.d = z;
        v[i] = cact;
        a[x] = cmmmc(a[x],z);
        a[y] = cmmmc(a[y],z);
    }
    bool boo = true;
    for (int i = 1; i <= m; i++)
    {
        if (cmmdc(a[v[i].p1],a[v[i].p2]) != v[i].d)
            boo = false;
    }
    if (boo == false)
        out <<-1;
    else
        for (int i = 1; i <= n; i++)
            out << a[i] << " ";
    return 0;
}