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

#include<cstdio>

const int N=1e5+5;

int n,m,par[N],dir[N];

void init() {

    for(int i=1;i<=n;i++) par[i]=i,dir[i]=0;

}

int fd(int x) {

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

    int t=par[x];

    par[x]=fd(par[x]);

    dir[x]+=dir[t];

    return par[x];

}

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

    int a=fd(x),b=fd(y);

    if(a==b) return;

    dir[a]=-dir[x]+c+dir[y];

    par[a]=b;

}

int main() {

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

    int l,r,d;init();

    bool ok=true;

    while(m--) {

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

        if(fd(l)!=fd(r)) unite(l,r,d);

        else if(dir[l]-dir[r]!=d) ok=false;

    }

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

    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 <algorithm>

#include <cstring>

#include <cmath>

#include <queue>

#include <vector>

using namespace std;

struct node{

    int u,w;

};

node p;

vector<node>q[100005];

int visit[100005];

int dis[100005],flag;

void query(int x){

    if(flag==1) return ;

    for(int i=0;i<q[x].size();i++){

        node y=q[x][i];

        if(visit[y.u]==1){

            if(dis[y.u]!=dis[x]+y.w) {

                flag=1;

                return ;

            }

            else continue;

        }

        visit[y.u]=1;

        dis[y.u]=dis[x]+y.w;

        query(y.u);

    }



}

int main(){

    int n,m,k,u,w;

    while(scanf("%d%d",&n,&m)!=EOF){

        memset(visit,0,sizeof(visit));

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

            scanf("%d%d%d",&k,&u,&w);

            p.u=u,p.w=w;

            q[k].push_back(p);

            p.u=k,p.w=-w;

            q[u].push_back(p);

        }

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

            if(visit[i]==0){

                flag=0;

                query(i);

            }

            if(flag==1){

                printf("No\n");

                break;

            }



        }

        if(flag==0) printf("Yes\n");

    }

    return 0;

}




// Optimized solution to the same problem (faster):
#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;

}

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

#include <queue>

#include <stdio.h>

using namespace std;

int f=0;

int seedis(int x,int *arr,long long *dis)

{

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

    else return seedis(arr[x],arr,dis) + dis[x];

}

int seef(int l,int *arr,long long *dis)

{

    if(arr[l]==0) return l;

    int a1=arr[l];

    int a2=seef(arr[l],arr,dis);

    arr[l]=a2;

    dis[l]=dis[l]+dis[a1];

    return a2;

}

void hebing(int l,int r,int d,int *arr,long long *dis)

{

    int lf=seef(l,arr,dis);

    int rf=seef(r,arr,dis);

    if(rf==lf)

    {

        if(seedis(r,arr,dis)-seedis(l,arr,dis)!=d)

            f=1;

        return;

    }

    arr[rf]=lf;

    dis[rf]=seedis(l,arr,dis)+d-seedis(r,arr,dis);

}

int main()

{

    int n,m;

    int arr[100500];

    long long dis[100500];

    cin>>n>>m;

    int l,r,d;

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

    {

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

        hebing(l,r,d,arr,dis);

        if(f==1) break;

    }

    if(f) cout<<"No";

    else cout<<"Yes";

    return 0;

}


// Optimized solution to the same problem (faster):
#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;

}

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn=1e5+5; 

vector <pair<int, int> > adj[maxn];

bool vis[maxn];

int x[maxn];



void dfs(int now,int dist)

{

	vis[now] = true;

	x[now] = dist;

 

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

	{

		int next = adj[now][i].first;

		int d = adj[now][i].second;

		if (vis[next] == false)

		{

			dfs(next, dist + d);

		}

	}

}

 

int main()

{

	int n, m;

	int l, r, d;

	bool ok = true;

 

	cin.tie(0);

 

	while(cin >> n >> m)

	{

		ok=true;

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

	{

		cin >> l >> r >> d;

		adj[l].push_back(make_pair(r, d)); //关联点 

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

	}

 

	memset(x, 0, sizeof(x));

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

 

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

	{

		if (vis[i]==false)

		{

			dfs(i, 0);

		}

	}



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

	{

		for (int j = 0; j < adj[i].size(); j++)

		{

			int v = adj[i][j].first;

			if (adj[i][j].second != x[v]-x[i])

			{

				ok = false;

				break;

			}

		}

		if (ok == false)

		{

			cout << "No" << '\n';

			break;

		}

 

	}

 

	if (ok == true)

	{

		cout << "Yes" << '\n';

	}

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#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;

}

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

using namespace std;

#define fi first

#define se second

typedef vector<int> VI;

typedef pair<int,int> PII;

typedef long long ll;

#define mp make_pair

#define pb push_back

void solve();

void read_input(){



}

void solve(){



}

vector<PII> edges[100005];



vector<int> order;

bool visited[100005];

ll pos[100005];

void dfs(int i){

	if (visited[i])

		return ;

	visited[i] = true;

	for(PII p:edges[i]){

		if (!visited[p.fi])

			dfs(p.fi);

	}

	order.pb(i);

}

int main(){

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

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

		edges[i] = vector<PII>();

	}	

	int N,M;

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

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

		int a,b,c;

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

		edges[a].pb({b,c});

	}

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

		if (!visited[i]){

			dfs(i);

		}

	} 

	reverse(order.begin(),order.end());

	for(int i:order){

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

			pos[i] = 0;

		}

		for(PII p:edges[i]){

			if (pos[p.fi] == -1){

				pos[p.fi] = pos[i]+p.se;

			}else{

				if (pos[p.fi] != pos[i]+p.se){

					cout<<"No"<<endl;

					return 0;

				}

			}

		}

	}

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

		if (pos[i]>1000000000){

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}

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

using namespace std;



typedef pair<int, int> pi;



int n, m, l, r, d, dist[100001], vis[100001];

vector<pi> adj[100001];



void dfs(int node){

	vis[node] = 1;

	for(auto it:adj[node]){

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

			if(dist[it.first] != dist[node]+it.second){ cout << "No"; exit(0); }

			continue;

		}

		vis[it.first] = 1;

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

		dfs(it.first);

	}

}



int main(){

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

	cin >> n >> m;

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

		cin >> l >> r >> d;

		adj[l].push_back(pi(r, d));

		adj[r].push_back(pi(l, -d));

	}

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

		if(vis[i] == -1) dfs(i);

	}

	cout << "Yes";

}

// 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;       //input members

int main()    //main body

{

   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");          //output

   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;

bool f;



void dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

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

        {

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

            dfs(pa.f);

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

        {

            f =1;

        }

    }

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

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

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    #   ifdef LOCAL_PROJECT

    freopen("in.txt", "r", stdin);

    #   endif

    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;

                }

            }

            dfs(i);

        }

        st.pop();

    }

    if(f) cout << "No";

    else 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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



typedef pair < int , pair < int , int > > pll;



const int N = 1e5+5;



int n;

int m;

int dist[N];

int indeg[N];

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



int main(){

    memset(dist , -1 , sizeof dist);

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

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

        int l , r , d;

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

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

        indeg[r]++;

    }

    int bad = 0;

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

        if(!indeg[i]){

            queue < int > q;

            q.push(i);

            dist[i] = 0;

            while(!q.empty()){

                int u = q.front();

                q.pop();

                for(auto nw : adj[u]){

                    int v = nw.first;

                    int d = nw.second;

                    if(dist[v] == -1){

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

                        if(dist[v] > 1e9)

                            bad = 1;

                        q.push(v);

                    } else {

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

                            bad = 1;

                        }

                    }

                }

            }

        }

    }

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

        if(dist[i] == -1)

            bad = 1;

    }

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

}


// 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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}

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

using namespace std;

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

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

typedef long long ll;

typedef pair<int, int> P;

const int INF = 1e9;

const int MOD = 1e9 + 7;



#define MAX_V 100000



struct edge { int to, cost; };



int V;//頂点数

vector<edge> G[MAX_V];//辺の張り方 : G[from].push_back((edge){to, cost});

int dist[MAX_V];

bool used[MAX_V];

bool no;



void DFS(int v, int d){

    if(used[v] == true){

        if(d != dist[v]){

            no = true;

        }

        return;

    }

    dist[v] = d;

    used[v] = true;

    REP(i,G[v].size()){

        DFS(G[v][i].to, d + G[v][i].cost);

    }

    return;

}



signed main(){

    int m;

    cin >> V >> m;

    int l,r,d;

    REP(i,m){

        cin >> l >> r >> d;

        l--;

        r--;

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

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

    }

    REP(i,V){

        if(!used[i]){

            DFS(i, 0);

            if(no){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
//ABC-87-B

#include <bits/stdc++.h>

using namespace std;

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

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

typedef long long ll;

typedef pair<int, int> P;

const int INF = 1e9;

const long long LINF = 1e18;

const long long MOD = 1e9 + 7;



template <typename T>

struct WeightedUnionFind{

    vector<int> par,siz;

    vector<T> ws;



    WeightedUnionFind(int n) : par(n), siz(n, 1), ws(n, T(0)){

        iota(par.begin(), par.end(), 0);

    }



    int root(int x){

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

        int t = root(par[x]);

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

        return par[x] = t;

    }



    T weight(int x){

        root(x);

        return ws[x];

    }



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

        w += weight(x);

        w -= weight(y);

        x = root(x);

        y = root(y);

        if(x == y) return;

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

            swap(x, y);

            w = -w;

        }

        siz[x] += siz[y];

        par[y] = x;

        ws[y] = w;

    }



    bool same(int x, int y){

        return root(x) == root(y);

    }



    int size(int x){

        return siz[root(x)];

    }



    T diff(int x, int y){

        return weight(y) - weight(x);

    }

};



signed main(){

    int n,m;

    cin >> n >> m;

    int l[m],r[m],d[m];

    REP(i,m){

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

        l[i]--;

        r[i]--;

    }

    WeightedUnionFind<ll> wuf(n);

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

        if(wuf.same(l[i], r[i]) && wuf.diff(l[i], r[i]) != d[i]){

            cout << "No" << endl;

            return 0;

        }

        wuf.unite(l[i], r[i], d[i]);

    }

    cout << "Yes" << endl;

    return 0;

}

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

using namespace std;

long long d[100005];

int n,m;

bool vis[100005],use[100005];

vector<int>v[100005];

vector<int>e[100005];

void read(int& x)

{

	char c=getchar();

	x=0;

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

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

	{

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

		c=getchar();

	}

}

bool dfs(int fa)

{

	vis[fa]=1;

	use[fa]=1;

	for(int i=0;i<v[fa].size();i++)

	{

		int y=v[fa][i];

		int z=e[fa][i];

		if(d[y]>d[fa]+z)

		{

			if(vis[y])return 1;

			d[y]=d[fa]+z;

			if(dfs(y))return 1;

		}

	}

	vis[fa]=0;

	return 0;

}

int main()

{

	read(n);

	read(m);

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

	{

		d[i]=200000000000ll;

	}

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

	{

		int x,y,z;

		read(x);

		read(y);

		read(z);

		v[x].push_back(y);

		e[x].push_back(z);

		v[y].push_back(x);

		e[y].push_back(-z);

	}

	bool ok=0;

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

	{

		if(!use[i])

		{

			d[i]=0;

			if(dfs(i))

			{

				ok=1;

			}

		}

	}

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

	else printf("Yes\n");

	return 0;

}

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

#define maxn 100005

using namespace std;



int fa[maxn], dis[maxn];



int find(int x)

{

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

	int t = fa[x];

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

	dis[x] += dis[t];

	return fa[x];

}



int main(int argc, char** argv)

{

	int n, m;

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

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

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

	{

		int l, r, d;

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

		int x = find(--l), y = find(--r);

		if(x == y && dis[l] - dis[r] != d)

		{

			puts("No");

			return 0;

		}

		if(x != y)

			fa[x] = fa[y], dis[x] = d + dis[r] - dis[l] + dis[y];

	}

	puts("Yes");

	return 0;

}

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

            memset(dis,0,sizeof(dis));

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

            memset(dis,0,sizeof(int) * (n + 1));

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

}


// Fastest known solution for this problem: