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

#include<cstdio>

using namespace std;

struct Edge

{

    int to,next,cost;	

}e[400005];

int head[100005],cnt=0;

bool vis[100005];

int d[100005];

bool use[100005];

void addedge(int from,int to,int cost)

{

    e[++cnt].to=to;

    e[cnt].cost=cost;

    e[cnt].next=head[from];

    head[from]=cnt;

}

bool flag=0;

void dfs(int x)

{

    vis[x]=1;

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

    {

        int w=e[i].cost,v=e[i].to;

        if(d[v]>d[x]+w)

        {

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

            if(vis[v])

            {

                flag=1;

                return;

            }

            dfs(v);	

        }	

    } 

    vis[x]=0;

    return;

}

inline int read()

{

    int s=0,f=1;char x=getchar();

    while(!isdigit(x))

    {

        if(x=='-') f=-1;

        x=getchar();

    }

    while(isdigit(x))

    {

        s=(s<<1)+(s<<3)+(x^48);

        x=getchar();

    }

    return s*f;

}

int main()

{

    int n,m;

    n=read();m=read();

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

    {

        int x,y,z;

        x=read();y=read();z=read();

        addedge(y,x,z);

        addedge(x,y,-z);

    }

    flag=0;

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

    {

        d[i]=0;

        if(!use[i])

        {

            use[i]=1;

            dfs(i);

        }	

    }

    if(flag) cout<<"No";

    else cout<<"Yes";

    return 0;

}

// Optimized solution to the same problem (faster):
// SeptEtavioxy

#include<cstdio>

#include<cctype>

#include<cstring>

#include<algorithm>

#include<cmath>

#define re register

#define ll long long

#define il inline

#define rep(i,s,t) for(re int i=(s);i<=(t);i++)

#define rev_rep(i,s,t) for(re int i=(s);i>=(t);i--)

#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)

#define pt(ch) putchar(ch)

#define pti(x) printf("%d",x)

#define ptll(x) printf("%lld",x)

#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)

using namespace std;

// c_ints

il int ci(){

	re char ch;int f=1;

	while(!isdigit(ch=getchar()))f= ch=='-'?-1:1 ;

	re int x= ch^'0';

	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');

	return f*x;

}

enum{N=100024};

int fa[N],dis[N];

void fd(int x){

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

	fd(fa[x]);

	dis[x]+= dis[fa[x]];

	fa[x]= fa[fa[x]];

}

int main(){

	int n=ci(), m=ci();

	rep(i,1,n) fa[i]= i;

	while( m-- ){

		int l=ci(), r=ci(), d=ci();

		fd(l), fd(r);

		if( fa[l]==fa[r] ){

			if( dis[r]-dis[l]!=d ){

				puts("No");

				return 0;

			}

		}

		else{

			int fl=fa[l], fr=fa[r];

			int x= dis[l]+d-dis[r];

//			printf("%d %d\n",fl,fr);

//			if( x<0 ){

//				puts("No");

//				return 0;

//			}

			dis[fr]= x;

			fa[fr]= fl;

		}

//		printf("fa :");rep(i,1,n) printf("%d ",fa[i]); pt('\n');

//		printf("dis:");rep(i,1,n) printf("%d ",dis[i]); pt('\n');

	}

	puts("Yes");

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>



using namespace std;



struct edge {

	int v,w,nxt;

} e[400050];



int head[100050],ecnt;



void adde(int u,int v,int w) {

	e[++ecnt].v = v;

	e[ecnt].w = w;

	e[ecnt].nxt = head[u];

	head[u] = ecnt;

}



int N,M;

int l[200050],r[200050];

int d[200050];

int dist[100005];

queue<int> que;

bool vis[100050],inq[100050];



bool spfa(int st) {

	que.push(st);

	inq[st] = 1;

	dist[st] = 0;

	while(!que.empty()) {

		int u = que.front();

		que.pop();

		for(int i = head[u]; i; i = e[i].nxt) {

			int v = e[i].v;

			if(dist[v] == 1061109567) {

				dist[v] = dist[u] + e[i].w;

				vis[v] = 1;

				if(!inq[v]) que.push(v);

			}

			else if(dist[v] != dist[u] + e[i].w) return 0;

				

		}

		inq[u] = 0;

	}

	return 1;

}



int main() {

	memset(dist,0x3f,sizeof(dist));

	scanf("%d%d",&N,&M);

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

		scanf("%d%d%d",&l[i],&r[i],&d[i]);

		adde(l[i],r[i],d[i]);

		adde(r[i],l[i],-d[i]);

	}

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

		if(!vis[i]) {

			vis[i] = 1;

			if(!spfa(i)) {

				printf("No\n");

				return 0;

			}

		}

	}

	printf("Yes\n");

	return 0;

}

// Optimized solution to the same problem (faster):
// SeptEtavioxy

#include<cstdio>

#include<cctype>

#include<cstring>

#include<algorithm>

#include<cmath>

#define re register

#define ll long long

#define il inline

#define rep(i,s,t) for(re int i=(s);i<=(t);i++)

#define rev_rep(i,s,t) for(re int i=(s);i>=(t);i--)

#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)

#define pt(ch) putchar(ch)

#define pti(x) printf("%d",x)

#define ptll(x) printf("%lld",x)

#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)

using namespace std;

// c_ints

il int ci(){

	re char ch;int f=1;

	while(!isdigit(ch=getchar()))f= ch=='-'?-1:1 ;

	re int x= ch^'0';

	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');

	return f*x;

}

enum{N=100024};

int fa[N],dis[N];

void fd(int x){

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

	fd(fa[x]);

	dis[x]+= dis[fa[x]];

	fa[x]= fa[fa[x]];

}

int main(){

	int n=ci(), m=ci();

	rep(i,1,n) fa[i]= i;

	while( m-- ){

		int l=ci(), r=ci(), d=ci();

		fd(l), fd(r);

		if( fa[l]==fa[r] ){

			if( dis[r]-dis[l]!=d ){

				puts("No");

				return 0;

			}

		}

		else{

			int fl=fa[l], fr=fa[r];

			int x= dis[l]+d-dis[r];

//			printf("%d %d\n",fl,fr);

//			if( x<0 ){

//				puts("No");

//				return 0;

//			}

			dis[fr]= x;

			fa[fr]= fl;

		}

//		printf("fa :");rep(i,1,n) printf("%d ",fa[i]); pt('\n');

//		printf("dis:");rep(i,1,n) printf("%d ",dis[i]); pt('\n');

	}

	puts("Yes");

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>



using namespace std;



struct edge

{

    int v,w,nxt;

}e[400050];



const long long INF=1e12;



int head[100050],ecnt;



void add(int u,int v,int w)

{

    e[++ecnt].v = v;

    e[ecnt].w = w;

    e[ecnt].nxt = head[u];

    head[u] = ecnt;

}



int N,M;

int l[200050],r[200050];

int d[200050];

long long dis[100005];

queue<int> que;

bool vis[100050],inq[100050];



bool spfa(int st)

{

    que.push(st);

    inq[st] = true;

    dis[st] = 0;

    long long curmax = 0,curmin = 0;

    while(!que.empty())

    {

        int u = que.front();

        que.pop();

        for(int i = head[u];i;i = e[i].nxt)

        {

            int v = e[i].v;

            if(dis[v] == INF)

            {

                dis[v] = dis[u] + e[i].w;

                curmax = max(curmax,dis[v]);

                curmin = min(curmin,dis[v]);

                if(curmax - curmin > 1000000000)

                    return false;

                vis[v] = 1;

                if(!inq[v])

                    que.push(v);

            }

            else if(dis[v] != dis[u] + e[i].w)

                return false;

        }

        inq[u] = false;

    }

    return true;

}



int main()

{

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

        dis[i] = INF;

    scanf("%d%d",&N,&M);

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

    {

        scanf("%d%d%d",&l[i],&r[i],&d[i]);

        add(l[i],r[i],d[i]);

        add(r[i],l[i],-d[i]);

    }

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

    {

        if(!vis[i])

        {

            vis[i] = 1;bool l = spfa(i);

            if(!l)

            {

                printf("No\n");

                return 0;

            }

        }

    }

    printf("Yes\n");

    return 0;

}

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

#include <cstring>

#include <iostream>

using namespace std;

const int mn=1e5+5,mm=4e5+5;

int dis[mn],vis[mn],nbs[mn],nxt[mm],ev[mm],ew[mm];

int n,m,ok,tot;



void add(int u,int v,int w)

{

	nxt[++tot]=nbs[u],nbs[u]=tot,ev[tot]=v,ew[tot]=w;

}



void spfa(int i)

{

	vis[i]=1;

	for (int j=nbs[i];j!=0;j=nxt[j])

	{

		int v=ev[j],w=ew[j];

		if (dis[v]>dis[i]+w)

		{

			if ((vis[v]==1)||(ok==1)) return ok=1,void();

			dis[v]=dis[i]+w,spfa(v);

		}

	}

	vis[i]=0;

}



int main()

{

	int x,y,z;

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

	for (int i=1;i<=m;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,-z);

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

	printf(ok==1?"No\n":"Yes\n");

	return 0;

}

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

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

#define rep(i,n) repd(i,0,n)

#define int long long

#define double long double

#define PI 3.141592653589793

typedef long long ll;

const int MOD = 1000000007;

const int INF = LLONG_MAX;

const double EPS = 1e-10;

using namespace std;

const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 



vector<int> divisor(int n){

  vector<int> ret;

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

    if(n%i == 0){

      ret.push_back(i);

      if(i*i != n)ret.push_back(n/i);

    }

  }

  sort(ret.begin(), ret.end());

  return ret;

}



int n,m;vector<pair<int,int>> v[110000];

int inds[110000];



bool dfs(int ind, int node){

  if(inds[node] != INF)return (inds[node] == ind);

  inds[node] = ind;

  bool ret = true;

  rep(i,v[node].size()){

    ret = ret && dfs(ind+v[node][i].second, v[node][i].first);

  }

  return ret;

}



signed main(){

  cin>>n>>m;

  rep(i,m){

    int l,r,d;cin>>l>>r>>d;

    v[l].push_back(make_pair(r,d));

    v[r].push_back(make_pair(l,-d));

  }

  rep(i,110000)inds[i] = INF;

  bool ans = true;

  repd(i,1,n+1){

    if(inds[i] == INF)ans = ans && dfs(0,i);

  }

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

}




// Optimized solution to the same problem (faster):
//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

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

#define rep(i,n) repd(i,0,n)

typedef long long ll;

typedef long double lb;

#define int long long

#define double long double

#define endn "\n"

typedef pair<int,int> P;

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

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

const int MOD = 1000000007;

const ll INF = 1e16;

const double EPS = 1e-10;

const double PI = 3.141592653589793;

const string abc="abcdefghijklmnopqrstuvwxyz";

const string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 





////////////////////////////////////////////////////////////////////



int n,m;vector<P> v[110000];

bool visited[110000];

int d[110000], mx, mm;

bool dfs(int x){

  chmax(mx, d[x]);chmin(mm, d[x]);

  if(visited[x])return true;

  visited[x] = true;

  bool ret = true;

  rep(i,v[x].size()){

    int to = v[x][i].first, dis = v[x][i].second;

    if(d[to] == INF){

      d[to] = d[x] + dis;

      ret = ret && dfs(to);

    }else if(d[to] == d[x] + dis){

      ret = ret && dfs(to);

    }else{

      return false;

    }

  }

  return ret;

}

signed main(){

  cin>>n>>m;

  rep(i,m){

    int l, r, d;cin>>l>>r>>d;

    l--;r--;

    v[l].push_back(P(r, d));

    v[r].push_back(P(l, -d));

  }

  rep(i,n)d[i] = INF;

  bool ans = true;

  rep(i,n){

    if(!visited[i]){

      d[i] = 0;mx = -INF, mm = INF;

      ans = ans && dfs(i);

      if(mx - mm > 1000000000)ans = false;

    }

  }

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

}


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

using namespace std;

#define int long long



typedef pair<int, int> P;

const int N = 2e5;

const int INF = 1e18;



struct edge{ int to,cost; };

vector< edge > G[N];



signed main(){



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

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

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

    G[L].push_back({R,D});

    G[R].push_back({L,-D});

  }



  int d[N];

  fill(d,d+N,INF);

  queue<int> que;

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

    if(G[i].empty() || d[i] < INF) continue;

    d[i] = 0; //基準にして考える

    que.push(i);

    while(!que.empty()){

      int from = que.front();

      for(int j = 0; j < G[from].size(); j++){

        int to = G[from][j].to;

        int cost = G[from][j].cost;

        if(d[to] < INF){

          if(d[to] != d[from] + cost){

            cout << "No" << endl;

            return 0;

          }

        }

        else {

          d[to] = d[from] + cost;

          que.push(to);

        }

      }

      que.pop();

    }

  }

  cout << "Yes" << endl;



  return 0;

}


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

using namespace std;

#define int long long



struct UF{

  const int N = 1e5+10;

  vector<int> par;

  vector<int> depth;

  vector<int> diff;



  void init(int n){

    par.resize(n); depth.resize(n); diff.resize(n);

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

      par[i] = i;

      depth[i] = 0;

      diff[i] = 0;

    }

  }



  //木の根を求める

  int find(int x){

    if(par[x] == x){

      return x;

    }

    else {

      int r = find(par[x]);

      diff[x] += diff[par[x]];

      return par[x] = r;

    }

  }



  int weight(int x){

    find(x); //経路圧縮

    return diff[x];

  }



  //xとyの属する集合を併合

  void unite(int x,int y,int w){

    w += weight(x) - weight(y);

    x = find(x);

    y = find(y);

    if(x == y) return;



    if(depth[x] < depth[y]) swap(x,y),w = -w;

    if(depth[x] == depth[y]) depth[x]++;

    par[y] = x;

    diff[y] = w;

  }

}uf;



signed main(){

  cin.tie(0);

  ios::sync_with_stdio(false);



  int n,q; cin >> n >> q;

  uf.init(n);

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

    int l,r,d; cin >> l >> r >> d;

    l--,r--;

    if(uf.find(l) == uf.find(r)){

      if(uf.weight(r)-uf.weight(l) != d){

        cout << "No" << endl;

        return 0;

      }

    }

    else uf.unite(l,r,d);

  }

  cout << "Yes" << endl;



  return 0;

}


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

using namespace std;

#define int long long



typedef pair<int, int> P;

const int N = 2e5;

const int INF = 1e18;



struct edge{ int to,cost; };

vector< edge > G[N];



signed main(){

  cin.tie(0);

  ios::sync_with_stdio(false);



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

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

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

    G[L].push_back({R,D});

    G[R].push_back({L,-D});

  }



  int d[N];

  fill(d,d+N,INF);

  queue<int> que;

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

    if(G[i].empty() || d[i] < INF) continue;

    d[i] = 0; //基準にして考える

    que.push(i);

    while(!que.empty()){

      int from = que.front();

      for(int j = 0; j < G[from].size(); j++){

        int to = G[from][j].to;

        int cost = G[from][j].cost;

        if(d[to] < INF){

          if(d[to] != d[from] + cost){

            cout << "No" << endl;

            return 0;

          }

        }

        else {

          d[to] = d[from] + cost;

          que.push(to);

        }

      }

      que.pop();

    }

  }

  cout << "Yes" << endl;



  return 0;

}


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

using namespace std;

#define int long long



struct UF{

  const int N = 1e5+10;

  vector<int> par;

  vector<int> depth;

  vector<int> diff;



  void init(int n){

    par.resize(n); depth.resize(n); diff.resize(n);

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

      par[i] = i;

      depth[i] = 0;

      diff[i] = 0;

    }

  }



  //木の根を求める

  int find(int x){

    if(par[x] == x){

      return x;

    }

    else {

      int r = find(par[x]);

      diff[x] += diff[par[x]];

      return par[x] = r;

    }

  }



  int weight(int x){

    find(x); //経路圧縮

    return diff[x];

  }



  //xとyの属する集合を併合

  void unite(int x,int y,int w){

    w += weight(x) - weight(y);

    x = find(x);

    y = find(y);

    if(x == y) return;



    if(depth[x] < depth[y]) swap(x,y),w = -w;

    if(depth[x] == depth[y]) depth[x]++;

    par[y] = x;

    diff[y] = w;

  }

}uf;



signed main(){

  cin.tie(0);

  ios::sync_with_stdio(false);



  int n,q; cin >> n >> q;

  uf.init(n);

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

    int l,r,d; cin >> l >> r >> d;

    l--,r--;

    if(uf.find(l) == uf.find(r)){

      if(uf.weight(r)-uf.weight(l) != d){

        cout << "No" << endl;

        return 0;

      }

    }

    else uf.unite(l,r,d);

  }

  cout << "Yes" << endl;



  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;

typedef pair<int,int> P;

const int INF = 1e9;

const int MOD = 1000000007;

const int MAX = 1000000;

int n = 100000;

vector<vector<P>> graph(n,vector<P>());

vector<bool> seen(n,false);

vector<int> dis(n);



bool dfs(int i,int p = -1,int d = 0){

        if(!seen[i]) dis[i] = d;

        else return dis[i] == d;

        seen[i] = true;

        bool res = true;

        for(P child:graph[i]){

                if(child.first == p) continue;

                 res = res && dfs(child.first,i,d+child.second);

        }

        return res;

}





int main(){

        int m;

        cin >> n >> m;

        rep(i,m){

                int l,r,d;

                cin >> l >> r>> d;

                --l;--r;

                graph[l].push_back(P(r,d));

                graph[r].push_back(P(l,-d));

        }



        rep(i,n){

                if(!seen[i]){

                        if(!dfs(i)){

                                cout << "No" << endl;

                                return 0;

                        }

                }

        }



        cout << "Yes" << endl;

        return 0;

}

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

using namespace std;

typedef long long ll;

typedef pair<int,int> pp;

const int INF = 1e9;

const int MOD = 1000000007;

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



struct potential_Unionfind{

    vector<int> rank;

    vector<int> par;

    vector<int> weights;

    potential_Unionfind(int n){

        rank.resize(n);

        par.resize(n);

        weights.resize(n);

        for(int i=0;i<n;i++) par[i] = i,rank[i] = 0,weights[i] = 0;

    }



    int find(int x){

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

        else{

            int p = find(par[x]);

            weights[x] += weights[par[x]];

            return par[x] = p;

        }

    }



    //weight[y] - weights[x] = wとなるようにuniteする。

    bool unite(int x,int y,int w){

        w += weight(x);

        w -= weight(y);

        x = find(x);

        y = find(y);

        if(x == y) return false;//もうすでに繋がれている頂点ならfalseを返す

        if(rank[x] < rank[y]) {swap(x,y); w = -w;}

        if(rank[x] == rank[y]) rank[x] ++;

        par[y] = x;

        weights[y] = w;

        return true;

    }



    bool same(int x,int y){

        return find(x) == find(y);

    }



    //weightsは各頂点から根までの距離

    int weight(int x){

        find(x);

        return weights[x];

    }



    int diff(int x,int y){

        return weight(y) - weight(x);

    }





};



int main(){

    int n,m;

    cin >> n >> m;

    potential_Unionfind uf(n);

    rep(i,m){

        int a,b,w;

        cin >> a >> b >> w;

        --a;--b;

        if(!uf.unite(a,b,w)){

            if(uf.diff(a,b) != w){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    

    cout << "Yes" << endl;

    return 0;



}

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

using namespace std;



using VI = vector<int>;

using VVI = vector<VI>;

using PII = pair<int, int>;

using LL = long long;

using VL = vector<LL>;

using VVL = vector<VL>;

using PLL = pair<LL, LL>;

using VS = vector<string>;



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

#define RALL(a) (a).rbegin(), (a).rend()

#define PB push_back

#define EB emplace_back

#define MP make_pair

#define SZ(a) int((a).size())

#define SORT(c) sort(ALL((c)))

#define RSORT(c) sort(RALL((c)))

#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))



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

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



#define FF first

#define SS second



#define DUMP(x) cout<<#x<<":"<<(x)<<endl

template<class S, class T>

istream& operator>>(istream& is, pair<S,T>& p){

  return is >> p.FF >> p.SS;

}

template<class T>

istream& operator>>(istream& is, vector<T>& xs){

  for(auto& x: xs)

	is >> x;

  return is;

}

template<class S, class T>

ostream& operator<<(ostream& os, const pair<S,T>& p){

  return os << p.FF << " " << p.SS;

}

template<class T>

ostream& operator<<(ostream& os, const vector<T>& xs){

  for(unsigned int i=0;i<xs.size();++i)

	os << (i?" ":"") << xs[i];

  return os;

}

template<class T>

void maxi(T& x, T y){

  if(x < y) x = y;

}

template<class T>

void mini(T& x, T y){

  if(x > y) x = y;

}





const double EPS = 1e-10;

const double PI  = acos(-1.0);

const LL MOD = 1e9+7;

const LL INF = 1e18;



bool dfs(vector<vector<PLL>>& G, int u, VL& dist){

  for(auto& e: G[u]){

	if(dist[e.FF] == INF){

	  dist[e.FF] = dist[u] + e.SS;

	  if(!dfs(G, e.FF, dist))

		return false;

	}

	else if(dist[e.FF] != dist[u] + e.SS)

	  return false;

  }

  return true;

}



int main(){

  cin.tie(0);

  ios_base::sync_with_stdio(false);



  int N, M;

  cin >> N >> M;

  vector<vector<PLL>> G(N);

  REP(i,M){

	int l, r, d;

	cin >> l >> r >> d;

	--l;

	--r;

	G[l].EB(r,d);

	G[r].EB(l,-d);

  }



  VL dist(N, INF);

  bool ok = true;

  REP(i,N){

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

	dist[i] = 0;

	ok = ok && dfs(G, i, dist);

  }



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



  return 0;

}

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

#define maxn 200010

using namespace std;

int n,m,fa[maxn],dis[maxn];

int find(int x){

	if(x!=fa[x]){

		int f=fa[x];

		fa[x]=find(fa[x]);

		dis[x]+=dis[f];	

	}

	return fa[x]; 

}

int main(){

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

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

		fa[i]=i;

	int falg=0;

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

		int x,y,z;

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

		int r1=find(x);

		int r2=find(y);

		if(r1==r2){

			if(dis[r2]+dis[y]!=z+dis[x])falg=1;

			else continue;

		}

		else {

			fa[r2]=r1;

			dis[r2]=z+dis[x]-dis[y];

		}

	}

	if(falg)printf("No\n");

	else printf("Yes\n");

	return 0; 

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <iostream>

#include <cstring>

#define maxn 2000005

using namespace std;

int Far[maxn],dis[maxn];

void inti(int n){

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

        Far[i]=i;

        dis[i]=0;

    }

}

int find_F(int x){

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

    int tmp=Far[x];

    Far[x]=find_F(Far[x]);

    dis[x]+=dis[tmp];

    return Far[x];

}

int find_D(int x){

    if(Far[x]==x) return 0;

    else return find_D(Far[x])+dis[x];

}

int main()

{

    int n,m;

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

    inti(n);

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

        int a,b,c;

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

        int Fa=find_F(a);

        int Fb=find_F(b);

        if(Fa==Fb){

            if(dis[b]-dis[a]!=c){

                puts("No");

                return 0;

            }

        }

        else{

            Far[Fb]=Fa;

            dis[Fb]=find_D(a)-find_D(b)+c;

        }

    }

    puts("Yes");

}


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

#define maxn 200010

using namespace std;

int n,m,fa[maxn],dis[maxn];

int find(int x){

	if(x!=fa[x]){

		int f=fa[x];

		fa[x]=find(fa[x]);

		dis[x]+=dis[f];	

	}

	return fa[x]; 

}

int main(){

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

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

		fa[i]=i;

	int falg=0;

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

		int x,y,z;

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

		int r1=find(x);

		int r2=find(y);

		if(r1==r2){

			if(dis[r2]+dis[y]!=z+dis[x])falg=1;

			else continue;

		}

		else {

			fa[r2]=r1;

			dis[r2]=z+dis[x]-dis[y];

		}

	}

	if(falg)printf("No\n");

	else printf("Yes\n");

	return 0; 

}

// Fastest known solution for this problem: