Cod sursa(job #1831028)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 17 decembrie 2016 12:55:10
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include<fstream>
#include<algorithm>
using namespace std;
int i,j,d,h,n,m,ok;
long long v[10010];
pair<pair<int,int>,int>u[100010];
int cmmdc(int a,int b)
{
    int r;
    while(b>0)
    {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}
int main()
{
    ifstream f("oz.in");
    ofstream g("oz.out");
    f>>n>>m;
    for(i=1;i<=n;i++)
      v[i]=1;
    for(h=1;h<=m;h++)
    {
        f>>i>>j>>d;
        v[i]*=d/cmmdc(v[i],d);
        v[j]*=d/cmmdc(v[j],d);
        u[h].first.first=i;
        u[h].first.second=j;
        u[h].second=d;
    }
    ok=0;
    for(h=1;h<=m;h++)
    {
        if(cmmdc(v[u[h].first.first],v[u[h].first.second])!=u[h].second)
        {
            g<<"-1";
            ok=1;
            break;
        }
    }
    if(ok==0)
      for(i=1;i<=n;i++)
        g<<v[i]<<" ";
    f.close(); g.close();
    return 0;
}