Cod sursa(job #3130001)

Utilizator profinfo114Prof Info profinfo114 Data 16 mai 2023 16:29:43
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<fstream>
using namespace std;

typedef unsigned long long int64;

void read();
void write();
int64 cmmdc( int64 a, int64 b )
{
	if ( b == 0 )
		return a;
	return cmmdc( b, a % b );
}
int64 cmmmc( int64 a, int64 b )
{
	return a * b / cmmdc( a, b );
}

int64 n, m;
int64 a[10001];

struct tri
{
	int64 i, j, d;
};
tri his[100001];
bool isPos = true;

int main()
{
	read();
	write();
	return 0;
}

void read()
{
	ifstream fin( "oz.in" );
	fin >> n >> m;
	int64 i, j, d;
	for ( int64 x = 0; x < m; ++x )
	{
		fin >> i >> j >> d;
		his[x].i = i, his[x].j = j, his[x].d = d;
		if ( a[i] != 0 )
			a[i] = cmmmc( a[i], d );
		else
			a[i] = d;
		if ( a[j] != 0 )
			a[j] = cmmmc( a[j], d );
		else
			a[j] = d;
	}
	for ( int64 x = 0; x < m; ++x )
	{
		if ( cmmdc( a[his[x].i], a[his[x].j] ) != his[x].d )
			isPos = false;
	}
	fin.close();
}

void write()
{
	ofstream fout( "oz.out" );
	if ( isPos )
		for ( int64 i = 1; i <= n; ++i )
		{
			if ( a[i] == 0 )
				a[i] = 1;
			fout << a[i] << ' ';
		}
	else
		fout << -1;
	fout << '\n';
	fout.close();
}