Cod sursa(job #2281752)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 12 noiembrie 2018 18:24:41
Problema Oz Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

using namespace std;
ifstream f("oz.in");
ofstream g("oz.out");
int v[10010],m,n,i,x,y,z,z1,d,x1,y1;

int main()
{
    f>>n>>m;
    for(i=1;i<=n;i++)
        v[i]=1;
    for(i=1;i<=m;i++)
    {
        f>>x1>>y1>>z;
        x=v[x1];y=v[y1];
        d=2;z1=z;
        while(x>1)
        {
            while(x%d==0)
            {
                if(z%d==0)
                    z/=d;
                x/=d;
            }
            d++;
            if(d*d>x&&x!=1)
                d=x;
        }
        d=2;
        while(y>1)
        {
            while(y%d==0)
            {
                if(z1%d==0)
                    z1/=d;
                y/=d;
            }
            d++;
            if(d*d>y&&y!=1)
                d=y;
        }
        v[x1]*=z;
        v[y1]*=z1;
    }
    for(i=1;i<=n;i++)
        g<<v[i]<<" ";
    return 0;
}