stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define int long long

#define ld long double

#define pb push_back

#define mp make_pair

#define eb emplace_back

#define fi first

#define se second

#define lb lower_bound

#define ub upper_bound

const int mod = 1e9+7;

const int mx = INT_MAX;

const long double pi = 4*atan((long double)1);

typedef vector<int> vi;

typedef pair<int,int> pii;

typedef vector<pii> vii;

#define file "orz"

const int mxN = 2e5+5;

vii G[mxN];

bool visited[mxN]; 

int dist[mxN];	

int n,m;

void dfs(int u)

{

	visited[u] = true;

	for(auto v:G[u])

	{

		if(visited[v.fi])

			continue;

		dist[v.fi]=dist[u]+v.se;

		dfs(v.fi);

	}

}

vector<pair<pii,int>> check;

signed main()

{

    //freopen(file".inp","r",stdin);

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

    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin>>n>>m;

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

	{

		int u,v,w;

		cin>>u>>v>>w;

		G[u].eb(v,w);

		G[v].eb(u,-w);

		check.eb(mp(mp(u,v),w));

	}

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

		if(!visited[i])

			dfs(i);

	for(auto u:check)

		if(dist[u.fi.se]!=dist[u.fi.fi]+u.se)

			return cout<<"No",0;

	cout<<"Yes";

}


// Faster C++ solution for the same problem (measured speedup: 6.419349248060334x):
#include <bits/stdc++.h>

using namespace std;

int main(){

  int N, M;

  cin >> N >> M;

  vector<vector<pair<int, int>>> E(N);

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

    int L, R, D;

    cin >> L >> R >> D;

    L--;

    R--;

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

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

  }

  vector<int> d(N);

  vector<int> comp(N, -1);

  bool ok = true;

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

    if (comp[i] == -1){

      comp[i] = i;

      d[i] = 0;

      queue<int> Q;

      Q.push(i);

      while (!Q.empty()){

        int v = Q.front();

        Q.pop();

        for (auto P : E[v]){

          int dist = P.first;

          int w = P.second;

          if (comp[w] != -1){

            if (d[w] != d[v] + dist){

              ok = false;

            }

          } else {

            comp[w] = i;

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

            Q.push(w);

          }

        }

      }

    }

  }

  if (ok){

    cout << "Yes" << endl;

  } else {

    cout << "No" << endl;

  }

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define _for(i,j,N) for(int i = (j); i < (N);i++)

#define LL long long

#define MAXN 100005



struct P{

    LL index;

    LL dis;

    P(LL a = 0,LL b = 0):index(a),dis(b){;}

};



P line[MAXN];



LL root(LL &a){

    LL ret_val = 0;

    while(line[a].index != a){

        ret_val += line[a].dis;

        a = line[a].index;

    }

    return ret_val;

}



int main()

{

    int N,M;

    cin >> N >> M;

    _for(i,1,N+1){

        line[i] = P(i,0);

    }

    bool failed = false;

    _for(i,0,M){

        LL l,r,d;

        cin >> l >> r >> d;

        LL d1 = root(l);

        LL d2 = root(r);

        if(l != r){

            if(d1 + d > d2){

                line[r].index = l;

                line[r].dis = d1+d-d2;

            }

            else{

                line[l].index = r;

                line[l].dis = d2-d1-d;

            }

        }

        else if(l == r){

            if(d2 - d1 != d){

                failed = true;

                break;

            }

        }

    }

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

    else printf("Yes\n");

}


// Faster C++ solution for the same problem (measured speedup: 1.8516856071088275x):
#include <bits/stdc++.h>

using namespace std;

#define _for(i,j,N) for(int i = (j);i < (N);i++)

#define _rep(i,j,N) for(int i = (j);i <= (N);i++)

#define _dec(i,j,N) for(int i = (N-1);i >= (j);i--)

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

#define PB push_back

#define LL long long



template<typename T>

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

    _for(i,0,v.size()) os << v[i] << " ";

    return os;

}



template<typename T>

ostream& operator<<(ostream& os,const set<T>& v){

    for(typename set<T>::iterator it = v.begin();it != v.end();it++)

    os << *it <<" ";

    return os;

}



const int maxn = 100005;

int f[maxn];

LL d[maxn];

int N,M;



int finds(int x,LL &temp){

    if(f[x] != x){

        int root = finds(f[x],temp);

        temp += d[x];

        d[x] = temp;

        f[x] = root;

        return root;

    }

    else{

        temp = 0;

        return x;

    }

}





int main()

{

    cin >> N >> M;

    _for(i,0,maxn) {

        f[i] = i;

    }

    int ok = 1;



    //cout << f[1] << endl;



    _for(i,0,M){

        int l,r,dd;

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

        LL ll = 0,rr = 0;

        int rootr = finds(r,rr);

        int rootl = finds(l,ll);

        //cout << rootl << " "<<rootr << endl;

        if(rootr == rootl){

            if(d[r] - d[l] != dd) {ok = 0;break;}

        }

        else{

            LL d1 = d[l];

            LL d2 = d[r];

            if(d1+dd < d2){

                d[rootl] = d2 - dd - d1;

                f[rootl] = rootr;

            }

            else{

                d[rootr] = dd + d1 -d2;

                f[rootr] = rootl;

            }

        }

    }





    //_for(i,0,4){

        //cout << f[i] << " "<<d[i] << endl;

    //}





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

    else cout <<"No"<<endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

using namespace std;

 

#define _for(i,j,N) for(int i = (j); i < (N);i++)

#define LL long long

#define MAXN 100005

 

struct P{

    LL index;

    LL dis;

    P(LL a = 0,LL b = 0):index(a),dis(b){;}

};

 

P line[MAXN];

 

LL root(LL &a){

    LL ret_val = 0;

    while(line[a].index != a){

        ret_val += line[a].dis;

        a = line[a].index;

    }

    return ret_val;

}

 

int main()

{

    int N,M;

    cin >> N >> M;

    _for(i,1,N+1){

        line[i] = P(i,0);

    }

    bool failed = false;

    _for(i,0,M){

        LL l,r,d;

        cin >> l >> r >> d;

        LL d1 = root(l);

        LL d2 = root(r);

        if(l != r){

            if(d1 + d > d2){

                line[r].index = l;

                line[r].dis = d1+d-d2;

            }

            else{

                line[l].index = r;

                line[l].dis = d2-d1-d;

            }

        }

        else if(l == r){

            if(d2 - d1 != d){

                failed = true;

                break;

            }

        }

    }

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

    else printf("Yes\n");

}

// Faster C++ solution for the same problem (measured speedup: 1.8516856071088275x):
#include <bits/stdc++.h>

using namespace std;

#define _for(i,j,N) for(int i = (j);i < (N);i++)

#define _rep(i,j,N) for(int i = (j);i <= (N);i++)

#define _dec(i,j,N) for(int i = (N-1);i >= (j);i--)

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

#define PB push_back

#define LL long long



template<typename T>

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

    _for(i,0,v.size()) os << v[i] << " ";

    return os;

}



template<typename T>

ostream& operator<<(ostream& os,const set<T>& v){

    for(typename set<T>::iterator it = v.begin();it != v.end();it++)

    os << *it <<" ";

    return os;

}



const int maxn = 100005;

int f[maxn];

LL d[maxn];

int N,M;



int finds(int x,LL &temp){

    if(f[x] != x){

        int root = finds(f[x],temp);

        temp += d[x];

        d[x] = temp;

        f[x] = root;

        return root;

    }

    else{

        temp = 0;

        return x;

    }

}





int main()

{

    cin >> N >> M;

    _for(i,0,maxn) {

        f[i] = i;

    }

    int ok = 1;



    //cout << f[1] << endl;



    _for(i,0,M){

        int l,r,dd;

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

        LL ll = 0,rr = 0;

        int rootr = finds(r,rr);

        int rootl = finds(l,ll);

        //cout << rootl << " "<<rootr << endl;

        if(rootr == rootl){

            if(d[r] - d[l] != dd) {ok = 0;break;}

        }

        else{

            LL d1 = d[l];

            LL d2 = d[r];

            if(d1+dd < d2){

                d[rootl] = d2 - dd - d1;

                f[rootl] = rootr;

            }

            else{

                d[rootr] = dd + d1 -d2;

                f[rootr] = rootl;

            }

        }

    }





    //_for(i,0,4){

        //cout << f[i] << " "<<d[i] << endl;

    //}





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

    else cout <<"No"<<endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

struct data{int to,next,wi;}e[maxn*2];

int head[maxn];

int cnt=0;

int du[maxn];

queue<int> q;

bool vis[maxn];

void ins(int u,int v,int ww)

{

	e[++cnt].to=v;

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

	e[cnt].wi=ww;

	head[u]=cnt;

}

int n,m;

int dis[maxn];

int main()

{

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

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

	{

		int x,y,z;

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

		ins(x,y,z);

		du[y]++;

	}

	for(int i=1;i<=n;i++)dis[i]=1e9+10;

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

	{

		if(du[s]==0)

		{

			dis[s]=0;

			vis[s]=1;

			q.push(s);

			while(!q.empty())

			{

				int x=q.front();

				q.pop();

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

				{

					int u=e[i].to;

					if(vis[u]==1&&dis[u]!=dis[x]+e[i].wi)

					{

						puts("No");

						return 0;

					}

					if(vis[u]==0)

					{

						vis[u]=1;

						dis[u]=min(dis[u],dis[x]+e[i].wi);

						q.push(u);

					}

				}

			}

		}

	}

	for(int i=1;i<=n;i++)if(vis[i]==0){puts("No");return 0;}

	puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.248952527869911x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;

struct data{int to,next,wi;}e[maxn*2];

int head[maxn];

int cnt=0;

int du[maxn];

queue<int> q;

int vis[maxn];

void ins(int u,int v,int ww)

{

	e[++cnt].to=v;

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

	e[cnt].wi=ww;

	head[u]=cnt;

}

int n,m;

int dis[maxn];

int main()

{

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

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

	{

		int x,y,z;

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

		ins(x,y,z);

		du[y]++;

	}

	for(int i=1;i<=n;i++)dis[i]=1e9+10;

	int tot=0;

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

	{

		if(du[s]==0)

		{

			tot++;

			dis[s]=0;

			vis[s]=tot;

			q.push(s);

			while(!q.empty())

			{

				int x=q.front();

				q.pop();

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

				{

					int u=e[i].to;

					if(vis[u]==tot&&dis[u]!=dis[x]+e[i].wi)

					{

						puts("No");

						return 0;

					}

					if(vis[u]==0)

					{

						vis[u]=tot;

						dis[u]=min(dis[u],dis[x]+e[i].wi);

						q.push(u);

					}

				}

			}

		}

	}

	for(int i=1;i<=n;i++)if(vis[i]==0){puts("No");return 0;}

	puts("Yes");

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.248679691060244x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+5,maxm=2e5+5;

int fa[maxn],d[maxn],n,m,x,y,z;

inline int get(int u)

{

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

	int f=get(fa[u]);

	d[u]+=d[fa[u]];

	return fa[u]=f;

}



int main()

{

	while(~scanf("%d%d",&n,&m))

	{

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

		bool ok=1;

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

		{

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

			if(!ok)continue;

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

			if(fx==fy)

			{

				if(d[y]-d[x]!=z)ok=0;

			}

			else

			{

				fa[fy]=fx;

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

			}

		}

		printf("%s\n",ok?"Yes":"No");

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.254746991789824x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;

int per[100010];

long long dis[100010];

int flag;

long long maxn;

inline int find(int x)

{

	if(x == per[x])

		return x;

	else

	{

		int x1 = per[x];

		int x2 = find(x1);

		dis[x] = dis[x] + dis[x1];

		return per[x] = x2;

	}

}

inline void merge(int x,int y,int d1)

{

	int fx = find(x);

	int fy = find(y);

	if(fx != fy)

	{

		if(dis[x] + d1 - dis[y] > 0)

		{

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

			per[fy] = fx;

		}

		else

		{

			dis[fx] = dis[y] - d1 - dis[x];

			per[fx] = fy;

		}

		if(dis[fy] > maxn)

			maxn = dis[fy];

		if(dis[fx] > maxn)

			maxn = dis[fx];

	}

	else

	{

		if(dis[y] - dis[x] != d1)

			flag = 0;

	}

}

int main()

{

	int n,m;

	while(cin >>n >> m)

	{

		int x1,x2,d;

		flag = 1;

		maxn = 0;

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

		{

			per[i] = i;

			dis[i] = 0;

		}

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

		{

			cin >> x1 >> x2 >> d;

			merge(x1,x2,d);

		}

		if(maxn > 1e+9)

			flag = 0;

		if(flag)

			cout << "Yes" << endl;

		else

			cout << "No" << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.280862636252491x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>

#include<cstdio>

#include<set>

#include<map>

#include<queue>

#include<vector>

#include<algorithm>

using namespace std;

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

#define drep(x,a,b) for(int x=a;x>=b;x--)

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

#define wash(x,y) memset(x,y,sizeof(x));



const int maxn=1e5+5;

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



//并查集初始化

//！！！注意i的起点

void init(){

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

		fa[i]=i;

		dd[i]=0;

	}

}

//并查集路径压缩寻找根节点

int find(int x){

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

	else{

		int tmp=fa[x];

		fa[x]=find(tmp);

		dd[x]=dd[x]+dd[tmp];

		return fa[x];

	}

}

//并查集合并

bool merge(int x,int y,int d){

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

	if(fx==fy) return (dd[y]-dd[x]==d);

	else{

		fa[fx]=fy;

		dd[fx]=dd[y]-dd[x]-d;

		return true;

	}

}



int main(){

	int l,r,d;

	bool flag=true;

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

	init();

	rep(i,1,m){

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

		if(flag&&!merge(l,r,d)) flag=false;

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 5.205910677652896x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
#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 f[maxn];  

int d[maxn];  

int find(int x)  

{  

    if(f[x]==x)  

        return x;  

  

    int r=find(f[x]);  

    d[x]=d[f[x]]+d[x];//当前x到她老祖宗的距离等于他现在的距离加上她爸爸到老祖宗的距离  

  

    return f[x]=r; //把每个子节点合并到他的祖先  

}  

  

  

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

{  

    int fx=find(x);  

    int fy=find(y);  

    if(fx==fy)  

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

    f[fy]=fx;//把x当作y的爸爸；  

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

    return 1;  

}  

  

  

  

  

  

int main()  

{  

    int n,m;  

    int lose=1;  

    cin>>n>>m;  

    for(int i=1;i<=n;i++)//初始化：先把所有点的爸爸都设为自己；  

        f[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;  

}  


// Faster C++ solution for the same problem (measured speedup: 5.276848248166463x):
#include<stdio.h>

#define MAXN 101000

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

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

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();

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

	{

		int x, y, z;

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}
// Slower C++ solution:
//状态:

//题号:

#include <iostream>

#include <stdio.h>

#include <fstream>

#include <algorithm>

#include <ctime>

#include <cstdlib>

#include <cstring>

#include <vector>

#include <set>

#include <string>

#include <queue>

#include <map>

#include <iomanip>

#include <cassert>

#include <complex>

#include <functional>

#include <list>

using namespace std;

typedef pair<int,int> pii;

const int INF=1e9+7;

int dp[100005];

int n,m;

vector<pii> nei[100005];

int main(){

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

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

		int a,b,c;

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

		a--;

		b--;

		nei[a].push_back(make_pair(b,c));

		nei[b].push_back(make_pair(a,-c));

	}

	fill(dp,dp+100005,INF);

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

		if(dp[i]==INF){

			//f/or(int i=0;i<n;i++){

				//c/out<<dp[i]<<" ";

			//}

			//cout<<endl;

			queue<int> q;

			q.push(i);

			dp[i]=0;

			for(;q.size();){

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

				for(int j=0;j<nei[t].size();j++){

					pii p=nei[t][j];

					int pp=p.first,c=p.second;

					if(dp[pp]==INF){

						dp[pp]=dp[t]+c;

						q.push(pp);

					}

					else if(dp[pp]!=dp[t]+c){

						puts("No");

						return 0;

					}

				}

			}

		}

	}

	puts("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.178281060523611x):
#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;

    }

}