stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
# include <cstring>

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050 //N为顶点个数 

# define M 200050 //N为顶点个数 

# define WQD 0x7f7f7f7f//无穷大 

int n;//图的顶点个数

int m;//图的边数 

struct Edge{int next,to, w;}  e[M<<1];//不用0号单元 

int head[N+1]; 

int dis[N+1];//存放源点到其他点的路径

bool vis[N+1];

bool book[N+1];

bool spfa(int s);//差分约束判断 

using namespace std;

int main()

{

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

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

	cin>>n>>m;

	for(int i=1; i<=m; i++)  //本题约束关系为=，要添加两条边 

	{

		cin>>u>>v>>w;//输入第i条边 ,i为编号 

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

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

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

	for(int i=1; i<=n; i++)//每一个点都要校验到 

		if(!vis[i]) //如果i点没有校验，就用spfa校验和i点连通的区域 

		{

			vis[i]=1;

			if(spfa(i)==false)  {cout<<"No";return 0;}//有约束自相矛盾 

		}

	cout<<"Yes";//所有的点都符合条件 

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

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

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

	dis[s]=0;

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

	//如果没松弛就松弛，如果松弛过就判断是否符合约束条件，如果自相矛盾就是不可能的数据

	while(q.size()>0) 

	{

		int k=q.front();q.pop();//对头元素出队 

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			//题目的约束条件是= ，不相等，自相矛盾 

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

			else if(dis[v]==WQD && dis[k]<WQD)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{				

				dis[v]=dis[k]+e[i].w;//更新 

				if(book[v]==0)	{q.push(v);	book[v]=1;}//入队标记

				vis[v]=1;//标记该点已经检验过 

			}

			

		}		

    }

	return true;//别掉了这句 

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

using namespace std;

int n,m;

struct edge{

	int next;

	int to;

	int w;

}e[4000020];

int head[2000020];

bool dijia(int s);

int dis[2000020];

bool book[2000020],vis[2000020];

int main(){

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

	int u,v,w;

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

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

		e[x].to=v;

		e[x].w=w;

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

		head[u]=x;

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

		e[x+m].to=u;

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

		head[v]=x+m;

	}

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

		if(vis[x]){

			continue;

		}

		if(!dijia(x)){

			printf("No");

			return 0;

		}

		else{

			continue;

		}

	}

	printf("Yes");

	return 0;

}

bool dijia(int s){

	dis[s]=0;

	queue<int> q;

	q.push(s);

	book[s]=1;

	while(q.size()){

		int k=q.front();

		q.pop();

		book[k]=0;

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

			if(!vis[e[x].to]){

				dis[e[x].to]=dis[k]+e[x].w;

				q.push(e[x].to);

				book[e[x].to]=1;

				vis[e[x].to]=1;

			}

			else{

				if(dis[e[x].to]!=dis[k]+e[x].w){

					return false;

				}

			}

		}

	}

	return true;

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

using namespace std;

typedef long long ll;

const int N=100005;

struct node

{

	int to;

	ll v;

	node(int a=0,int b=0){to=a;v=b;}

};

vector<node> path[N];

bool vis[N],use[N];

ll d[N];

bool dfs(int o)

{

	//if(vis[o]) return false;

	vis[o]=true;

	use[o]=true;

	for(int i=0;i<path[o].size();i++)

	{

		node e=path[o][i];

		if(d[e.to]>d[o]+e.v)

		{

			if(use[e.to]) return true;

			d[e.to]=d[o]+e.v;

			if(dfs(e.to)) return true;

		}

	}

	use[o]=false;

	return false;

}

int main()

{

	int n,m,a,b;

	ll dd;

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

	while(m--)

	{

		scanf("%d%d%lld",&a,&b,&dd);

		path[a].push_back(node(b,dd));

		path[b].push_back(node(a,-dd));

	}

	for(int i=1;i<=n;i++) d[i]=LLONG_MAX;

	

	bool bo=false;

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

	{

		if(vis[i]) continue;

		d[i]=0;

		if(dfs(i)) {bo=true;break;}

	}

	if(bo) puts("No");

	else puts("Yes");

	return 0;

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

using namespace std;

const int MAXN=100007,MAXM=200007,MAX=1061109567;

typedef long long ll;

struct node {

	int v,w;

	node(int x=0,int y=0) {

		v=x,w=y;

	}

};

int deg[MAXN],X,n,m,x1,y,x2,tot[MAXN];

ll pos[MAXN];

bool vis[MAXN];

char c;

vector<node> edge[MAXN];

int read() {

	X=0,c=getchar();

	while(!isdigit(c))c=getchar();

	while(isdigit(c))X=(X<<1)+(X<<3)+c-'0',c=getchar();

	return X;

}

bool ok;

bool check(int &u) {

	vis[u]=true;

	for(int i=0; i<tot[u]; i++) {

		if(vis[edge[u][i].v]) {

			if(pos[edge[u][i].v]-edge[u][i].w!=pos[u])return false;

		} else {

			pos[edge[u][i].v]=pos[u]+edge[u][i].w;

			if(!check(edge[u][i].v))return false;

			else continue;

		}

	}

	return true;

}

int main() {

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

		if(!m){

			cout<<"Yes";

			return 0;

		}

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

			x1=read(),x2=read(),y=read();

			tot[x1]++;

			deg[x2]++;

			edge[x1].push_back(node(x2,y));

		}

		for(int i=0; i<n; i++)if(!deg[i]&&tot[i]) {

				ok=1;

				if(!check(i)) {

					cout<<"No";

					return 0;

				}

			};

		if(!ok) {

			cout<<"No";

			return 0;

		}

		cout<<"Yes";

		return 0;

}


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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#define LL long long

#define mp make_pair

#define pb push_back

#define INF (1<<29)

using namespace std;

typedef pair<int,int>pii;

int n,m;

vector<pii>G[200010];

int col[200010];

int vis[200010];

bool dfs(int x,int co){

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

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

	vis[x]=1;

	col[x]=co;

	for(pii i:G[x]){

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

//		else{

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

//		}

	}

	return true;

}

int main(){

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

	cin>>n>>m;

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

	if(m==0){

		cout<<"Yes\n";

		return 0;

	}

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

		int l,r,d;

		cin>>l>>r>>d;

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

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

	}

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

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

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

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

			cout<<"No\n";

			return 0;

		}

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

	}

	cout<<"Yes\n";

	return 0;

}


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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#include<iostream>

#include<cstring>

#include<algorithm>

#include<queue>

int dis[100005],flag[100005],vis[100005],h[100005];

int n,m,cnt;

struct wf{

    int to,nxt,w;

}edge[200005*2];

using namespace std;

queue < int > Q;

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

{

    edge[++cnt].to=v;

    edge[cnt].w=w;

    edge[cnt].nxt=h[u];

    h[u]=cnt;

}

bool spfa(int s)

{

    dis[s]=0;

    //vis[s]=1;

    Q.push(s);

    flag[s]=1;

    while(!Q.empty())

    {

        int u=Q.front();

        Q.pop();

        //vis[u]=0;

        for (int j=h[u];j;j=edge[j].nxt)

        {

            int v=edge[j].to;

            if (dis[v]==99999999)

            {

                dis[v]=dis[u]+edge[j].w;

                vis[v]=1;

                if (!flag[v]) Q.push(v);

            }

            else if (dis[v]!=dis[u]+edge[j].w) return false;

        }

        flag[u]=0;

    }

    return true;

}

int main()

{

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

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

	  dis[i]=99999999;

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

    {

        int k,l,q;

        scanf("%d%d%d",&k,&l,&q);

        add(k,l,q);

        add(l,k,-q);

    }

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

    {

        if (!vis[i])

        {

            vis[i]=1;

            if (!spfa(i))

            {

                printf("No");

                return 0;

            }

        }

    }

    printf("Yes");

    //while(1);

    return 0;

}


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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#include<cstring>

#include<iostream>

#include<algorithm>

#define int long long

using namespace std;

int n,m,cnt=0;

int head[410000],nxt[410000],to[410000],val[410000];

int ma=0,mi=0;

int q[1000000],dis[100100],f[100100],vis[100100];

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

{

    cnt++;

    nxt[cnt]=head[x];

    head[x]=cnt;

    to[cnt]=y;

    val[cnt]=z;

}

bool spfa(int x)

{

    int l=0,r=1;

    q[1]=x;vis[x]=1;dis[x]=0;

    while(l<r)

    {

        int u=q[++l];

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

        {

            int v=to[i];

            if(dis[v]==12345678987654321)

            {

                dis[v]=dis[u]+val[i];

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

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

                if(ma-mi>1e9) return 0;

                vis[v]=1;

                if(!f[v]) q[++r]=v,f[v]=1;

            }

            else if(dis[v]!=dis[u]+val[i]) return 0;

        }

        f[u]=0;

    }

    return 1;

}

signed main()

{

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

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

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

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

        dis[i]=12345678987654321;

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

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

    {

        int x,y,z;

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

        addedge(x,y,z);

        addedge(y,x,-z);

    }

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

        if(!vis[i]) {if(!spfa(i)) {printf("No");return 0;}}

    printf("Yes");

    return 0;

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#define R(i,j,k) for(int i=j;i<=k;i++)

using namespace std;

const int maxn=1e5+5;

int n,m;

struct Union_Set {

	int p[maxn],v[maxn];

	Union_Set() {

		R(i,1,maxn-1) p[i]=i;

	}

	void fp(int x) {

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

		fp(p[x]);

		v[x]+=v[p[x]];

		p[x]=p[p[x]];

	}

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

		fp(x); fp(y);

		int X=p[x],Y=p[y];

		if(X==Y) {

			int v1=-v[x],v2=-v[y];

			if(v1+val==v2) return true;

			return false;

		}

		val=val+v[y]-v[x];

		p[X]=Y;

		v[X]=val;

		return true;

	}

}us;

int main() {

#ifdef LOCAL

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

#endif

	cin>>n>>m;

	R(i,1,m) {

		int Li,Ri,Di;

		cin>>Li>>Ri>>Di;

		if(!us.merge(Li,Ri,Di)) {

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

using namespace std;  

int fa[100500];  

int dis[100500];  

int find(int x) {  

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

    int f1=fa[x];  

    int f2=find(fa[x]);  

    fa[x]=f2;  

    dis[x]=dis[x]+dis[f1];  

    return f2;  

}

 

int find_dis(int x) {  

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

    return find_dis(fa[x])+dis[x];

}

   

int main() {  

    int n,m,a,b,d;  

    cin>>n>>m;  

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

        fa[i]=i;  

        dis[i]=0;  

    }  

    while(m>0) {

        m--;

        cin>>a>>b>>d;  

        int f1=find(a);  

        int f2=find(b);  

        if(f1==f2) {  

            if(dis[a]+d!=dis[b]) {  

                cout<<"No";  

                return 0;

            }  

        } 

        else{  

            fa[f2]=f1; 

            dis[f2]=find_dis(a)-find_dis(b)+d;  

        }  

    }  

    cout<<"Yes";

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#include<cstdio>

#include<queue>

using namespace std;

inline void read(int&ans)

{

	ans=0;

	register char ch=getchar();

	while(ch<'0' or ch>'9')ch=getchar();

	while(ch>='0' and ch<='9')ans=ans*10+(ch^48),ch=getchar();

}

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

int find(int x)

{

	if(fa[x]!=x){

		int a=fa[x];fa[x]=find(fa[x]),dis[x]+=dis[a];	

	}

	return fa[x];

}

int main()

{

	read(n),read(m);

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

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

	{

		int l,r,d;

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

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

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

		{

			cout<<"No";return 0;

		}

		else if(ll!=rr)

		{

			fa[ll]=rr;

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

		}

	}

	cout<<"Yes";

}

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

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

bool ok=1;

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(){

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

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

        fa[i]=i;

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

        int l,r,d;

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

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

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

            ok=0;

        if(rl!=rr){

            fa[rl]=rr;

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

        }

    }

    if(ok)printf("Yes");

    else printf("No");

    return 0;

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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>



using namespace std;



struct edge

{

    int v,w,nxt;

}e[400050];



int head[100050],ecnt;



void adde(int u,int v,int w)

{

    e[++ecnt].v = v;

    e[ecnt].w = w;

    e[ecnt].nxt = head[u];

    head[u] = ecnt;

}



int N,M;

int l[200050],r[200050];

int d[200050];

long long dist[100005];

queue<int> que;

bool vis[100050],inq[100050];



bool spfa(int st)

{

    que.push(st);

    inq[st] = true;

    dist[st] = 0;

    long long curmax = 0,curmin = 0;

    while(!que.empty())

    {

        int u = que.front();

        que.pop();

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

        {

            int v = e[i].v;

            if(dist[v] == 12345678987654321)

            {

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

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

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

                if(curmax - curmin > 1000000000)

                    return false;

                vis[v] = 1;

                if(!inq[v])

                    que.push(v);

            }

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

                return false;

        }

        inq[u] = false;

    }

    return true;

}



int main()

{

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

        dist[i] = 12345678987654321;

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

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

    {

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

        adde(l[i],r[i],d[i]);

        adde(r[i],l[i],-d[i]);

    }

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

#include <cstdio>

#include <algorithm>

#define N 400005

#define inf 0x7fffffff

using namespace std;



struct E {int next, to, dis;} e[N];

int n, m, num, flag, cnt;

int h[N], a[N], pos[N], obj[N];

bool vis[N];



int read()

{

	int x = 0; char c = getchar();

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

	while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}

	return x;

}



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

{

	e[++num].next = h[u];

	e[num].to = v;

	e[num].dis = w;

	h[u] = num;

}



void dfs(int x)

{

	if(flag) return;

	vis[x] = 1;

	for(int i = h[x]; i != 0; i = e[i].next)

	{

		int t = pos[x] + e[i].dis;

		if(pos[e[i].to] == inf) pos[e[i].to] = t, dfs(e[i].to);

		else if(t != pos[e[i].to]) {flag = 1; return;}

	}

}



int main()

{

	cin >> n >> m;

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

	{

		int a = read(), b = read(), c = read();

		add(a, b, c), add(b, a, -c);

		pos[a] = pos[b] = inf;

		obj[++cnt] = a, obj[++cnt] = b;

	}

	sort(obj + 1, obj + 1 + cnt);

	cnt = unique(obj + 1, obj + 1 + cnt) - obj - 1;

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

		if(!vis[obj[i]])

		{

			flag = 0;

			pos[obj[i]] = 0;

			dfs(obj[i]);

			if(flag) {cout << "No"; return 0;}

		}

	cout << "Yes";

	return 0;

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

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050

# define M 200050 

# define WQD 0x7f7f7f7f

int n;

int m;

struct Edge{int next,to, w;}  e[M<<1];

int head[N+1]; 

int dis[N+1];

bool vis[N+1];

bool book[ N + 1 ] ;

bool spfa(int s);

using namespace std;

int main()

{

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

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

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

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

	cin>>n>>m;

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

	{

		cin>>u>>v>>w;

		e[i]=(Edge){head[u],v,w};head[u]=i;

		e[m+i]=(Edge){head[v],u,-w};head[v]=m+i;

	}

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

		if(!vis[i]) 

		{

			vis[i]=1;

			if(!spfa(i))  {cout<<"No";return 0;}

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

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

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

	dis[s]=0;

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

	while(q.size()>0)

	{

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

		book[k]=0;

		for(int i=head[k]; i!=-1; i=e[i].next)

		{

			int v=e[i].to;

			if(dis[v]==WQD ) 

			{				

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

				if( ! book[v])	{q.push(v);	book[v]=1;}

				vis[v]=1;

			}

			else if(  (dis[v]!=dis[k]+e[i].w) )	return false;

		}		

    }

	return true;

}

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

#include <cstdio>

#include <algorithm>

#define N 400005

#define inf 0x7fffffff

using namespace std;



struct E {int next, to, dis;} e[N];

int n, m, num, flag, cnt;

int h[N], a[N], pos[N], obj[N];

bool vis[N];



int read()

{

	int x = 0; char c = getchar();

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

	while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}

	return x;

}



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

{

	e[++num].next = h[u];

	e[num].to = v;

	e[num].dis = w;

	h[u] = num;

}



void dfs(int x)

{

	if(flag) return;

	vis[x] = 1;

	for(int i = h[x]; i != 0; i = e[i].next)

	{

		int t = pos[x] + e[i].dis;

		if(pos[e[i].to] == inf) pos[e[i].to] = t, dfs(e[i].to);

		else if(t != pos[e[i].to]) {flag = 1; return;}

	}

}



int main()

{

	cin >> n >> m;

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

	{

		int a = read(), b = read(), c = read();

		add(a, b, c), add(b, a, -c);

		pos[a] = pos[b] = inf;

		obj[++cnt] = a, obj[++cnt] = b;

	}

	sort(obj + 1, obj + 1 + cnt);

	cnt = unique(obj + 1, obj + 1 + cnt) - obj - 1;

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

		if(!vis[obj[i]])

		{

			flag = 0;

			pos[obj[i]] = 0;

			dfs(obj[i]);

			if(flag) {cout << "No"; return 0;}

		}

	cout << "Yes";

	return 0;

}