Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class Weighted_UnionFind{
    vector<int> par,sz;
    vector<T> wd;
    int V;
    Weighted_UnionFind(int node_size){
        V = node_size;
        par.resize(V),wd.resize(V,0),sz.resize(V,1);
        rep(i,V){
            par[i] = i;
        }
    }
    int find(int x){
        if(par[x] == x){
            return x;
        }else{
            int parent = find(par[x]);
            wd[x] += wd[par[x]];
            return wd[x] = parent;
        }
    }
    T weight(int x){
        find(x);
        return diff[x];
    }
    void unite(int x,int y,T w){
        w += weight(x), w -= weight(y);
        x = find(x),y = find(y);
        if(x == y) return;
    	if(sz[x] < sz[y]){
            swap(x,y);
            w = -w;
        }
        sz[x] += sz[y];
        par[y] = x;
        wd[y] = w;
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
    T diff(int x,int y){
        return weight(x)-weight(y);
    }
};


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    Weighted_UnionFind<int> wu(n);
    rep(i,m){
        int l,r,d;
        cin >> l >> r >> d;
        --l,--r;
        if(wu.same(l,r)){
            if(wu.diff(l,r) != d){
                cout << "No\n";
                return 0;
            }
        }else{
            wu.unite(l,r,d);
        }
    }
    cout << "Yes\n";
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:89:33: error: 'Weighted_UnionFind<T>::Weighted_UnionFind(int) [with T = int]' is private within this context
   89 |     Weighted_UnionFind<int> wu(n);
      |                                 ^
a.cc:42:5: note: declared private here
   42 |     Weighted_UnionFind(int node_size){
      |     ^~~~~~~~~~~~~~~~~~
a.cc:94:19: error: 'bool Weighted_UnionFind<T>::same(int, int) [with T = int]' is private within this context
   94 |         if(wu.same(l,r)){
      |            ~~~~~~~^~~~~
a.cc:74:10: note: declared private here
   74 |     bool same(int x,int y){
      |          ^~~~
a.cc:95:23: error: 'T Weighted_UnionFind<T>::diff(int, int) [with T = int]' is private within this context
   95 |             if(wu.diff(l,r) != d){
      |                ~~~~~~~^~~~~
a.cc:77:7: note: declared private here
   77 |     T diff(int x,int y){
      |       ^~~~
a.cc:100:21: error: 'void Weighted_UnionFind<T>::unite(int, int, T) [with T = int]' is private within this context
  100 |             wu.unite(l,r,d);
      |             ~~~~~~~~^~~~~~~
a.cc:62:10: note: declared private here
   62 |     void unite(int x,int y,T w){
      |          ^~~~~
a.cc: In instantiation of 'T Weighted_UnionFind<T>::weight(int) [with T = int]':
a.cc:78:16:   required from 'T Weighted_UnionFind<T>::diff(int, int) [with T = int]'
   78 |         return weight(x)-weight(y);
      |                ^~~~~~
a.cc:95:23:   required from here
   95 |             if(wu.diff(l,r) != d){
      |                ~~~~~~~^~~~~
a.cc:60:16: error: invalid use of member function 'T Weighted_UnionFind<T>::diff(int, int) [with T = int]' (did you forget the '()' ?)
   60 |         return diff[x];
      |                ^~~~