Cod sursa(job #3209803)

Utilizator YuzukyIstrate Andreea Ruxandra Yuzuky Data 3 martie 2024 16:02:59
Problema Oz Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("oz.in");
ofstream out("oz.out");
const int MAX = 10000;
int v[MAX+1];
int main()
{
    int n,m;
    in>>n>>m;
    for(int i=0; i<m; ++i)
    {
        int x,y,d;
        in>>x>>y>>d;
        if(v[x]==0)
            v[x]=d;
        if(v[y]==0)
            v[y]=d;
        else if(v[x]!=0 && v[y]!=0)
        {
            int cop1=v[x], cop2=d, r;
            while(cop2!=0)
            {
                r=cop1%cop2;
                cop1=cop2;
                cop2=r;
            }
            v[x]=v[x]*(d/cop1);
            cop1=v[y], cop2=d;
            while(cop2!=0)
            {
                r=cop1%cop2;
                cop1=cop2;
                cop2=r;
            }
            v[y]=v[y]*(d/cop1);
        }
    }
    int ok=1;
    for(int i=1; i<=n; ++i)
    {
        if(v[i]==0)
            ok=0;
    }
    if(ok==0)
        out<<-1;
    else
        for(int i=1; i<=n; ++i)
            out<<v[i]<<" ";
    return 0;
}