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

#include<cstdio>

#include<cstring>

#include<iostream>

#include<algorithm>

using namespace std;

const int maxn=1e5+1000;

struct node{

	int v,nxt,c;

}edge[maxn<<2];

int head[maxn],tot;

int vis[maxn],dist[maxn];

int n,m,len=0;

inline void read(int &x){

	x=0;char tmp=getchar();

	while(tmp<'0'||tmp>'9') tmp=getchar();

	while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();

}

inline void add_edge(int x,int y,int c){

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

	edge[tot].v=y;

	edge[tot].c=c;

	head[x]=tot++;

}

inline bool spfa(int x){

	int maxx=0,minn=0;

	dist[x]=0,vis[x]=1;

	queue<int>q;

	q.push(x);

	while(!q.empty()){

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

		for(int i=head[u];i!=-1;i=edge[i].nxt){

			int v=edge[i].v;

			if(!vis[v]){

				dist[v]=dist[u]+edge[i].c;

				maxx=max(dist[v],maxx);

				minn=min(dist[v],minn);

				vis[v]=1;

				q.push(v);

			}

			else if(dist[v]!=dist[u]+edge[i].c)

				return false;

		}

	}

	len=max(len,maxx-minn);

	if(len>1e9) return false;

	return true;

}

int main(){

	memset(head,-1,sizeof(head));

	memset(vis,0,sizeof(vis));

	cin>>n>>m;

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

		int l,r,d;

		read(l),read(r),read(d);

		add_edge(l,r,d);

		add_edge(r,l,-d);

	}

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

		if(!vis[i]){

			if(!spfa(i)){

				printf("No\n");

				return 0;

			}

		}

	}

	printf("Yes\n");

	return 0;

}

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

#define ll long long

#define inf 0x3f3f3f3f

#define il inline

 

namespace io {

 

#define in(a) a = read()

#define out(a) write(a)

#define outn(a) out(a), putchar('\n')

 

#define I_int ll

inline I_int read() {

    I_int x = 0, f = 1;

    char c = getchar();

    while (c < '0' || c > '9') {

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

        c = getchar();

    }

    while (c >= '0' && c <= '9') {

        x = x * 10 + c - '0';

        c = getchar();

    }

    return x * f;

}

char F[200];

inline void write(I_int x) {

    if (x == 0) return (void) (putchar('0'));

    I_int tmp = x > 0 ? x : -x;

    if (x < 0) putchar('-');

    int cnt = 0;

    while (tmp > 0) {

        F[cnt++] = tmp % 10 + '0';

        tmp /= 10;

    }

    while (cnt > 0) putchar(F[--cnt]);

}

#undef I_int

 

}

using namespace io;

 

using namespace std;

 

#define N 200010

 

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

struct edge {

	int to, nxt, v;

}e[N<<1];

int head[N], cnt, d[N], vis[N];

 

int flag = 0;

void dfs(int u) {

	vis[u] = 1;

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

		if(vis[e[i].to]) {

			if(d[e[i].to] == d[u] + e[i].v) continue;

			else {

				flag = 1;

				return;

			}

		} else {

			d[e[i].to] = d[u] + e[i].v;

			dfs(e[i].to);

		}

	}

}



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

	e[++cnt] = {v, head[u], w};

	head[u] = cnt;

}

 

int main() {

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

    	int l = read(), r = read(), t = read();

    	ins(l, r, t); ins(r, l, -t);

	}

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

	if(flag) puts("No");

	else puts("Yes");

}

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

int INF = (1LL << 30) - 1;

int MOD = 1e9+7;

int N,M;

vector<P> graph[200010];

vector<int> dist;

bool dfs(int now, int d){

    //cout << " " << now << endl;

    dist[now] = d;

    for(auto p:graph[now]){

        if(dist[p.first] != INF){

            if(dist[p.first] != d + p.second)return true;

            continue;

        }

        if(dfs(p.first, d + p.second))return true;

    }

    return false;

}

main(){ 

    cin >> N >> M;

    int num = 0;

    rep(i,0,M){

        int a,b,c;

        cin >> a >> b >> c;

        a--,b--;

        graph[a].emplace_back(b,c);

        graph[b].emplace_back(a,-c);

        num = a;

    }

    dist = vector<int>(N,INF);

    rep(i,0,N){

        int cost = dist[i];

        if(cost == INF)cost = 0;

        if(dist[i] == INF && dfs(i, 0) ){

            cout << "No" << endl;

            //cout << i << endl;

            return 0;

        }

        //for(auto i:dist)cout << i << " ";cout << endl;

    }

    cout << "Yes" << endl;

}

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

int INF = (1LL << 30) - 1;

int MOD = 1e9+7;

vector<P> graph[100010];

vector<ll> dist;

bool dfs(int x, int d){

    dist[x] = d;

    for(auto p:graph[x]){

        if(dist[p.first] != INF){

            if(dist[p.first] != d + p.second){

                return true;

            }else continue;

        }

        if(dfs(p.first, d + p.second))return true;

    }

    return false;

}

main(){

    int N,M;

    cin >> N >> M;

    rep(i,0,M){

        int l,r,d;

        cin >> l >> r >> d;

        l--,r--;

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

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

    }

    dist.resize(N, INF);

    rep(i,0,N){

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

        if(dfs(i, 0)){

            //cout << i << endl;

            cout << "No" << endl;

            return 0;

        }

    }

    cout << "Yes" << endl;

}

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

using namespace std;

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

#define INF 2e9+10

typedef pair<int,int> edge;



vector<edge> g[100010];

bool check[100010];

int p[100010];



bool solve(int v){

    if(abs(p[v]) > int(1e9)) return false;

    for(edge e : g[v]){

        int to = e.first;

        int d = e.second;

        if(p[to] == INF){

            p[to] = p[v] + d;

            if(!solve(to)) return false;

        }else if(p[v] + d != p[to]){

            return false;

        }

    }

    return true;

}



int main(void){

    int n,m;

    cin>>n>>m;

    rep(_,m){

        int l,r,d;

        cin>>l>>r>>d;

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

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

    }



    rep(i,100010){

        p[i] = INF;

    }



    rep(i,n){

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

        p[i] = 0;

        if(!solve(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;

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

#define INF 1e9

typedef pair<int,int> edge;



vector<vector<edge>> g;

vector<int> p;



bool solve(int v){

    if(abs(p[v]) > INF) return false;

    for(edge e : g[v]){

        int to = e.first;

        int d = e.second;

        if(p[to] == INF){

            p[to] = p[v] + d;

            if(!solve(to)) return false;

        }else if(p[v] + d != p[to]){

            return false;

        }

    }

    return true;

}



int main(void){

    int n,m;

    cin>>n>>m;

    g.resize(n);

    p.resize(n,INF);

    rep(_,m){

        int l,r,d;

        cin>>l>>r>>d;

        l--,r--;

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

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

    }



    rep(i,n){

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

        p[i] = 0;

        if(!solve(i)){

            cout<<"No"<<endl;

            return 0;

        }

    }

    cout<<"Yes"<<endl;

    return 0;

}


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

#include <algorithm>

#include <vector>

#include <utility>



typedef long long ll;

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

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

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

using namespace std;



vector<pair<int, int>> G[100001];

int dist[100001];

bool used[100001];



bool dfs(int x, int d) {

    if (used[x]) return d == dist[x];



    dist[x] = d;

    used[x] = true;

    for (auto p : G[x]) {

        if (!dfs(p.first, d + p.second))

            return false;

    }

    return true;

}



int main() {

    int N, M, L, R, D;

    cin >> N >> M;

    REP(i, M) {

        cin >> L >> R >> D;

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

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

    }



    FOR(i, 1, N + 1) {

        if (!used[i] && !dfs(i, 0)) {

            cout << "No" << endl;

            return 0;

        }

    }



    cout << "Yes" << endl;

    return 0;

}

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

#include <algorithm>

#include <utility>

#include <vector>

#include <string>



using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

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

#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)

#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 ALL(a) (a).begin(), (a).end()



class UnionFind {

private:

    int n;

    vector<pii> par;

public:

    UnionFind(int n) : n(n), par(n, {-1, 0}) {}



    pii root(int x) {

        if (par[x].first < 0) return {x, par[x].second};



        pii pp = par[x];

        pii rp = root(par[x].first);

        return par[x] = {rp.first, pp.second + rp.second};

    }



    bool same(int x, int y) { return root(x).first == root(y).first; }



    bool unite(int l, int r, int d) {

        pii lp = root(l), rp = root(r);

        if (lp.first != rp.first) {

            par[lp.first] = {par[lp.first].first + par[rp.first].first, 0};

            par[rp.first] = {lp.first, d + lp.second - rp.second};

        }

        else {

            return rp.second - lp.second == d;

        }

        return true;

    }



    int set_size(int x) { return -par[root(x).first].first; }



    int size() const { return n; }

};



int main() {

    int N, M, L, R, D;

    cin >> N >> M;



    UnionFind uf(N);

    bool ok = true;

    REP(i, M) {

        cin >> L >> R >> D;

        ok &= uf.unite(L - 1, R - 1, D);

    }



    if (ok) cout << "Yes" << endl;

    else    cout << "No" << endl;

    return 0;

}

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



using namespace std;



inline void Boost() {

	ios::sync_with_stdio(false);

	cin.tie(NULL); cout.tie(NULL);

}

typedef long long int ll;

typedef long double ld;



const int NMax = 1e5 + 50;

const int LIM = 1e9;



struct op {

	int l, r, d;

};



int in[NMax];

int pos[NMax];

op v[2 * NMax];

vector < pair < int, int > > G[NMax];

vector < pair < int, int > > revG[NMax];



void SolveG(int node);

void SolveRev(int node);



void SolveG(int node) {

	for(auto it: G[node]) {

		if(pos[it.first] == -1) {

			pos[it.first] = pos[node] + it.second;

			SolveRev(it.first); 

			SolveG(it.first);

		}

	}

}

void SolveRev(int node) {

	for(auto it: revG[node]) {

		if(pos[it.first] == -1) {

			pos[it.first] = pos[node] + it.second;

			SolveG(it.first); 

			SolveRev(it.first);

		}

	}

}



int main() {

	Boost();



	int n, m;

	cin >> n >> m;



	for(int i = 1; i <= n; ++i) pos[i] = -1;



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

		cin >> v[i].l >> v[i].r >> v[i].d;

		G[v[i].l].push_back({v[i].r, v[i].d});

		revG[v[i].r].push_back({v[i].l, -v[i].d});

		in[v[i].r]++;

	}



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

		if(in[i] == 0 && pos[i] == -1) {

			if(pos[i] == -1) pos[i] = LIM;

			SolveG(i);

		}

	}



	// for(int i = 1; i <= n; ++i) cout << pos[i] << " ";

	// cout << endl;



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

		if(pos[v[i].r] - pos[v[i].l] != v[i].d) {

			cout << "No";

			exit(0);

		}

	}



	cout << "Yes";

	return 0;

}

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



using namespace std;



inline void Boost() {

	ios::sync_with_stdio(false);

	cin.tie(NULL); cout.tie(NULL);

}

typedef long long int ll;

typedef long double ld;



const int NMax = 1e5 + 50;

const int LIM = 1e9;



struct op {

	int l, r, d;

};



int pos[NMax];

op v[2 * NMax];

vector < pair < int, int > > G[NMax];



int main() {

	Boost();



	int n, m;

	cin >> n >> m;



	for(int i = 1; i <= n; ++i) pos[i] = -1;



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

		cin >> v[i].l >> v[i].r >> v[i].d;

		G[v[i].l].push_back({v[i].r, v[i].d});

		G[v[i].r].push_back({v[i].l, -v[i].d});

	}



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

		if(pos[i] != -1) continue;

		queue < int > Q;

		Q.push(i);



		while(!Q.empty()) {

			int node = Q.front(); Q.pop();

			if(pos[node] == -1) pos[node] = LIM;



			for(auto it: G[node]) {

				if(pos[it.first] == -1) {

					pos[it.first] = pos[node] + it.second;

					Q.push(it.first);

				}

			}

		}

	}



	// for(int i = 1; i <= n; ++i) cout << pos[i] << " ";

	// cout << endl;



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

		if(pos[v[i].r] - pos[v[i].l] != v[i].d) {

			cout << "No";

			exit(0);

		}

	}



	cout << "Yes";

	return 0;

}

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

#define LL long long

#define mp make_pair

#define pb push_back

#define INF (1<<29)

using namespace std;

typedef pair<int,int>pii;

int n,m;

vector<pii>G[200010];

int col[200010];

int vis[200010];

bool dfs(int x,int co){

//	cout<<"In:"<<x<<' '<<co<<endl;

	if(vis[x]!=-1)return col[x]==co;

	vis[x]=1;

	col[x]=co;

	for(pii i:G[x]){

		if(dfs(i.first,co+i.second)==0)return false;

//		else{

//			if(vis[i.first]!=co+i.second)return false;

//		}

	}

	return true;

}

int main(){

//	freopen("produce/People.txt","r",stdin);

	cin>>n>>m;

	memset(vis,-1,sizeof(vis));

	if(m==0){

		cout<<"Yes\n";

		return 0;

	}

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

		int l,r,d;

		cin>>l>>r>>d;

		G[l].pb(mp(r,d));

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

	}

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

		if(vis[i]!=-1)continue;

		memset(col,0,sizeof(col));

		if(dfs(i,0)==0){

			cout<<"No\n";

			return 0;

		}

//		cout<<"-----------------------\n";

	}

	cout<<"Yes\n";

	return 0;

}


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

#include <string>

#include <cstdio>

#include <cmath>

#include <cstring>

#define max(x,y) x > y? x: y

using namespace std;



const int maxn = 1011111;

int father[maxn];

int gradfa[maxn];



int find(int x)

{

    if(father[x] == x)

        return x;



    int r = find(father[x]);

    gradfa[x] = gradfa[father[x]] + gradfa[x];



    return father[x] = r;

}



int fix(int x,int y,int z)

{

    int fx = find(x);

    int fy = find(y);

    if(fx ==  fy)

        return z == gradfa[y] - gradfa[x];

    father[fy] = fx;

    gradfa[fy] = gradfa[x] + z - gradfa[y];

    return 1;

}



int main()

{

    int n, m;

    int lose = 1;

    cin >> n >> m;

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

        father[i] = i;

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

    {

        int x, y, z;

        cin >> x >> y >> z;



        if(lose)

            lose = fix(x, y, z);

    }



    if(lose)

        cout << "Yes" << endl;

    else

        cout << "No" << endl;

    return 0;

}


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

#include <queue>

#include <set>

#include <vector>

using namespace std;



int N;

int M;



int L[200010];

int R[200010];

int D[200010];



typedef pair<int,int> P;



vector<P> edges[100010];

set<int> st;



int dist[100010];

int visited[100010];



int main()

{

	fill(dist,dist + 100010,-1e9);



	cin >> N >> M;



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

	{

		cin >> L[i] >> R[i] >> D[i];

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

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

		st.insert(L[i]);

		st.insert(R[i]);

	}



	int s = -1;



	while(!st.empty())

	{

		auto ite = st.begin();

	

		{

			s = *ite;



			queue<P> que;

			que.push({s,0});

			dist[s] = 0;



			while(!que.empty())

			{

				int v = que.front().first;

				int d = que.front().second;

				que.pop();



				st.erase(v);

				for(P& p : edges[v]) {

					if(dist[p.first] == -1e9){

						dist[p.first] = d + p.second;

						que.push(P(p.first,d + p.second));

					}

					else if(dist[p.first] != d + p.second)

					{

						cout << "No" << endl;

						return 0;

					}



				}

				

			}

			

		}

	}



	cout << "Yes" << endl;

	return 0;

}




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

using namespace std;

using i64 = long long;

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



#include <vector>

using namespace std;

using i64 = long long;



struct WeightedUnionFind {

  vector<int> par;

  vector<i64> ws;



  WeightedUnionFind(int n) : par(n, 0), ws(n, 0) {

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

      par[i] = -1;

    }

  }



  int find(int x) {

    if (par[x] < 0) {

      return x;

    } else {

      const int parent = find(par[x]);

      ws[x] += ws[par[x]];

      return par[x] = parent;

    }

  }



  int weight(int x) {

    find(x);

    return ws[x];

  }



  bool unite(int x, int y, int w)  // x + w = y

  {

    w += weight(x);

    w -= weight(y);

    x = find(x);

    y = find(y);



    if (x != y) {

      if (par[y] < par[x]) {

        swap(x, y);

        w = -w;

      }

      par[x] += par[y];

      par[y] = x;

      ws[y] = w;

      return true;

    } else {

      return false;

    }

  }



  bool same(int x, int y) { return find(x) == find(y); }



  pair<bool, int> diff(int x, int y) {

    if (!same(x, y)) return {false, 0};

    return {true, weight(x) - weight(y)};

  }

};



int N;

int M;



int main(){

  cin >> N;

  cin >> M;



  WeightedUnionFind uf(N);

  

  rep(i,0,M - 1){

    int l,r,d;

    cin >> l >> r >> d;

    l--;

    r--;

    auto p = uf.diff(r,l);

    if(!p.first){

      uf.unite(l,r,d);

    }

    else if(p.second != d){

      cout << "No" << endl;

      return 0;

    }

  }



  cout << "Yes" << endl;

}


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

#include<cstring>

#include<algorithm>

#include<vector>

#include<iostream>



using namespace std;



#define   M             100005

#define   ll            long long

#define   PI            atan(1.0)*4

#define   mst(x,v)      memset(x,v,sizeof(x))

#define   inf           1e9

#define   rush()        int T;scanf("%d",&T);while(T--)



typedef   pair<int,int> P;



vector< P > E[M];

P temp;

int d[M],n,m,l,r,dis;

bool  vis[M],f;



bool dfs(int now,int bef) {

	vis[now]=1;

	if(f) return false;

	

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

		temp=E[now][i];

		

		if(!vis[temp.first]) {

			d[temp.first]=d[now]+temp.second;

			dfs(temp.first,now);

		}

		else {

			if(d[temp.first] != d[now] + temp.second) {

				f=1;

				return false;

			}

		}

	}

	return true;

}





int main() {



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

	

	while(m--) {

		scanf("%d %d %d",&l,&r,&dis);

		E[l].push_back(make_pair(r,dis));

		E[r].push_back(make_pair(l,-dis));

	}

	

	f=0;

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

		if(!vis[i]) {

			if(!dfs(i,-1)) {

				f=1;

				break;

			}

		}

	}

	

	puts(f?"No":"Yes");

}




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

using namespace std;



#define   M             100005

#define   ll            long long

#define   PI            atan(1.0)*4

#define   mst(x,v)      memset(x,v,sizeof(x))

#define   inf           1e9

#define   rush()        int T;scanf("%d",&T);while(T--)



typedef   pair<int,int> P;



int n,m,l,r,dis,flag;

int f[M],d[M];



int ff(int x) {

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

	int a=f[x];

	int b=ff(a);

	

	d[x]+=d[a];

	f[x]=b;

	

	return b;

}





//          bi - ai = dis              a 接在 b下面 

int mix(int a,int b,int dis) {

	int fa=ff(a);

	int fb=ff(b);

	

	if(fa==fb)

		return d[b]-d[a]==dis;

	

	f[fb]=fa; // fb 的父亲变成了 fa

	d[fb]=d[a] - d[b] + dis;

	

	return 1;

}







int main() {



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

	flag=0;

	

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

	

	while(m--) {

		scanf("%d %d %d",&l,&r,&dis);

		if(!mix(l,r,dis))

			flag=1;

	}

	

	puts(flag?"No":"Yes");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

   给出  n 个人 ， m 句话

   

   每句话说 ：给你 l , r , dis 表示 l 在 r 左边的 dis 个距离单位

   

   （里面有假话，有真话）

    

   问 ： 是否上面都是真话

   

 ----------------------------------------------------------------------  

   题解：   

      假设 n 个人 为 n 个点， 然后距离看做 权值，

	  

	  作为一个单向图进行dfs 判断

	  

	  

	  从 1 到 N 每个点进行判断 

	  

	  如果某个值没被访问 ， 以这个点进行 一次深搜，判断是否冲突

	  

	  dfs内容 : 进行标记 访问过 表示已经存在了距离，然后看 能通往这个地方的一些人 （节点）

	  

	  如果这个点被访问过那么通往他的值都会被确定 只要验证

	  

	  如果这个点没被访问，就奖通完他的地方的值进行赋值 ，继续搜索能通往这个没被访问过的节点， 

	  

*/





#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

#include<iostream>



using namespace std;



#define   M             100005

#define   ll            long long

#define   PI            atan(1.0)*4

#define   mst(x,v)      memset(x,v,sizeof(x))

#define   inf           1e9

#define   rush()        int T;scanf("%d",&T);while(T--)



typedef   pair<int,int> P;



vector< P > E[M];

P temp;

int d[M],n,m,l,r,dis;

bool  vis[M],f;



bool dfs(int now) {

	vis[now]=1;

	if(f) return false;

	

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

		temp=E[now][i];

		

		if(!vis[temp.first]) {

			d[temp.first]=d[now]+temp.second;

			dfs(temp.first);

		}

		else {

			if(d[temp.first] != d[now] + temp.second) {

				f=1;

				return false;

			}

		}

	}

	return true;

}





int main() {



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

	

	while(m--) {

		scanf("%d %d %d",&l,&r,&dis);

		E[l].push_back(make_pair(r,dis));

		E[r].push_back(make_pair(l,-dis));

	}

	

	f=0;

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

		if(!vis[i]) {

			if(!dfs(i)) {

				f=1;

				break;

			}

		}

	}

	

	puts(f?"No":"Yes");

}




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

using namespace std;



#define   M             100005

#define   ll            long long

#define   PI            atan(1.0)*4

#define   mst(x,v)      memset(x,v,sizeof(x))

#define   inf           1e9

#define   rush()        int T;scanf("%d",&T);while(T--)



typedef   pair<int,int> P;



int n,m,l,r,dis,flag;

int f[M],d[M];



int ff(int x) {

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

	int a=f[x];

	int b=ff(a);

	

	d[x]+=d[a];

	f[x]=b;

	

	return b;

}





//          bi - ai = dis              a 接在 b下面 

int mix(int a,int b,int dis) {

	int fa=ff(a);

	int fb=ff(b);

	

	if(fa==fb)

		return d[b]-d[a]==dis;

	

	f[fb]=fa; // fb 的父亲变成了 fa

	d[fb]=d[a] - d[b] + dis;

	

	return 1;

}







int main() {



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

	flag=0;

	

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

	

	while(m--) {

		scanf("%d %d %d",&l,&r,&dis);

		if(!mix(l,r,dis))

			flag=1;

	}

	

	puts(flag?"No":"Yes");

}




// Fastest known solution for this problem: