stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( int (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( int (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( int (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define print cout << endl

#define tri pair<pair<int, int>, int>

using namespace std;



int n,m;

int fa[100005], cnt[100005];

void find(int x){

    if ( fa[x] == x ) return;

    find(fa[x]);

    cnt[x] += cnt[fa[x]]; fa[x] = fa[fa[x]];

}



int main(){

    while ( cin >> n >> m ) {

        bool has = false;

        MSET(fa); MSET(cnt);

        for ( int i = 0 ; i <= n ; ++ i ) fa[i] = i;

        for ( int i = 0, x, y, z ; i < m ; ++ i ) {

            scanf("%d %d %d", &x, &y, &z);

            find(x); find(y);

            if ( fa[x] == fa[y] ) {

                if ( cnt[y] != cnt[x] + z ) has = true;

            }

            else {

                cnt[fa[y]] = z - cnt[y];

                fa[fa[y]] = x;

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

    if(x==pre[x])   return x;

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

   scanf("%d%d",&n,&m);

   for(int i=1;i<=n;i++)

    pre[i]=i;

   bool lose=true;

   for(int i=0;i<m;i++)

   {

       int x,y,w;

   scanf("%d%d%d",&x,&y,&w);

        if(lose)

       lose=unionset(x,y,w);

   }

   if(lose) printf("Yes\n");

   else     printf("No\n");

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,l,r,d, dis[N], vis[N], v=1;

vector <pair<int, int>> g[N];

stack <int> st;

int dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

        if(vis[pa.f] != v)

        {

            dis[pa.f] = dis[from] + pa.s;

            if(dfs(pa.f) == -1)

            {

                return -1;

            }

        }else if(dis[pa.f] != dis[from] + pa.s)

        {

            return -1;

        }

    }

    return 1;

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

        if(vis[pa.f] != v)

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    cin >> n >> m;

    for(int i=0; i<m; i++)

    {

        cin >> l >> r >> d;

        g[l].push_back({r,d});

    }

    for(int i=1; i<n ;i++)

    {

        if(vis[i] != v) 

        {

            st.push(i);

            dfs1(i);

        }

    }

    v++;

    while(!st.empty())

    {

        int i = st.top();

        if(vis[i] != v)

        {

            for(auto pa : g[i])

            {

                if(vis[pa.f] == v)

                {

                    dis[i] = dis[pa.f] - pa.s;

                    break;

                }

            }

            if(dfs(i) == -1)

            {

               cout << "No";

                return 0;

            }

        }

        st.pop();

    }

    cout << "Yes";

    return 0;

}

// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

    if(x==pre[x])   return x;

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

   scanf("%d%d",&n,&m);

   for(int i=1;i<=n;i++)

    pre[i]=i;

   bool lose=true;

   for(int i=0;i<m;i++)

   {

       int x,y,w;

   scanf("%d%d%d",&x,&y,&w);

        if(lose)

       lose=unionset(x,y,w);

   }

   if(lose) printf("Yes\n");

   else     printf("No\n");

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



#define _ << " " <<

#define pb push_back

#define ff first

#define ss second





typedef long long ll;

typedef long double ld;



#define int ll



const int MAXN = 1e5 + 5;



vector<int> used(MAXN);



vector<pair<int, int> > g[MAXN];



bool bfs(int v) {

    vector<int> q = {v};

    int ind = 0;

    map<int, int> dst;

    dst[v] = 0;

    used[v] = 1;

    while (q.size() > ind) {

        int tmp = q[ind];

        ++ind;

        for (auto to : g[tmp]) {

            if (used[to.ff] == 1) {

                if (dst[to.ff] != dst[tmp] + to.ss) return false;

            } else {

                dst[to.ff] = dst[tmp] + to.ss;

                used[to.ff] = 1;

                q.pb(to.ff);

            }

        }

    }

    return true;

}



void solve() {

    int n, m;

    cin >> n >> m;

    int l, r, d;

    for (int i = 0; i < m; ++i) {

        cin >> l >> r >> d;

        g[l].pb({r, d});

        g[r].pb({l, -d});

    }

    for (int i = 0; i <= n; ++i) {

        if (used[i] == 0 && !bfs(i)) {

            cout << "No";

            return;

        }

    }

    cout << "Yes";

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    //freopen("dominoes.in", "r", stdin);

    //freopen("dominoes.out", "w", stdout);

    int test = 1;

    //cin >> test;

    while (test--) {

        solve();

    }

}


// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

    if(x==pre[x])   return x;

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

   scanf("%d%d",&n,&m);

   for(int i=1;i<=n;i++)

    pre[i]=i;

   bool lose=true;

   for(int i=0;i<m;i++)

   {

       int x,y,w;

   scanf("%d%d%d",&x,&y,&w);

        if(lose)

       lose=unionset(x,y,w);

   }

   if(lose) printf("Yes\n");

   else     printf("No\n");

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define fi first

#define se second

#define ll long long



const int N = 200200;



using namespace std;



int n, m;

long long a[N];

int res = 1;

long long L, R;

bool used[N];

vector < pair < int, int > > v[N];



void dfs(int x, long long c)

{

        if(used[x]){

                res &= (a[x] == c);

                return;

        }

        used[x] = 1;

        L = min(L, c);

        R = max(R, c);

        a[x] = c;

        for(auto y: v[x]){

                dfs(y.fi, c + y.se);

        }

}



int main()

{

        ios_base::sync_with_stdio(0);



        //freopen("input.txt", "r", stdin);

        //freopen("output.txt", "w", stdout);



        cin >> n >> m;

        for(int i = 1; i <= m; i++){

                int a, b, d;

                cin >> a >> b >> d;

                v[a].push_back({b, d});

                v[b].push_back({a, - d});

        }

        long long inf = 1e9;

        for(int i = 1; i <= n; i++){

                if(!used[i]){

                        L = R = 0;

                        dfs(i, 0);

                        res &= (R - L <= inf);

                }

        }

        cout << (res ? "Yes" : "No") << "\n";

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define int long long

#define vi vector<int>

#define fileopen(filename) if (fopen(filename".INP", "r")) {freopen(filename".INP", "r", stdin);freopen(filename".OUT", "w", stdout);}

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define rep(i,n) for (int i=1;i<=n;++i)

#define eb emplace_back

#define pb push_back

#define memset(a,i) memset(a,i,sizeof(a))

#define upper(v,a) upper_bound(v.begin(), v.end(), a)

#define lower(v,a) lower_bound(v.begin(), v.end(), a)

#define ii pair<int,int>

#define fi first

#define se second

#define endl '\n'

#define gcd __gcd

const int maxn=1e5+1;

const int INF=1e9+1;



bool used[maxn];

vector<ii> adj[maxn];



void dfs(int dis[],int node, int dist)

{

	used[node]=1;

	for (ii i : adj[node])

		if (!used[i.fi])

		{

			dis[i.fi]=dist+i.se;

			dfs(dis,i.fi,dist+i.se);

		}	

		else if (dist+i.se!=dis[i.fi])

		{

			//cout<<node<<' '<<i.fi;

			cout<<"No";exit(0);

			

		}

}



signed main()

{

	fastio;

	int n,m;cin>>n>>m;

	while (m--)

	{

		int a,b,c;cin>>a>>b>>c;

		adj[a].eb(ii(b,c));

		adj[b].eb(ii(a,-c));

	}

	rep(i,n)if (!used[i])

	{

		int a[n+1];

		a[i]=0;

		dfs(a,i,0);

	}

	cout<<"Yes";

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n) FOR(i,0,n)

#define ALL(v) (v).begin(),(v).end()

#define fi first

#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }

template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;

typedef pair<int, int> pii;

const ll INF = 1e9+100;

const ll MOD = 1e9+7;

const bool debug = 0;

//-----------------------------------------------//



int N, M;

int dist[112345];

vector<pii> g[112345];



int main() {

	cin >> N >> M;

	REP(i, M) {

		int a, b, d;

		scanf("%d %d %d", &a, &b, &d);

		a--; b--;

		g[a].push_back(pii(b, d));

		g[b].push_back(pii(a, -d));

	}

	

	fill(dist, dist + N, INF);

	REP(st, N) {

		if (dist[st] != INF) continue;

		

		queue<int> que;

		que.push(st);

		dist[st] = 0;

		

		while (!que.empty()) {

			int u = que.front(); que.pop();

			

			REP(i, g[u].size()) {

				int v = g[u][i].fi;

				int ncost = dist[u] + g[u][i].se;

				

				if (dist[v] == INF) {

					dist[v] = ncost;

					que.push(v);

				}

				else if (dist[v] != ncost) {

					puts("No");

					return 0;

				}

				

			}

		}

	}

	

	puts("Yes");

	

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n) FOR(i,0,n)

#define ALL(v) begin(v),end(v)

#define fi first

#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }

template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

using ll = long long;

using pii = pair<int, int>;

constexpr ll INF = 1ll<<30;

constexpr ll longINF = 1ll<<60;

constexpr ll MOD = 1000000007;

constexpr bool debug = 0;

//---------------------------------//



int main() {

	int N, M;

	cin >> N >> M;

	vector<int> L(M), R(M), D(M);

	REP(i, M) scanf("%d %d %d", &L[i], &R[i], &D[i]), --L[i], --R[i];

	

	vector<vector<pii>> g(N);

	REP(i, M) {

		g[L[i]].emplace_back(R[i], D[i]);

		g[R[i]].emplace_back(L[i], -D[i]);

	}

	

	vector<int> x(N, -1), done(N, false);

	

	bool ok = true;

	

	REP(i, N) {

		if (done[i]) continue;

		queue<int> que;

		que.push(i);

		done[i] = true;

		x[i] = 0;

		while (!que.empty()) {

			int u = que.front(); que.pop();

			

			for (pii e : g[u]) {

				int v = e.fi, d = e.se;

				if (chmax(done[v], true)) {

					x[v] = x[u] + d;

					que.push(v);

				}

				else {

					ok &= x[v] == x[u] + d;

				}

			}

		}

	}

	

	puts([&] {

		return ok;

	}() ? "Yes": "No");

	

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(n);++i)

using namespace std;

typedef long long ll;



const ll INF = 1LL << 60;



vector< vector< pair<int,int> > > to(100005);

vector< ll > pos(100005, INF);

set<int> A;



bool DFS( int v ) {

  bool ok = true;

//  cout << v << " : " << pos[v] << endl;

  for ( auto nv : to[v] ) {

//    cout << " " << nv.first << ", " << nv.second << " : " << pos[nv.first] << endl;

    if ( pos[nv.first] == INF ) {

      pos[nv.first] = pos[v] + nv.second;

      if (pos[nv.first] > 1000000000 && pos[nv.first] != INF) return false;

      ok = DFS( nv.first );

      if (!ok ) return ok;

    } else {

      if ( pos[nv.first] != pos[v] + nv.second ) {

//        cout << " false" << endl;

        return false;

      }

    }

  }

  return ok;

}



int main() {

  int N, M;

  cin >> N >> M;

  REP(i,M) {

    int l,r,d;

    cin >> l >> r >> d;

    to[l].emplace_back( r, d );

    to[r].emplace_back( l, -d );

    A.emplace(r);

    A.emplace(l);

  }

  for ( auto v : A ) {

    if ( pos[v] != INF ) continue;

    pos[v] = 0;

    if ( DFS( v ) == false ) {

      cout << "No" << endl;

      return 0;

    }

  }

  cout << "Yes" << endl;

}




// Optimized solution to the same problem (faster):
#include <iostream>

#include <vector>

#include <string>

using namespace std;



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;

        diff_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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;



typedef long long ll;

ll N, M, R, L, D, path[100000], loc[100000];



int main() {

  cin >> N >> M;



  vector<vector<pair<ll, ll> > > vec(N);

  memset(path, 0, sizeof(path));

  for (int i = 0; i < M; i++) {

    cin >> L >> R >> D;

    L--;

    R--;

    vec[L].push_back(make_pair(R ,D));

    vec[R].push_back(make_pair(L ,-D));

  }



  for (int i  = 0; i < N; i++) {

    if (!path[i]) {

      path[i]++;

      queue<ll> que;

      que.push(i);



      while(!que.empty()) {

        ll q = que.front();

        que.pop();



        for (auto x : vec[q]) {

          if (!path[x.first]) {

            loc[x.first] = loc[q] + x.second;

            path[x.first]++;

            que.push(x.first);

          } else {

            if (loc[x.first] != loc[q] + x.second) {

              cout << "No" << endl;

              return 0;

            }

          }

        }

      }

    }

  }

  cout << "Yes" << endl;

  return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <set>

#include <map>

#include <vector>

#include <algorithm>

#include <iomanip>

#include <climits>

#include <numeric>

#include <cmath>

#include <queue>

#include <sstream>

#include <string.h>

#include <bitset>

#include <stack>



using namespace std;

typedef long long ll;



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;

    diff_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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



vector<vector<ll>>a(100010);

vector<vector<ll>>c(100010);

ll p[100010];

bool ans=true;

void dep(ll now,ll oya,ll co){

	if(p[now]!=INF){

		if(p[now]!=co){

			ans=false;

		}

		return ;

	}

	p[now]=co;

	if(a[now].size()==1&&a[now][0]==oya){

		return;

	}

	for(int i=0;i<a[now].size();i++){

		ll next=a[now][i];

		dep(next,now,co+c[now][i]);

	}

}

int main() {

	ll n,m;

	cin>>n>>m;

	for(int i=0;i<100010;i++){

		p[i]=INF;

	}

	for(int i=0;i<m;i++){

		ll x,y,z;

		cin>>x>>y>>z;

		x--,y--;

		a[x].push_back(y);

		c[x].push_back(z);

		a[y].push_back(x);

		c[y].push_back(-1*z);

	}

	for(int i=0;i<n;i++){

		if(p[i]!=INF){

			continue;

		}

		dep(i,-1,0);

	}

	cout << (ans?"Yes":"No");

	// your code goes here

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



bool ans=true;

vector<pair<ll,ll>>a[100010];

ll co[100010];

void dep(ll now,ll cos){

	for(ll i=0;i<a[now].size();i++){

		ll next=a[now][i].first;

		ll val=a[now][i].second;

		if(co[next]!=INF){

			ll ch=co[now]+val;

			if(ch!=co[next]){

				ans=false;

			}

			continue;

		}

		co[next]=co[now]+val;

		dep(next,co[next]);

	}

}

int main() {

	ll n,m;

	cin>>n>>m;

	for(ll i=0;i<m;i++){

		ll x,y,z;

		cin>>x>>y>>z;

		x--;y--;

		a[x].push_back(make_pair(y,z));

		a[y].push_back(make_pair(x,-1*z));

	}

	for(ll i=0;i<n;i++){

		co[i]=INF;

	}

	for(ll i=0;i<n;i++){

		if(co[i]!=INF){

			continue;

		}

		co[i]=0;

		dep(i,co[i]);

	}

	cout << (ans?"Yes":"No");

	// your code goes here

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=100005;

struct node

{

	int to;

	ll v;

	node(int a=0,int b=0){to=a;v=b;}

};

vector<node> path[N];

bool use[N];

ll d[N];

bool dfs(int o)

{

	use[o]=true;

	for(int i=path[o].size()-1;i>=0;i--)

	{

		node e=path[o][i];

		if(d[e.to]>d[o]+e.v)

		{

			if(use[e.to]) return true;

			d[e.to]=d[o]+e.v;

			if(dfs(e.to)) return true;

		}

	}

	use[o]=false;

	return false;

}

int main()

{

	int n,m,a,b;

	ll dd;

	scanf("%d%d",&n,&m);

	while(m--)

	{

		scanf("%d%d%lld",&a,&b,&dd);

		path[a].push_back(node(b,dd));

		path[b].push_back(node(a,-dd));

	}

	for(int i=1;i<=n;i++) 

	{

		d[i]=LLONG_MAX;

		path[0].push_back(node(i,0));

	}

	if(dfs(0)) puts("No");

	else puts("Yes");

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define int long long

using namespace std;

const int maxN=1e6 + 100;

struct Node

{

	int to,value,next;

}edge[maxN*2+1];

int n,m,head[maxN+1],tot,dis[maxN+1];

bool col[maxN+1];

int maxT=-1e15,minT=1e15,ans=-1e15;

void add(int x,int y,int t)

{

	tot++;

	edge[tot].to=y;

	edge[tot].value=t;

	edge[tot].next=head[x];

	head[x]=tot;

}

bool dfs(int x)

{

	maxT=max(maxT,dis[x]);

	minT=min(minT,dis[x]);

	bool flag=true;

	col[x]=true;

	for(int i=head[x];i;i=edge[i].next)

		if(!col[edge[i].to]) dis[edge[i].to]=dis[x]+edge[i].value,flag=(flag&&dfs(edge[i].to));

		else if(dis[x]+edge[i].value!=dis[edge[i].to]) flag=false;

	return flag;

}

#undef int

int main()

#define int long long

{

//	freopen("line.in","r",stdin);

//	freopen("line.out","w",stdout);

	scanf("%lld%lld",&n,&m);

	for(int i=1;i<=m;i++)

	{

		int x,y,t;

		scanf("%lld%lld%lld",&x,&y,&t);

		add(x,y,t); add(y,x,-t);

	}

	for(int i=1;i<=n;i++)

		if(!col[i])

		{

			maxT=-1e18,minT=1e18;

			if(!dfs(i))

			{

				printf("No");

				return 0;

			}

			ans=max(ans,maxT-minT);

		}

	printf("Yes");

	fclose(stdin);

	fclose(stdout);

	return 0;

}

// Fastest known solution for this problem: