Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#include<regex>
#include<bitset>
#include<chrono>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define pb push_back
#define eb emplace_back
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
using namespace std;
 
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << "\n"; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " <<to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

 template<class T> struct Weighted_UF {
	vector<int>par;//親
	vector<int>Rank;//木の深さ
  vector<T> weight;//親との値の差
  vector<int>sz;//要素数

   Weighted_UF(int n,T value=0){init(n,value);}

	//初期化
	void init(int n,T value=0) {
		par.resize(n); Rank.resize(n);sz.resize(n);weight.resize(n);
		for (int i = 0; i < n; i++) {
			par[i]= i;
			Rank[i] = 0;
      sz[i]=1;
      weight[i]=value;
		}
	}

	//木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
      weight[x]+=weight[par[x]];
			return par[x] = find(par[x]);
		}
	}
  
  T get_weight(int x){
    find(x);
    return weight[x];
  }

	//xとyの属する集合を併合
	bool unite(int x, int y,T w) {
    w+=get_weight(x);w-=get_weight(y);
		x = find(x);
		y = find(y);
		if (x == y)return false;//すでに同じ集合に属している

		if (Rank[x] < Rank[y]) {//高さが小さい方を下に
			par[x] = y;
      sz[y]+=sz[x];
      weight[x]=-w;
		}
		else {
			par[y] = x;
			if (Rank[x] == Rank[y])Rank[x]++;//同じだとrankが変わらないままになるので増やす（それ以外は大きい方のrankが採用される）
            sz[x]+=sz[y];
            weight[y]=w;
		}
		return true;
	}

	//xとyが同じ集合に属するかを判定
	bool same(int x, int y) {
		return find(x) == find(y);
	}

    int size(int k){//要素数を求める
        return sz[find(k)];
    }
  T diff(int x,int y){
    return get_weight(y)-get_weight(x);
  }
};


void solve(){
  int n,m;
  cin>>n>>m;
  Weighted_UF<int>tree(n);
  rep(i,m){
    int l,r,d;
    cin>>l>>r>>d;
    l--;r--;
    if(tree.same(l,r)){
      if(tree.diff(l,r)!=d){
        cout<<"No\n";
        return;
      }
    }
    else tree.unite(l,r,d);
  }
  if(ng)cout<<"No\n";
  else cout<<"Yes\n";
}
 
int main(){
	ios::sync_with_stdio(false);
  cin.tie(0);
	cout<<fixed<<setprecision(15);
	solve();
	return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:218:6: error: 'ng' was not declared in this scope; did you mean 'n'?
  218 |   if(ng)cout<<"No\n";
      |      ^~
      |      n