Language: C++

#include <iostream>
#include <cstring>
using namespace std;

int prev[100000 + 2];
int dist[100000 + 2];
int size[100000 + 2];

int root(int x) {
  if (prev[x] == -1) return x;
  int px = prev[x];
  int xx = root(prev[x]);
  dist[x] = dist[px] + dist[x];
  prev[x] = xx;
  return prev[x];
}

int main() {
  memset(prev, 0, sizeof(prev));
  memset(dist, 0, sizeof(dist));
  memset(size, 0, sizeof(size));

  int N, M, L, R, D;
  cin >> N >> M;

  for (int i = 0; i < N; ++i) {
    prev[i] = -1;
    size[i] = 1;
  }

  bool ok = true;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> D;
    L--, R--;
    if (root(L) == root(R)) {
      // check
      if (dist[R] + D != dist[L])
        ok = false;
    } else {
      int x = root(L), y = root(R);
      int x_to_y = -dist[L] + D + dist[R];
      if (size[x] >= size[y]) {
        prev[y] = x;
        size[x] += size[y];
        dist[y] = -x_to_y;
      } else {
        prev[x] = y;
        size[y] += size[x];
        dist[x] = x_to_y;
      }
      root(L), root(R);
    }
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Compiler output:
a.cc: In function 'int root(int)':
a.cc:10:7: error: reference to 'prev' is ambiguous
   10 |   if (prev[x] == -1) return x;
      |       ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:11:12: error: reference to 'prev' is ambiguous
   11 |   int px = prev[x];
      |            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:12:17: error: reference to 'prev' is ambiguous
   12 |   int xx = root(prev[x]);
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:14:3: error: reference to 'prev' is ambiguous
   14 |   prev[x] = xx;
      |   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:15:10: error: reference to 'prev' is ambiguous
   15 |   return prev[x];
      |          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:19:10: error: reference to 'prev' is ambiguous
   19 |   memset(prev, 0, sizeof(prev));
      |          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:19:26: error: reference to 'prev' is ambiguous
   19 |   memset(prev, 0, sizeof(prev));
      |                          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:21:10: error: reference to 'size' is ambiguous
   21 |   memset(size, 0, sizeof(size));
      |          ^~~~
In file included from /usr/include/c++/14/string:53:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:21:26: error: reference to 'size' is ambiguous
   21 |   memset(size, 0, sizeof(size));
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:27:5: error: reference to 'prev' is ambiguous
   27 |     prev[i] = -1;
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:28:5: error: reference to 'size' is ambiguous
   28 |     size[i] = 1;
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:42:11: error: reference to 'size' is ambiguous
   42 |       if (size[x] >= size[y]) {
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:42:22: error: reference to 'size' is ambiguous
   42 |       if (size[x] >= size[y]) {
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:43:9: error: reference to 'prev' is ambiguous
   43 |         prev[y] = x;
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int prev [100002]'
    5 | int prev[100000 + 2];
      |     ^~~~
a.cc:44:9: error: reference to 'size' is ambiguous
   44 |         size[x] += size[y];
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:44:20: error: reference to 'size' is ambiguous
   44 |         size[x] += size[y];
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [100002]'
    7 | int size[100000 + 2];
      |     ^~~~
a.cc:47:9: error: reference to 'prev' is ambiguous
   47 |         prev[x] = y;
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> c