Cod sursa(job #1982056)

Utilizator patcasrarespatcas rares danut patcasrares Data 17 mai 2017 17:20:59
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include<fstream>
using namespace std;
ifstream fin("oz.in");
ofstream fout("oz.out");
int n,m,h;
long long r[10005];
struct sdf
{
    int i,j,d;
}x[100005];
int eu(long long a,int b)
{
    if(b==0)
        return a;
    return eu(b,a%b);

}
int main()
{
    fin>>n>>m;
    for(h=1;h<=n;h++)
        r[h]=1;
    for(h=1;h<=m;h++)
    {
        fin>>x[h].i>>x[h].j>>x[h].d;
        r[x[h].i]=r[x[h].i]*(x[h].d/eu(r[x[h].i],x[h].d));
        r[x[h].j]=r[x[h].j]*(x[h].d/eu(r[x[h].j],x[h].d));
    }
    for(h=1;h<=m;h++)
        if(eu(r[x[h].i],r[x[h].j])!=x[h].d)
        {
            fout<<-1;
            return 0;
        }
    for(h=1;h<=n;h++)
        fout<<r[h]<<' ';
}