stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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";

} 

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

int fa[100001],fl,fr,d[100001],n,m,l,r,d2;

bool j;

int gf(int x)

{

	int d1=fa[x];

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

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

	d[x]+=d[d1];

	return fa[x];

}

int main()

{

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

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

	fa[i]=i;

	while(m)

	{

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

		fl=gf(l);fr=gf(r);

		if(fl==fr&&d[r]-d[l]!=d2)

		{

			j=1;break;

		}

		else 

		{

			fa[fr]=fl;d[fr]=d2-d[r]+d[l];

		}

		m--;

	}

	if(j)printf("No");

	else printf("Yes");

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

}

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

int fa[100001],fl,fr,d[100001],n,m,l,r,d2;

bool j;

int gf(int x)

{

	int d1=fa[x];

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

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

	d[x]+=d[d1];

	return fa[x];

}

int main()

{

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

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

	fa[i]=i;

	while(m)

	{

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

		fl=gf(l);fr=gf(r);

		if(fl==fr&&d[r]-d[l]!=d2)

		{

			j=1;break;

		}

		else 

		{

			fa[fr]=fl;d[fr]=d2-d[r]+d[l];

		}

		m--;

	}

	if(j)printf("No");

	else printf("Yes");

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

#include<cstdio>

using namespace std;

const int maxn=5*1e5+7;

const int INF=1e9+7;

int cur,E,V,head[maxn],dist[maxn];

int A,B,C;

bool vis[maxn];

struct edge{

	int to,nex,cost;

	edge(){}

	edge(int _a,int _b,int _c){

		to=_a,nex=_b,cost=_c;

	}	

}eg[maxn];

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

	eg[++cur]=edge(v,head[u],w);

	head[u]=cur;

}

bool SPFA(int x){

	vis[x]=true;

	for(int i=head[x];i;i=eg[i].nex){

		int v=eg[i].to;

		if(dist[v]>dist[x]+eg[i].cost){

			dist[v]=dist[x]+eg[i].cost;

			if(vis[v]||!SPFA(v)){

				return false;

			}

		}

	}

	vis[x]=false;

	return true;

}

int main(){

	cin>>V>>E;

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

		cin>>A>>B>>C;

		add_eg(A,B,C);

		add_eg(B,A,-C);

	}

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

		dist[i]=0;

		if(!SPFA(i)){

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

} 

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

int fa[100001],fl,fr,d[100001],n,m,l,r,d2;

bool j;

int gf(int x)

{

	int d1=fa[x];

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

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

	d[x]+=d[d1];

	return fa[x];

}

int main()

{

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

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

	fa[i]=i;

	while(m)

	{

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

		fl=gf(l);fr=gf(r);

		if(fl==fr&&d[r]-d[l]!=d2)

		{

			j=1;break;

		}

		else 

		{

			fa[fr]=fl;d[fr]=d2-d[r]+d[l];

		}

		m--;

	}

	if(j)printf("No");

	else printf("Yes");

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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>

using namespace std;



struct ed{

    int to;

	int dis;

	int next;

} edge[400050];

int head[100050];

int edge_tot;



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

{

	edge_tot += 1;

    edge[edge_tot].to = to;

    edge[edge_tot].dis = dis;

    edge[edge_tot].next = head[from];

    head[from] = edge_tot;

}



int n,m;

long long dis[100005];

queue<int> q;

bool vis[100050],inq[100050];



bool spfa(int s){

    q.push(s);

    inq[s] = true;

    dis[s] = 0;

    long long curmax = 0;

	long long curmin = 0;

    while(!q.empty()){

        int u = q.front();

        q.pop();

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

            int v = edge[i].to;

            if(dis[v] == 12345678987654321){

                dis[v] = dis[u] + edge[i].dis;

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

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

                if(curmax - curmin > 1000000000) return false;

                vis[v] = 1;

                if(!inq[v])

                    q.push(v);

            }

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

                return false;

        }

        inq[u] = false;

    }

    return true;

}



int main()

{

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

        dis[i] = 12345678987654321;

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

    int tmp1,tmp2,tmp3;

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

    {

        scanf("%d%d%d",&tmp1,&tmp2,&tmp3);

        addedge(tmp1,tmp2,tmp3);

        addedge(tmp2,tmp1,-tmp3);

    }

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

        if(!vis[i]){

            vis[i] = 1;

			bool l = spfa(i);

            if(!l){

                printf("No\n");

                return 0;

            }

        }

    }

    printf("Yes\n");

    return 0;

}

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

int fa[100001],fl,fr,d[100001],n,m,l,r,d2;

bool j;

int gf(int x)

{

	int d1=fa[x];

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

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

	d[x]+=d[d1];

	return fa[x];

}

int main()

{

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

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

	fa[i]=i;

	while(m)

	{

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

		fl=gf(l);fr=gf(r);

		if(fl==fr&&d[r]-d[l]!=d2)

		{

			j=1;break;

		}

		else 

		{

			fa[fr]=fl;d[fr]=d2-d[r]+d[l];

		}

		m--;

	}

	if(j)printf("No");

	else printf("Yes");

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

bool dfs(int fa)

{

    vis[fa]=true;

    use[fa]=true;

    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 true;

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

            if(dfs(y))return true;

        }

    }

    vis[fa]=false;

    return false;

}

int main()

{

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

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

    {

        d[i]=0x7fffffffffffffff;

    }

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

    {

        int x,y,z;

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

        v[x].push_back(y);

        e[x].push_back(z);

        v[y].push_back(x);

        e[y].push_back(-z);

    }

    bool ok=false;

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

    {

        if(!use[i])

        {

            d[i]=false;

            if(dfs(i))

            {

                ok=true;

            }

        }

    }

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

    else printf("Yes\n");

    return 0;

}

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

int fa[100001],fl,fr,d[100001],n,m,l,r,d2;

bool j;

int gf(int x)

{

	int d1=fa[x];

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

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

	d[x]+=d[d1];

	return fa[x];

}

int main()

{

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

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

	fa[i]=i;

	while(m)

	{

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

		fl=gf(l);fr=gf(r);

		if(fl==fr&&d[r]-d[l]!=d2)

		{

			j=1;break;

		}

		else 

		{

			fa[fr]=fl;d[fr]=d2-d[r]+d[l];

		}

		m--;

	}

	if(j)printf("No");

	else printf("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.675824716365318x):
#include<iostream>

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    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.7942900231357137x):
#include<iostream>

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    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.8942780612059527x):
#include<iostream>

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    return 0;

}
// Slower C++ solution:
#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";

} 

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

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    return 0;

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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>

using namespace std;



struct ed{

    int to;

	int dis;

	int next;

} edge[400050];

int head[100050];

int edge_tot;



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

{

	edge_tot += 1;

    edge[edge_tot].to = to;

    edge[edge_tot].dis = dis;

    edge[edge_tot].next = head[from];

    head[from] = edge_tot;

}



int n,m;

long long dis[100005];

queue<int> q;

bool vis[100050],inq[100050];



bool spfa(int s){

    q.push(s);

    inq[s] = true;

    dis[s] = 0;

    long long curmax = 0;

	long long curmin = 0;

    while(!q.empty()){

        int u = q.front();

        q.pop();

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

            int v = edge[i].to;

            if(dis[v] == 12345678987654321){

                dis[v] = dis[u] + edge[i].dis;

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

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

                if(curmax - curmin > 1000000000) return false;

                vis[v] = 1;

                if(!inq[v])

                    q.push(v);

            }

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

                return false;

        }

        inq[u] = false;

    }

    return true;

}



int main()

{

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

        dis[i] = 12345678987654321;

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

    int tmp1,tmp2,tmp3;

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

    {

        scanf("%d%d%d",&tmp1,&tmp2,&tmp3);

        addedge(tmp1,tmp2,tmp3);

        addedge(tmp2,tmp1,-tmp3);

    }

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

        if(!vis[i]){

            vis[i] = 1;

			bool l = spfa(i);

            if(!l){

                printf("No\n");

                return 0;

            }

        }

    }

    printf("Yes\n");

    return 0;

}

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

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    return 0;

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

bool dfs(int fa)

{

    vis[fa]=true;

    use[fa]=true;

    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 true;

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

            if(dfs(y))return true;

        }

    }

    vis[fa]=false;

    return false;

}

int main()

{

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

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

    {

        d[i]=0x7fffffffffffffff;

    }

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

    {

        int x,y,z;

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

        v[x].push_back(y);

        e[x].push_back(z);

        v[y].push_back(x);

        e[y].push_back(-z);

    }

    bool ok=false;

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

    {

        if(!use[i])

        {

            d[i]=false;

            if(dfs(i))

            {

                ok=true;

            }

        }

    }

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

    else printf("Yes\n");

    return 0;

}

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

using namespace std;

const int maxn=100005;

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

bool ok=true;

int find(int x)

{

    if(fa[x]!=x)

    {

        int nf=fa[x];

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

        dis[x]+=dis[nf];

    }

    return fa[x];

}

int main()

{

    cin>>n>>m;

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

        fa[i]=i;

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

    {

        int l,r,d;

        cin>>l>>r>>d;

        int ll=find(l),rr=find(r);

        if(ll==rr&&dis[l]-dis[r]!=d)

            ok=false;

        if(ll!=rr)

        {

            fa[ll]=rr;

            dis[ll]=d+dis[r]-dis[l];

        }

    }

    if(ok)

        cout<<"Yes"<<endl;

    else

        cout<<"No"<<endl;

    return 0;

}