Cod sursa(job #2924251)

Utilizator TheEpicWipedCreaVlad Chirita Alexandru TheEpicWipedCrea Data 27 septembrie 2022 21:41:26
Problema Oz Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in  ("oz.in");
ofstream out("oz.out");

#define MAX_N 10000
#define MAX_M 100000

struct ura{
    int a,b,d;
}w[MAX_M];
long long v[MAX_N+1];

inline int cmmdc(int a,int b){
    if(b==0){
        return a;
    }
    return cmmdc(b,a%b);
}
inline int cmmmc(int a,int b){
    return 1LL*a*b/cmmdc(a,b);
}

int main(){
    int n,m;
    in>>n>>m;
    for(int i=1;i<=n;i++){
        v[i]=1;
    }
    for(int i=1;i<=m;i++){
        int a,b,d;
        in>>a>>b>>d;
        w[i].a=a;
        w[i].b=b;
        w[i].d=d;
        v[a]=cmmmc(v[a],d);
        v[b]=cmmmc(v[b],d);
    }

    bool ok=true;
    for(int i=1;i<=m;i++){
        if( cmmdc( v[ w[i].a ] , v[ w[i].b ]) != w[i].d ){
            ok=false;
        }
    }

    if(ok==false){
        out<<-1;
    }
    else{
        for(int i=1;i<=n;i++){
            out<<v[i]<<" ";
        }
    }
}