stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#ifndef chafen

#define chafen



# include <cstdio>

# include <iostream>

# include <cstring>

# include <queue>

# pragma GCC optimize(2)

# define N 300000

# define INF 0x7f7f7f7f //0x7fffffff

# define _cin std::ios::sync_with_stdio(false)

# define rg register

# define il inline

struct edge{

    int next;

    int to;

    int w;

}e[(N+1)<<1];

int head[N+1];

int book[N+1];

int vis[N+1];

int dis[N+1];

int n,m;

using namespace std;

//template <class A> il bool SPFA(A s);

bool SPFA(int s);

int main()

{

    _cin;

    rg int edge_sum=0;

    rg int u,v,w;

    cin>>n>>m;

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

        cin>>u>>v>>w;

        //edge_sum++;

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

        e[i].to=v;

        e[i].w=w;

        head[u]=i;

        

        //edge_sum++;

        e[i+m].next=head[v];

        e[i+m].to=u;

        e[i+m].w=-w;

        head[v]=i+m;

    }

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

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

    memset(book,0,sizeof(book));

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

        if(!vis[i]){

            vis[i]=1;

            if(!SPFA(i)){

                cout<<"No"<<endl;

                return 0;

            }

        }

    }

    cout<<"Yes"<<endl;

    return 0;

}

//template <class A> il bool SPFA(A s)

il bool SPFA(int s)

{

    queue <int> q;

    dis[s]=0;

    q.push(s);book[s]=1;

    while(q.size()>0){

        rg int k=q.front();q.pop();

        book[k]=0;

        for(rg int i = head[k];i;i=e[i].next){

            rg int tt=e[i].to;

            if(dis[k]<INF && dis[tt]<INF && dis[tt]!=dis[k]+e[i].w) return false;

			else if(dis[k]<INF && dis[tt] == INF){

                dis[tt]=dis[k]+e[i].w;

                if(book[tt] == false) {q.push(tt);book[tt]=1;}

                vis[tt]=1;

            }

        }

    }

    return true;

}



#endif

// Faster C++ solution for the same problem (measured speedup: 24.66483906112478x):
#include<cstdio>

#include<cctype>

#include<queue>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=1e5+100,M=2e5+100,INF=0x7fffffff/2;

bool vis[N],flag;

int n,m,dis[N];

struct Graph{

	int tot,tail[N];

	struct edge{

		int v,w,last;

	}e[M<<1];

	void clear(){tot=0;memset(tail,0,sizeof tail);} 

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

		e[++tot]=(edge){y,w,tail[x]};

		tail[x]=tot;

	}

}G;

void SPFA_DFS(int h)

  {

       if(flag) return ;

       register int p,v,w;

       vis[h]=true;

       for(p=G.tail[h];p;p=G.e[p].last)

       {

           v=G.e[p].v,w=G.e[p].w;

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

          {

              if(vis[v]){flag=true;return ;}

              dis[v]=dis[h]+w;

              SPFA_DFS(v);

              if(flag) return ;

          }    

      }

      vis[h]=false;

  }

int main()

{

	register int i,l,r,d;

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

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

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

		G.add(l,r,d),G.add(r,l,-d);

	}

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

		SPFA_DFS(i); if(flag) break;

	}

	if(flag) printf("No");

	else printf("Yes");

	return 0;

}
// Slower C++ solution:
#ifndef chafen

#define chafen



# include <cstdio>

# include <iostream>

# include <cstring>

# include <queue>

# pragma GCC optimize(2)

# define N 300000

# define INF 0x7f7f7f7f //0x7fffffff

# define _cin std::ios::sync_with_stdio(false)

# define rg register

# define il inline

struct edge{

    int next;

    int to;

    int w;

}e[(N+1)<<1];

int head[N+1];

int book[N+1];

int vis[N+1];

int dis[N+1];

int n,m;

using namespace std;

//template <class A> il bool SPFA(A s);

bool SPFA(int s);

int main()

{

    _cin;

    rg int edge_sum=0;

    rg int u,v,w;

    cin>>n>>m;

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

        cin>>u>>v>>w;

        //edge_sum++;

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

        e[i].to=v;

        e[i].w=w;

        head[u]=i;

        

        //edge_sum++;

        e[i+m].next=head[v];

        e[i+m].to=u;

        e[i+m].w=-w;

        head[v]=i+m;

    }

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

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

    memset(book,0,sizeof(book));

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

        if(!vis[i]){

            vis[i]=1;

            if(!SPFA(i)){

                cout<<"No"<<endl;

                return 0;

            }

        }

    }

    cout<<"Yes"<<endl;

    return 0;

}

//template <class A> il bool SPFA(A s)

il bool SPFA(int s)

{

    queue <int> q;

    dis[s]=0;

    q.push(s);book[s]=1;

    while(q.size()>0){

        rg int k=q.front();q.pop();

        book[k]=0;

        for(rg int i = head[k];i;i=e[i].next){

            rg int tt=e[i].to;

            if(dis[k]<INF && dis[tt]<INF && dis[tt]!=dis[k]+e[i].w) return false;

			else if(dis[k]<INF && dis[tt] == INF){

                dis[tt]=dis[k]+e[i].w;

                if(book[tt] == false) {q.push(tt);book[tt]=1;}

                vis[tt]=1;

            }

        }

    }

    return true;

}



#endif

// Faster C++ solution for the same problem (measured speedup: 4.674185232587735x):
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

int n,m,fa[100005],f[100005];

int find(int x)

{

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

	int y=find(fa[x]);

	f[x]+=f[fa[x]];

	fa[x]=y;

	return fa[x];

}

void merge(int x,int y,int z)

{

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

	if (fx==fy)

	{

		if (f[y]-f[x]!=z) 

		{

			cout<<"No";

			exit(0);

		}

		return; 

	}

	fa[fy]=fx;

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

}

int main()

{

	cin>>n>>m;

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

	{

		fa[i]=i;

		f[i]=0;

	}

	int x,y,z;

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

	{

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

		if (z<0)

		{

			int t=x;

			x=y;

			y=t;

			z=-z;

		}

		merge(x,y,z);

//		for (int i=1;i<=n;i++) cout<<fa[i]<<' '<<f[i]<<'\n';

//		cout<<'\n';

	}

	cout<<"Yes";

//	int ans=0;

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

//	{

//		find(i);

//		ans=max(f[i],ans);

//	}

//	cout<<ans;

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

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

#include <vector>

#include <cmath>

#define LL long long

using namespace std;

const int MOD=1e9+7;

const int INF=0x3f3f3f3f;

const int M=200100;

const int N=100100;

inline void read(int &x){

	x=0;int mm=1; char ch=getchar();

	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();

	if(ch=='-') mm=-1,ch=getchar();

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

	x=x*mm;

}

inline void read(LL &x){

	x=0;LL mm=1; char ch=getchar();

	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();

	if(ch=='-') mm=-1,ch=getchar();

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

	x=x*mm;

}

struct Edge{

	int nxt;

	int v,w;

}edge[M<<2];

int head[N],tot;

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

	edge[++tot].nxt=head[u];

	edge[tot].v=v;

	edge[tot].w=w;

	head[u]=tot;

}

int n,m;

int dist[N];

bool visited[N],inque[N];

queue <int> q;

bool SPFA(int beginn){

	visited[beginn]=true;

	inque[beginn]=true;

	dist[beginn]=0;

	q.push(beginn);

	int maxx=0,minn=0;

	while(!q.empty()){

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

		inque[u]=false;

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

			Edge &e=edge[i];

			if(dist[e.v]<INF){

				if(dist[e.v]!=e.w+dist[u]){

					return false;

				}

			}else{

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

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

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

				if(maxx-minn>INF) return false;

				visited[e.v]=true;

				if(!inque[e.v]){

					inque[e.v]=true;

					q.push(e.v);

				}

			}

		}

	}

	return true;

}

int main(){

	read(n),read(m);

	memset(head,0,sizeof(head));

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

		int u,v,w;

		read(u),read(v),read(w);

		add(u,v,w);

		add(v,u,-w);

	}

	memset(dist,INF,sizeof(dist));

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

		if(!visited[i])

			if(!SPFA(i)){

				printf("No");

				return 0;

			}

	}

	printf("Yes");

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7936608914715362x):
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

int n,m,fa[100005],f[100005];

int find(int x)

{

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

	int y=find(fa[x]);

	f[x]+=f[fa[x]];

	fa[x]=y;

	return fa[x];

}

void merge(int x,int y,int z)

{

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

	if (fx==fy)

	{

		if (f[y]-f[x]!=z) 

		{

			cout<<"No";

			exit(0);

		}

		return; 

	}

	fa[fy]=fx;

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

}

int main()

{

	cin>>n>>m;

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

	{

		fa[i]=i;

		f[i]=0;

	}

	int x,y,z;

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

	{

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

		if (z<0)

		{

			int t=x;

			x=y;

			y=t;

			z=-z;

		}

		merge(x,y,z);

//		for (int i=1;i<=n;i++) cout<<fa[i]<<' '<<f[i]<<'\n';

//		cout<<'\n';

	}

	cout<<"Yes";

//	int ans=0;

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

//	{

//		find(i);

//		ans=max(f[i],ans);

//	}

//	cout<<ans;

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

using namespace std;

#define Rint register int

typedef long long ll;

ll hd[100010],nxt[400010],eds[400010],val[400010],et=0;

inline ll read(){

	ll x=0,w=1;char c=getchar();

	while(!isdigit(c)){

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

		c=getchar();

	}

	while(isdigit(c)){

		x=(x<<1)+(x<<3)+c-'0';

		c=getchar();

	}

	return x*w;

}

inline void ad(ll u,ll v,ll w){

	eds[++et]=v;

	val[et]=w;

	nxt[et]=hd[u];

	hd[u]=et;

}

ll n,m;

ll dis[100010];

bool vstd[100010];

bool inq[100010];

queue<ll> q;

ll spfa(ll u){

	memset(vstd,0,sizeof(vstd));

	ll minn=0,maxx=0;

	dis[u]=0;

	while(!q.empty())q.pop();

	q.push(u);

	inq[u]=true;

	while(!q.empty()){

		ll now=q.front();

		q.pop();

		inq[now]=false;

		for (Rint i=hd[now];i;i=nxt[i])

			if (dis[eds[i]]>dis[now]+val[i]){

				if(vstd[eds[i]])return -1;

				vstd[eds[i]]=true;

				dis[eds[i]]=dis[now]+val[i];

				maxx=max(maxx,dis[eds[i]]);

				minn=min(minn,dis[eds[i]]);

				if(!inq[eds[i]]){

					inq[eds[i]]=true;

					q.push(eds[i]);

				}

			}

	}

	return maxx-minn;

}

int main(){

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

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

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

	ll t1,t2,t3;

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

		t1=read(),t2=read(),t3=read();

		ad(t1,t2,t3);

		ad(t2,t1,-t3);

	}

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

	ll maxx=0;

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

		if (dis[i]==0x3f3f3f3f3f3f3f3f){

			ll tmp=spfa(i);

			if (tmp==-1){

				puts("No");

				return 0;

			}

			maxx=max(maxx,tmp);

		}

	puts("Yes");

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 1.8936138707497305x):
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

int n,m,fa[100005],f[100005];

int find(int x)

{

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

	int y=find(fa[x]);

	f[x]+=f[fa[x]];

	fa[x]=y;

	return fa[x];

}

void merge(int x,int y,int z)

{

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

	if (fx==fy)

	{

		if (f[y]-f[x]!=z) 

		{

			cout<<"No";

			exit(0);

		}

		return; 

	}

	fa[fy]=fx;

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

}

int main()

{

	cin>>n>>m;

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

	{

		fa[i]=i;

		f[i]=0;

	}

	int x,y,z;

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

	{

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

		if (z<0)

		{

			int t=x;

			x=y;

			y=t;

			z=-z;

		}

		merge(x,y,z);

//		for (int i=1;i<=n;i++) cout<<fa[i]<<' '<<f[i]<<'\n';

//		cout<<'\n';

	}

	cout<<"Yes";

//	int ans=0;

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

//	{

//		find(i);

//		ans=max(f[i],ans);

//	}

//	cout<<ans;

}
// Slower C++ solution:
#ifndef chafen

#define chafen



# include <cstdio>

# include <iostream>

# include <cstring>

# include <queue>

# pragma GCC optimize(2)

# define N 300000

# define INF 0x7f7f7f7f //0x7fffffff

# define _cin std::ios::sync_with_stdio(false)

# define rg register

# define il inline

struct edge{

    int next;

    int to;

    int w;

}e[(N+1)<<1];

int head[N+1];

int book[N+1];

int vis[N+1];

int dis[N+1];

int n,m;

using namespace std;

//template <class A> il bool SPFA(A s);

bool SPFA(int s);

int main()

{

    _cin;

    rg int edge_sum=0;

    rg int u,v,w;

    cin>>n>>m;

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

        cin>>u>>v>>w;

        //edge_sum++;

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

        e[i].to=v;

        e[i].w=w;

        head[u]=i;

        

        //edge_sum++;

        e[i+m].next=head[v];

        e[i+m].to=u;

        e[i+m].w=-w;

        head[v]=i+m;

    }

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

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

    memset(book,0,sizeof(book));

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

        if(!vis[i]){

            vis[i]=1;

            if(!SPFA(i)){

                cout<<"No"<<endl;

                return 0;

            }

        }

    }

    cout<<"Yes"<<endl;

    return 0;

}

//template <class A> il bool SPFA(A s)

il bool SPFA(int s)

{

    queue <int> q;

    dis[s]=0;

    q.push(s);book[s]=1;

    while(q.size()>0){

        rg int k=q.front();q.pop();

        book[k]=0;

        for(rg int i = head[k];i;i=e[i].next){

            rg int tt=e[i].to;

            if(dis[k]<INF && dis[tt]<INF && dis[tt]!=dis[k]+e[i].w) return false;

			else if(dis[k]<INF && dis[tt] == INF){

                dis[tt]=dis[k]+e[i].w;

                if(book[tt] == false) {q.push(tt);book[tt]=1;}

                vis[tt]=1;

            }

        }

    }

    return true;

}



#endif

// Faster C++ solution for the same problem (measured speedup: 1.3183219908727108x):
#include<iostream>

#include<cstdio>

#include<vector>

#include<queue>

#include<algorithm>

using namespace std;

struct node

{

	int to,dis;

	node(){}

	node(int x,int y):to(x),dis(y){}

};

#define INF 1000000000

int n,m,f[100005],b[100005];

vector<node> a[100005];

void spfa(int x)

{

	queue<int> p;

	p.push(x);

	b[x]=1;

	f[x]=0;

	while (!p.empty())

	{

		int v=p.front();

		p.pop();

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

		{

			if (f[a[v][i].to]==INF)

			{

				f[a[v][i].to]=f[v]+a[v][i].dis;

				b[a[v][i].to]=1;

				p.push(a[v][i].to);

			}

			else if (f[a[v][i].to]!=f[v]+a[v][i].dis)

			{

				cout<<"No";

				exit(0);

			}

		}

	}

}

int main()

{

	cin>>n>>m;

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

	{

		f[i]=INF;

	}

	int x,y,z;

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

	{

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

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

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

	} 

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

	{

		if (b[i]==0)

		{

			spfa(i);

		}

	}

	cout<<"Yes";

}
// Slower C++ solution:
#ifndef chafen

#define chafen



# include <cstdio>

# include <iostream>

# include <cstring>

# include <queue>

# pragma GCC optimize(2)

# define N 300000

# define INF 0x7f7f7f7f //0x7fffffff

# define _cin std::ios::sync_with_stdio(false)

# define rg register

# define il inline

struct edge{

    int next;

    int to;

    int w;

}e[(N+1)<<1];

int head[N+1];

int book[N+1];

int vis[N+1];

int dis[N+1];

int n,m;

using namespace std;

//template <class A> il bool SPFA(A s);

bool SPFA(int s);

int main()

{

    _cin;

    rg int edge_sum=0;

    rg int u,v,w;

    cin>>n>>m;

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

        cin>>u>>v>>w;

        //edge_sum++;

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

        e[i].to=v;

        e[i].w=w;

        head[u]=i;

        

        //edge_sum++;

        e[i+m].next=head[v];

        e[i+m].to=u;

        e[i+m].w=-w;

        head[v]=i+m;

    }

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

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

    memset(book,0,sizeof(book));

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

        if(!vis[i]){

            vis[i]=1;

            if(!SPFA(i)){

                cout<<"No"<<endl;

                return 0;

            }

        }

    }

    cout<<"Yes"<<endl;

    return 0;

}

//template <class A> il bool SPFA(A s)

il bool SPFA(int s)

{

    queue <int> q;

    dis[s]=0;

    q.push(s);book[s]=1;

    while(q.size()>0){

        rg int k=q.front();q.pop();

        book[k]=0;

        for(rg int i = head[k];i;i=e[i].next){

            rg int tt=e[i].to;

            if(dis[k]<INF && dis[tt]<INF && dis[tt]!=dis[k]+e[i].w) return false;

			else if(dis[k]<INF && dis[tt] == INF){

                dis[tt]=dis[k]+e[i].w;

                if(book[tt] == false) {q.push(tt);book[tt]=1;}

                vis[tt]=1;

            }

        }

    }

    return true;

}



#endif

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

using namespace std;



#define maxn 100010

#define maxm 200010

#define INF 0x7f7f7f7f



inline void read(int &x) {

  x = 0;

  bool sign = false;

  char ch = 0;

  while (!isdigit(ch)) { sign |= (ch == '-'); ch = getchar(); }

  while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); }

  x = sign ? -x : x;

}



int n, m, Li, Ri, Di, ans = 0;

int fa[maxm], r[maxn];



inline int get(int x) {

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

  else {

    int l = get(fa[x]);

    r[x] = r[fa[x]] + r[x];

    fa[x] = l;

    return l;

  }

}



int main() {

  read(n); read(m);

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

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

    read(Li); read(Ri); read(Di);

    int fl = get(Li), dl = r[Li];

    int fr = get(Ri), dr = r[Ri];

    if (fl == fr) {

      if (dl + Di != dr) {

        printf("No");

        return 0;

      }

    } else {

      fa[fr] = fl;

      r[fr] = dl + Di - dr;

    }

  }

  printf("Yes");

  return 0;

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

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <queue>

#include <vector>

#include <cmath>

#define LL long long

using namespace std;

const int MOD=1e9+7;

const int INF=0x3f3f3f3f;

const int M=200100;

const int N=100100;

inline void read(int &x){

	x=0;int mm=1; char ch=getchar();

	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();

	if(ch=='-') mm=-1,ch=getchar();

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

	x=x*mm;

}

inline void read(LL &x){

	x=0;LL mm=1; char ch=getchar();

	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();

	if(ch=='-') mm=-1,ch=getchar();

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

	x=x*mm;

}

struct Edge{

	int nxt;

	int v,w;

}edge[M<<2];

int head[N],tot;

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

	edge[++tot].nxt=head[u];

	edge[tot].v=v;

	edge[tot].w=w;

	head[u]=tot;

}

int n,m;

int dist[N];

bool visited[N],inque[N];

queue <int> q;

bool SPFA(int beginn){

	visited[beginn]=true;

	inque[beginn]=true;

	dist[beginn]=0;

	q.push(beginn);

	int maxx=0,minn=0;

	while(!q.empty()){

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

		inque[u]=false;

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

			Edge &e=edge[i];

			if(dist[e.v]<INF){

				if(dist[e.v]!=e.w+dist[u]){

					return false;

				}

			}else{

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

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

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

				if(maxx-minn>INF) return false;

				visited[e.v]=true;

				if(!inque[e.v]){

					inque[e.v]=true;

					q.push(e.v);

				}

			}

		}

	}

	return true;

}

int main(){

	read(n),read(m);

	memset(head,0,sizeof(head));

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

		int u,v,w;

		read(u),read(v),read(w);

		add(u,v,w);

		add(v,u,-w);

	}

	memset(dist,INF,sizeof(dist));

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

		if(!visited[i])

			if(!SPFA(i)){

				printf("No");

				return 0;

			}

	}

	printf("Yes");

	return 0;

}

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

using namespace std;



#define maxn 100010

#define maxm 200010

#define INF 0x7f7f7f7f



inline void read(int &x) {

  x = 0;

  bool sign = false;

  char ch = 0;

  while (!isdigit(ch)) { sign |= (ch == '-'); ch = getchar(); }

  while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); }

  x = sign ? -x : x;

}



int n, m, Li, Ri, Di, ans = 0;

int fa[maxm], r[maxn];



inline int get(int x) {

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

  else {

    int l = get(fa[x]);

    r[x] = r[fa[x]] + r[x];

    fa[x] = l;

    return l;

  }

}



int main() {

  read(n); read(m);

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

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

    read(Li); read(Ri); read(Di);

    int fl = get(Li), dl = r[Li];

    int fr = get(Ri), dr = r[Ri];

    if (fl == fr) {

      if (dl + Di != dr) {

        printf("No");

        return 0;

      }

    } else {

      fa[fr] = fl;

      r[fr] = dl + Di - dr;

    }

  }

  printf("Yes");

  return 0;

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

using namespace std;

#define Rint register int

typedef long long ll;

ll hd[100010],nxt[400010],eds[400010],val[400010],et=0;

inline ll read(){

	ll x=0,w=1;char c=getchar();

	while(!isdigit(c)){

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

		c=getchar();

	}

	while(isdigit(c)){

		x=(x<<1)+(x<<3)+c-'0';

		c=getchar();

	}

	return x*w;

}

inline void ad(ll u,ll v,ll w){

	eds[++et]=v;

	val[et]=w;

	nxt[et]=hd[u];

	hd[u]=et;

}

ll n,m;

ll dis[100010];

bool vstd[100010];

bool inq[100010];

queue<ll> q;

ll spfa(ll u){

	memset(vstd,0,sizeof(vstd));

	ll minn=0,maxx=0;

	dis[u]=0;

	while(!q.empty())q.pop();

	q.push(u);

	inq[u]=true;

	while(!q.empty()){

		ll now=q.front();

		q.pop();

		inq[now]=false;

		for (Rint i=hd[now];i;i=nxt[i])

			if (dis[eds[i]]>dis[now]+val[i]){

				if(vstd[eds[i]])return -1;

				vstd[eds[i]]=true;

				dis[eds[i]]=dis[now]+val[i];

				maxx=max(maxx,dis[eds[i]]);

				minn=min(minn,dis[eds[i]]);

				if(!inq[eds[i]]){

					inq[eds[i]]=true;

					q.push(eds[i]);

				}

			}

	}

	return maxx-minn;

}

int main(){

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

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

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

	ll t1,t2,t3;

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

		t1=read(),t2=read(),t3=read();

		ad(t1,t2,t3);

		ad(t2,t1,-t3);

	}

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

	ll maxx=0;

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

		if (dis[i]==0x3f3f3f3f3f3f3f3f){

			ll tmp=spfa(i);

			if (tmp==-1){

				puts("No");

				return 0;

			}

			maxx=max(maxx,tmp);

		}

	puts("Yes");

	return 0;

} 

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

using namespace std;



#define maxn 100010

#define maxm 200010

#define INF 0x7f7f7f7f



inline void read(int &x) {

  x = 0;

  bool sign = false;

  char ch = 0;

  while (!isdigit(ch)) { sign |= (ch == '-'); ch = getchar(); }

  while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); }

  x = sign ? -x : x;

}



int n, m, Li, Ri, Di, ans = 0;

int fa[maxm], r[maxn];



inline int get(int x) {

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

  else {

    int l = get(fa[x]);

    r[x] = r[fa[x]] + r[x];

    fa[x] = l;

    return l;

  }

}



int main() {

  read(n); read(m);

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

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

    read(Li); read(Ri); read(Di);

    int fl = get(Li), dl = r[Li];

    int fr = get(Ri), dr = r[Ri];

    if (fl == fr) {

      if (dl + Di != dr) {

        printf("No");

        return 0;

      }

    } else {

      fa[fr] = fl;

      r[fr] = dl + Di - dr;

    }

  }

  printf("Yes");

  return 0;

}