Cod sursa(job #1571274)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 17 ianuarie 2016 18:28:24
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("oz.in");
ofstream fout("oz.out");

const int NMax = 1e4 + 5;
const int MMax = 1e5 + 5;
const int LIM = 2e9;

struct Oz{
    int i, j, d;
};

Oz A[MMax];
int v[NMax];

inline int Cmmdc(int x, int y){
    int r;
    while(y){
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}

inline bool Update(const int &a, const int &f){
    int last = v[a];
    int value = Cmmdc(v[a], f);
    v[a] = last * (f / value);
    if(v[a] / (f / value) != last || v[a] > LIM) return false;
    return true;
}

int main(){
    int n, m, a, b, c, f;
    fin >> n >> m;
    for(int i = 1; i <= n; i++) v[i] = 1;
    for(int i = 1; i <= m; i++){
        fin >> a >> b >> c;
        A[i] = {a, b, c};
        if(!Update(a, c) || !Update(b, c)){
            fout << -1;
            return 0;
        }
    }
    for(int i = 1; i <= m; i++){
        a = A[i].i; b = A[i].j; c = A[i].d;
        if(Cmmdc(v[a], v[b]) != c){
            fout << -1;
            return 0;
        }
    }
    for(int i = 1; i <= n; i++) fout << v[i] << " ";
    return 0;
}