Language: C++

template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};

int main() {
    int N, M;
    cin >> N >> M;
    UnionFind<int> uf(N);
    for (int i = 0; i < M; ++i) {
        int l, r, d;
        cin >> l >> r >> d;
        --l, --r;
        if (uf.issame(l, r)) {
            int diff = uf.diff(l, r);
            if (diff != d) {
                cout << "No" << endl;
                return 0;
            }
        }
        else {
            uf.merge(l, r, d);
        }
    }
    cout << "Yes" << endl;
}

Compiler output:
a.cc:2:5: error: 'vector' does not name a type
    2 |     vector<int> par;
      |     ^~~~~~
a.cc:3:5: error: 'vector' does not name a type
    3 |     vector<int> rank;
      |     ^~~~~~
a.cc:4:5: error: 'vector' does not name a type
    4 |     vector<Abel> diff_weight;
      |     ^~~~~~
a.cc: In member function 'void UnionFind<Abel>::init(int, Abel)':
a.cc:11:9: error: 'par' was not declared in this scope
   11 |         par.resize(n); rank.resize(n); diff_weight.resize(n);
      |         ^~~
a.cc:11:24: error: 'rank' was not declared in this scope
   11 |         par.resize(n); rank.resize(n); diff_weight.resize(n);
      |                        ^~~~
a.cc:11:40: error: 'diff_weight' was not declared in this scope
   11 |         par.resize(n); rank.resize(n); diff_weight.resize(n);
      |                                        ^~~~~~~~~~~
a.cc: In member function 'int UnionFind<Abel>::root(int)':
a.cc:16:13: error: 'par' was not declared in this scope
   16 |         if (par[x] == x) {
      |             ^~~
a.cc:21:13: error: 'diff_weight' was not declared in this scope
   21 |             diff_weight[x] += diff_weight[par[x]];
      |             ^~~~~~~~~~~
a.cc: In member function 'Abel UnionFind<Abel>::weight(int)':
a.cc:28:16: error: 'diff_weight' was not declared in this scope
   28 |         return diff_weight[x];
      |                ^~~~~~~~~~~
a.cc: In member function 'bool UnionFind<Abel>::merge(int, int, Abel)':
a.cc:39:13: error: 'rank' was not declared in this scope
   39 |         if (rank[x] < rank[y]) swap(x, y), w = -w;
      |             ^~~~
a.cc:39:32: error: there are no arguments to 'swap' that depend on a template parameter, so a declaration of 'swap' must be available [-fpermissive]
   39 |         if (rank[x] < rank[y]) swap(x, y), w = -w;
      |                                ^~~~
a.cc:39:32: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:40:13: error: 'rank' was not declared in this scope
   40 |         if (rank[x] == rank[y]) ++rank[x];
      |             ^~~~
a.cc:41:9: error: 'par' was not declared in this scope
   41 |         par[y] = x;
      |         ^~~
a.cc: In function 'int main()':
a.cc:53:5: error: 'cin' was not declared in this scope
   53 |     cin >> N >> M;
      |     ^~~
a.cc:62:17: error: 'cout' was not declared in this scope
   62 |                 cout << "No" << endl;
      |                 ^~~~
a.cc:62:33: error: 'endl' was not declared in this scope
   62 |                 cout << "No" << endl;
      |                                 ^~~~
a.cc:70:5: error: 'cout' was not declared in this scope
   70 |     cout << "Yes" << endl;
      |     ^~~~
a.cc:70:22: error: 'endl' was not declared in this scope
   70 |     cout << "Yes" << endl;
      |                      ^~~~
a.cc: In instantiation of 'bool UnionFind<Abel>::merge(int, int, Abel) [with Abel = int]':
a.cc:67:21:   required from here
   67 |             uf.merge(l, r, d);
      |             ~~~~~~~~^~~~~~~~~
a.cc:39:36: error: 'swap' was not declared in this scope
   39 |         if (rank[x] < rank[y]) swap(x, y), w = -w;
      |                                ~~~~^~~~~~
a.cc:42:9: error: invalid use of member function 'Abel UnionFind<Abel>::weight(int) [with Abel = int]' (did you forget the '()' ?)
   42 |         weight[y] = w;
      |         ^~~~~~