Cod sursa(job #166897)

Utilizator timotei21Timotei Dolean timotei21 Data 28 martie 2008 17:01:37
Problema Oz Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include<fstream.h>
long x[10001],a1[100001],a2[100001],i,j,cmmdc1,cmmdc2[100001],n,m,aux1,aux2;
long cmmdc(long tz, long sh)
{
	long r;
	do {
		r=tz%sh;
		tz=sh;
		sh=r;
	}while (r);
	return tz;
}

int main()
{

ifstream f("oz.in");
f>>n>>m;

for (i=1;i<=m;i++)
{
	f>>a1[i]>>a2[i]>>cmmdc2[i];

	d=cmmdc2[i];
	aux1=x[a1[i]];
	aux2=x[a2[i]];

	x[a1[i]]*=d/cmmdc(x[a1[i]],d);
	x[a2[i]]*=d/cmmdc(x[a2[i]],d);
}
int ind=1;

for (i=1;i<=m;i++)
{
 if (cmmdc(x[a1[i]],x[a2[i]])!=cmmdc2[i])
 {
  ind=0;
  break;
 }
}
f.close();
ofstream g("oz.out");
if (ind)  for (i=1;i<=n;i++) g<<x[i]<<" ";
else g<<"-1";
g<<'\n';
g.close();

return 0;
}