Cod sursa(job #473676)

Utilizator crawlerPuni Andrei Paul crawler Data 31 iulie 2010 00:16:12
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>

using namespace std;

long long a[10100];
int n,m;

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int qi[100100];
int qj[100100];
int qk[100100];
int qn;

int main() {
  ifstream fin("oz.in");
  ofstream fout("oz.out");
  
  fin >> n >> m;
  
  for (int i = 1; i <= n; ++i)
    a[i] = 1;
  
  while (m--) {
    int i,j,k;
    fin >> i >> j >> k;
    ++qn;
    qi[qn] = i;
    qj[qn] = j;
    qk[qn] = k;
    #define ll long long
    a[i] = a[i]*(ll)k / (ll)gcd(a[i], k);
    a[j] = a[j]*(ll)k / (ll)gcd(a[j], k);

    if (gcd(a[i], a[j]) != k) {
      fout << "-1\n";
      fout.close();
      return 0;
    }

    if (a[i] > 2000000000) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
    if (a[j] > 2000000000) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
  }
  m = qn;
  while (m) {
    int i,j,k;
    i = qi[m];
    j = qj[m];
    k = qk[m];
    
    a[i] = a[i]*(ll)k / (ll)gcd(a[i], k);
    a[j] = a[j]*(ll)k / (ll)gcd(a[j], k);

    if (gcd(a[i], a[j]) != k) {
      fout << "-1\n";
      fout.close();
      return 0;
    }

    if (a[i] > 2000000000) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
    if (a[j] > 2000000000) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
    
    if (a[i] < 0 || a[j] < 0) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
    --m;
  }

  for (int i = 1; i <= n; ++i) {
    if (a[i] > 2000000000) {
      fout << "-1\n";
      fout.close();
      return 0;
    }
  }

  for (int i = 1; i < n; ++i)
    fout << a[i] << " ";
  fout << a[n] << "\n";
  fout.close();
  
  return 0;
}