Cod sursa(job #2034329)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 7 octombrie 2017 18:39:06
Problema Oz Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("oz.in");
ofstream g("oz.out");
long long n,v[10002];
int m,a,b,d;
long long lcm(int a,int b)
{
    int x1=a;
    int x2=b;
    int c;
    while(x2)
    {
        c=x1%x2;
        x1=x2;
        x2=c;
    }
    long long prd=a*b/x1;
    return prd;
}
int main()
{
    f>>n>>m;
    for(int i=1;i<=m;++i)
    {
        f>>a>>b>>d;
        if(v[a]==0)
            v[a]=d;
        else
            v[a]=lcm(v[a],d);
        if(v[b]==0)
            v[b]=d;
        else
            v[b]=lcm(v[b],d);
    }
    for(int i=1;i<=n;++i)
        if(v[i]==0){
            g<<-1;return 0;
        }
    for(int i=1;i<=n;++i)
        g<<v[i]<<" ";
}