Cod sursa(job #2658127)

Utilizator andreibazavanAndrei Bazavan andreibazavan Data 13 octombrie 2020 12:23:00
Problema Oz Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("oz.in");
ofstream fout("oz.out");
long long n,z,m,a[10001];
struct t{
 long x,y,d;
}v[100001];

long long CMMDC(int a,int b)
{
    int r;
    while(b)
    {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}

int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)a[i]++;
    for(int i=1;i<=m;i++)
    {
        fin>>v[i].x>>v[i].y>>v[i].d;
        a[v[i].x]=(a[v[i].x]*v[i].d)/CMMDC(a[v[i].x],v[i].d);
        a[v[i].y]=(a[v[i].y]*v[i].d)/CMMDC(a[v[i].y],v[i].d);
    }
    bool ok=1;
    for(int i=1;i<=m;i++)
    {
        if(CMMDC(a[v[i].x],a[v[i].y])!=v[i].d){ok=0;break;}
    }
    if(ok)
    {
        for(int i=1;i<=n;i++)
            fout<<a[i]<<" ";
        fout<<'\n';
    }
    else fout<<'-1'<<'\n';
    return 0;
}