Cod sursa(job #728551)

Utilizator danalex97Dan H Alexandru danalex97 Data 28 martie 2012 19:43:34
Problema Oz Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
using namespace std;

ifstream F("oz.in");
ofstream G("oz.out");

#define Nmax 10011
#define ll long long 

int A[Nmax];
int N,M;

int cmmdc(int x,int y)
{
	for (int aux=0 ; y ; aux=y,y=x%y,x=aux );
	return x;
}

inline int cmmmc(int x,int y)
{ return int ( (ll) ( x*y ) /cmmdc(x,y) ) ; }

int main()
{
	F>>N>>M;
	for (int i=1;i<=N;++i)
		A[i]=1;
	for (int i=1;i<=M;++i)
	{
		int a,b,c; F>>a>>b>>c;
		if ( cmmdc(A[a],A[b]) > c )
			{G<<"-1\n";	F.close();	G.close();	return 0;}
		else
			A[a]=cmmmc(A[a],c),A[b]=cmmmc(A[b],c);
	}
	for (int i=1;i<=N-1;++i)
		G<<A[i]<<' ';G<<A[N]<<'\n';
	F.close();
	G.close();
	return 0;
}