Cod sursa(job #2001061)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 15 iulie 2017 16:35:47
Problema Oz Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n,x[10002],ii,i,m,j,d,ok=1;
int cmmdc(int a, int b)
{
    int r;
    while(b)
    {
        r=b;
        b=a%b;
        a=r;
    }
    return a;
}
int main()
{
    ifstream fin ("oz.in");
    ofstream fout ("oz.out");
    fin>>n>>m;
    fill(x+1,x+n+1,1);
    for(ii=1;ii<=m;ii++)
    {
        fin>>i>>j>>d;
        x[i]*=d;x[j]*=d;
        if(cmmdc(x[i],x[j])!=d){fout<<x[i]<<" "<<x[j]<<" "<<d<<"\n";ok=0;break;}
    }
    if(ok)for(i=1;i<=n;i++)fout<<x[i]<<" ";
    else fout<<"-1";
    return 0;
}