Language: C++

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define REP(i, n) for (int i = 0; i < n; ++i)

typedef long long ll;
// ---
// union_find_tree

const int kMaxNUft = 100000;

int par[kMaxNUft];
int treerank[kMaxNUft];

void Init(int n) {
  REP(i, n) {
    par[i] = i;
    treerank[i] = 0;
  }
}



void Unite(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x == y) { return; }
  if (treerank[x] < treerank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (treerank[x] == treerank[y]) { ++treerank[x]; }
  }
  return;
}



// union_find_tree
// ---

const int kMaxN = 100000;
const int kMaxM = 200000;

int N, M;
int L[kMaxM], R[kMaxM], D[kMaxM];
string ans;

int parent[kMaxN];
int dist[kMaxN];

int Dist(int x, int d) {
  if (parent[x] == x) {
    return d;
  } else {
    return Dist(parent[x], d+Dist[x]);
  }
}

int Find(int x) {
  if (parent[x] == x) {
    return x;
  } else {
    return Find(parent[x]);
  }
}

bool Same(int x, int y) { return Find(x) == Find(y); }

string Sub() {
  fill(parent, parent+N, -1);
  REP(i, M) {
    int l = L[i];
    int r = R[i];
    int d = D[i];
    if (parent[l] == -1 && parent[r] == -1) {
      parent[l] = r;
      dist[l] = d;
      parent[r] = r;
    } else if (parent[l] == -1 && parent[r] != -1) {
      parent[r] = l;
      dist[r] = -d;
    } else if (parent[l] != -1 && parent[r] == -1) {
      parent[l] = r;
      dist[l] = d;
    } else {
      if (Same(l, r)) {
        if (Dist[l] + d != Dist[r]) { return "No"; }
      } else {
        parent[l] = r;
        dist[l] = d;
      }
    }
  }
  return "Yes";
}

int main() {
  cin >> N >> M;
  REP(i, M) { cin >> L[i] >> R[i] >> D[i]; }
  REP(i, M) {
    --L[i];
    --R[i];
  }

  ans = Sub();

  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'void Unite(int, int)':
a.cc:34:7: error: 'Find' was not declared in this scope
   34 |   x = Find(x);
      |       ^~~~
a.cc: In function 'int Dist(int, int)':
a.cc:65:36: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   65 |     return Dist(parent[x], d+Dist[x]);
      |                                    ^
a.cc:65:29: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   65 |     return Dist(parent[x], d+Dist[x]);
      |                            ~^~~~~~~~
a.cc:65:29: error: invalid conversion from 'int (*)(int, int)' to 'int' [-fpermissive]
   65 |     return Dist(parent[x], d+Dist[x]);
      |                            ~^~~~~~~~
      |                             |
      |                             int (*)(int, int)
a.cc:61:21: note:   initializing argument 2 of 'int Dist(int, int)'
   61 | int Dist(int x, int d) {
      |                 ~~~~^
a.cc: In function 'std::string Sub()':
a.cc:97:19: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   97 |         if (Dist[l] + d != Dist[r]) { return "No"; }
      |                   ^
a.cc:97:21: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   97 |         if (Dist[l] + d != Dist[r]) { return "No"; }
      |             ~~~~~~~~^~~
a.cc:97:34: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   97 |         if (Dist[l] + d != Dist[r]) { return "No"; }
      |                                  ^