Language: C++

#include <cassert>
#include <vector>

template <class Group> class potentialized_union_find {
public:
  using T = typename Group::value_type;

private:
  using size_t = std::size_t;
  class node_type {
    friend potentialized_union_find;

    T value;
    size_t parent;
    size_t size;

    node_type(const T value, const size_t parent, const size_t size)
        : value(value), parent(parent), size(size) {}
  };

  std::vector<node_type> tree;

  size_t size() const { return tree.size(); }

public:
  potentialized_union_find() = default;
  explicit potentialized_union_find(const size_t n)
      : tree(n, node_type(Group::identity, n, 1)) {}

  size_t find(const size_t x) {
    assert(x < size());
    compress(x);
    return tree[x].parent;
  }
  T potential(const size_t x) {
    assert(x < size());
    compress(x);
    return tree[x].value;
  }
  bool same(const size_t x, const size_t y) {
    assert(x < size());
    compress(x);
    return find(x) == find(y);
  }
  T distance(const size_t x, const size_t y) {
    assert(x < size());
    assert(y < size());
    return Group::operation(Group::inverse(potential(x)), potential(y));
  }
  size_t size(const size_t x) {
    assert(x < size());
    return tree[find(x)].size();
  }

  void unite(size_t x, size_t y, const T d) {
    if (same(x, y))
      return;
    if (size(x) < size(y)) {
      d = Group::inverse(d);
      std::swap(x, y);
    }
    d = Group::operation(Group::operation(potential(x), d),
                         Group::inverse(potential(y)));
    x = find(x);
    y = find(y);
    tree[x].size += tree[y].size;
    tree[y].parent = x;
    tree[y].value = d;
  }
};
template <class T> class plus_group {
public:
  using value_type = T;
  static constexpr T operation(const T &x, const T &y) noexcept {
    return x + y;
  }
  static constexpr T identity = 0;
  static constexpr T inverse(const T &x) noexcept { return -x; }
};
template <class T> constexpr T plus_group<T>::identity;

#include <iostream>

int main() {
  int n, m;
  std::cin >> n >> m;

  potentialized_union_find<plus_group<int>> puf(n);

  for (int i = 0; i != m; ++i) {
    int l, r, d;
    std::cin >> l >> r >> d;
    --l;
    --r;
    if (puf.same(l, r)) {
      if (puf.distance(l, r) != d) {
        std::cout << "No" << std::endl;
        return 0;
      }
    } else {
      puf.unite(l, r, d);
    }
  }
  std::cout << "Yes" << std::endl;
}

Compiler output:
a.cc: In member function 'potentialized_union_find<Group>::size_t potentialized_union_find<Group>::find(size_t)':
a.cc:32:5: error: there are no arguments to 'compress' that depend on a template parameter, so a declaration of 'compress' must be available [-fpermissive]
   32 |     compress(x);
      |     ^~~~~~~~
a.cc:32:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In member function 'potentialized_union_find<Group>::T potentialized_union_find<Group>::potential(size_t)':
a.cc:37:5: error: there are no arguments to 'compress' that depend on a template parameter, so a declaration of 'compress' must be available [-fpermissive]
   37 |     compress(x);
      |     ^~~~~~~~
a.cc: In member function 'bool potentialized_union_find<Group>::same(size_t, size_t)':
a.cc:42:5: error: there are no arguments to 'compress' that depend on a template parameter, so a declaration of 'compress' must be available [-fpermissive]
   42 |     compress(x);
      |     ^~~~~~~~
a.cc: In instantiation of 'bool potentialized_union_find<Group>::same(size_t, size_t) [with Group = plus_group<int>; size_t = long unsigned int]':
a.cc:95:17:   required from here
   95 |     if (puf.same(l, r)) {
      |         ~~~~~~~~^~~~~~
a.cc:42:13: error: 'compress' was not declared in this scope
   42 |     compress(x);
      |     ~~~~~~~~^~~
a.cc: In instantiation of 'void potentialized_union_find<Group>::unite(size_t, size_t, T) [with Group = plus_group<int>; size_t = long unsigned int; T = int]':
a.cc:101:16:   required from here
  101 |       puf.unite(l, r, d);
      |       ~~~~~~~~~^~~~~~~~~
a.cc:59:9: error: assignment of read-only parameter 'd'
   59 |       d = Group::inverse(d);
      |       ~~^~~~~~~~~~~~~~~~~~~
a.cc:62:7: error: assignment of read-only parameter 'd'
   62 |     d = Group::operation(Group::operation(potential(x), d),
      |     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   63 |                          Group::inverse(potential(y)));
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In instantiation of 'potentialized_union_find<Group>::T potentialized_union_find<Group>::potential(size_t) [with Group = plus_group<int>; T = int; size_t = long unsigned int]':
a.cc:48:44:   required from 'potentialized_union_find<Group>::T potentialized_union_find<Group>::distance(size_t, size_t) [with Group = plus_group<int>; T = int; size_t = long unsigned int]'
   48 |     return Group::operation(Group::inverse(potential(x)), potential(y));
      |                                            ^~~~~~~~~
a.cc:96:23:   required from here
   96 |       if (puf.distance(l, r) != d) {
      |           ~~~~~~~~~~~~^~~~~~
a.cc:37:13: error: 'compress' was not declared in this scope
   37 |     compress(x);
      |     ~~~~~~~~^~~
a.cc: In instantiation of 'potentialized_union_find<Group>::size_t potentialized_union_find<Group>::size(size_t) [with Group = plus_group<int>; size_t = long unsigned int]':
a.cc:58:13:   required from 'void potentialized_union_find<Group>::unite(size_t, size_t, T) [with Group = plus_group<int>; size_t = long unsigned int; T = int]'
   58 |     if (size(x) < size(y)) {
      |         ~~~~^~~
a.cc:101:16:   required from here
  101 |       puf.unite(l, r, d);
      |       ~~~~~~~~~^~~~~~~~~
a.cc:52:30: error: expression cannot be used as a function
   52 |     return tree[find(x)].size();
      |            ~~~~~~~~~~~~~~~~~~^~