Cod sursa(job #2533193)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 28 ianuarie 2020 20:27:46
Problema Oz Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int M = 1e5;
const int N = 1e4;

int cmmdc(int a, int b);
int cmmmc(int a, int b);

struct ura{
    int x, y, cm;
}v[5 + M];

int sol[5 + N];

int main()
{
    freopen("oz.in", "r", stdin);
    freopen("oz.out", "w", stdout);
    int n, m;
    bool ok(true);
    scanf("%d%d", &n, &m);

    for(int i = 1; i <= n; i++) sol[i] = 1;

    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].cm);
        sol[v[i].x] = cmmmc(sol[v[i].x], v[i].cm);
        sol[v[i].y] = cmmmc(sol[v[i].y], v[i].cm);
    }

    for(int i = 1; i <= m; i++){
        if(cmmdc(sol[v[i].x], sol[v[i].y]) != v[i].cm)
            ok = false;
    }

    if(ok == false) printf("-1\n");
    else{
        for(int i = 1; i <= n; i++) printf("%d ", sol[i]);
        printf("\n");
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}

int cmmdc(int a, int b){
    int r;
    while(b){
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int cmmmc(int a, int b){
    return (1LL * a * b / cmmdc(a, b));
}