stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
// D - People on a Line

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

#define rp(i,s,e) for(int i=(s);i<(e);++i)

using VVII = vector<vector<pair<int,int>>>;



#define V_MAX 100000

vi visited(V_MAX+1);

vi dist(V_MAX+1);



void dfs(VVII &T, int p, int w){

	if(visited[p]) return;

	visited[p] = true;

	dist[p] = w;

	for(auto cw:T[p]){

		int np,nw; tie(np,nw) = cw;

		dfs(T, np, w+nw);

	}

}



int main(){

	int N,M; cin>>N>>M;

	if(M==0){ puts("Yes"); return 0; }



	VVII V(N+1);

	int l,r,d, L = INT_MAX, R = 0;

	rp(i, 0, M){

		cin>>l>>r>>d;

		V[l].emplace_back(r, d);

		V[r].emplace_back(l,-d);

		L = min(L, l);

		R = max(R, r);

	}



	rp(l, L, R+1){

		dfs(V, l, 0);

		for(auto rd:V[l]){

			tie(r,d) = rd;

			if(abs(d) != abs(dist[r]-dist[l])){ puts("No"); return 0; }

		}

	}

	puts("Yes");

}

// Faster C++ solution for the same problem (measured speedup: 1.2882951482090457x):
// D - People on a Line

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

#define rp(i,s,e) for(int i=(s);i<(e);++i)

using VVII = vector<vector<pair<int,int>>>;



#define V_MAX 100000

vi dist(V_MAX+1, INT_MAX);



void dfs(VVII &T, int p, int w){

	if(dist[p] != INT_MAX) return;

	dist[p] = w;

	for(auto cw:T[p]){

		int np,nw; tie(np,nw) = cw;

		dfs(T, np, w+nw);

	}

}



int main(){

	int N,M; cin>>N>>M;

	if(M==0){ puts("Yes"); return 0; }



	VVII T(N+1);

	int l,r,d, L = INT_MAX, R = 0;

	rp(i, 0, M){

		cin>>l>>r>>d;

		T[l].emplace_back(r, d);

		T[r].emplace_back(l,-d);

		L = min(L, l);

		R = max(R, r);

	}



	rp(l, L, R+1){

		dfs(T, l, 0);

		for(auto rd:T[l]){

			tie(r,d) = rd;

			if(abs(d) != abs(dist[r]-dist[l])){ puts("No"); return 0; }

		}

	}

	puts("Yes");

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

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <cmath>

#include <string>

#include <fstream>

#define FOR(i,a,b) for(int i=a;i<b;i++)

#define ROF(i,a,b) for(int i=b-1;i>=a;i--)

#define FI first

#define SE second

#define VE vector<int>

#define PB push_back

#define PA pair<int,int>

#define MA make_pair

#define LL long long

#define YES(i) cout<<(i?"YES":"NO")<<endl

#define Yes(i) cout<<(i?"Yes":"No")<<endl

using namespace std;

//

const int INF=1e9+7;

const int MOD=1e9+7;

//

int V,E;

vector<PA> G[100000];

int D[100000];

bool visited[100000];

bool bfs(int S){

  queue<PA> que;

  D[S]=0;

  visited[S]=true;

  que.push(MA(0,S));

  while(!que.empty()){

    PA P=que.front();que.pop();

    int v=P.SE;

    FOR(i,0,(int)G[v].size()){

      PA E=G[v][i];

      if(visited[E.FI]&&D[E.FI]!=D[v]+E.SE){

        return false;

      }

      if(!visited[E.FI]){

        visited[E.FI]=true;

        D[E.FI]=D[v]+E.SE;

        que.push(MA(D[E.FI],E.FI));

      }

    }

  }

  return true;

}

int main(){

  cin>>V>>E;

  FOR(i,0,E){

    int L,R,D;

    cin>>L>>R>>D;

    L--;R--;

    G[L].PB(MA(R,D));

    G[R].PB(MA(L,-D));

  }

  FOR(i,0,V){

    if(!visited[i]){

      if(!bfs(i)){

        Yes(0);

        return 0;

      }

    }

  }

  Yes(1);

  return 0;

}


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

#include <iostream>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <list>

#include <cmath>

#include <stack>

#include <string>

#include <cstring>

using namespace std;

//

const int inf=1e9+7;//0x3f

//



struct WeightUnionFind{

	//

	vector<int> ran,par,wei;

	//

	WeightUnionFind(int n){

		init(n);

	}

	//

	void init(int n){

		par.resize(n);

		ran.resize(n,0);

		wei.resize(n,0);

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

			par[i]=i;

		}

	}

	//

	int find(int x){

		if(par[x]==x){

			return x;

		}else{

			int r=find(par[x]);

			wei[x]+=wei[par[x]];

			return par[x]=r;

		}

	}

	//

	int weight(int x){

		find(x);

		return wei[x];

	}

	//

	bool same(int x,int y){

		return find(x)==find(y);

	}

	//

	bool unite(int x,int y,int w){

		w+=weight(x);

		w-=weight(y);

		x=find(x);

		y=find(y);

		if(x==y){

			return false;

		}else if(ran[x]<ran[y]){

			swap(x,y);

			w=-w;

		}else if(ran[x]==ran[y]){

			ran[x]++;

		}

		par[y]=x;

		wei[y]=w;

		return true;

	}

	//

	int diff(int x,int y){

		return weight(y)-weight(x);

	}

	//

};

int main(){

	int N,Q;

	cin>>N>>Q;

	WeightUnionFind UF(N);

	while(Q--){

		int L,R,D;

		cin>>L>>R>>D;

		L--;R--;

		if(UF.same(L,R)){

			if(UF.diff(L,R)==D){

				continue;

			}else{

				cout<<"No"<<endl;

				return 0;

			}

		}else{

			UF.unite(L,R,D);

		}

	}

	cout<<"Yes"<<endl;

	return 0;

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

#define rep(i, n) for (int i = 0; i < (n); i++)

#define REP(i, s, n) for (int i = s; i < (n); i++)

using namespace std;

typedef pair<int, int> P;

typedef long long ll;

ll INF = 1e15;

struct edge

{

    int to;

    int cost;

};

vector<edge> v[100010];

ll fl[100010];

int n, m;

bool dfs(int n)

{

    bool ans = true;

    rep(i, v[n].size())

    {

        if (fl[v[n][i].to] != INF)

        {

            if (fl[v[n][i].to] != fl[n] + v[n][i].cost)

            {

                ans = false;

            }

        }

        else

        {

            fl[v[n][i].to] = fl[n] + v[n][i].cost;

            ans &= dfs(v[n][i].to);

        }

    }

    return ans;

}



int main()

{

    cin >> n >> m;

    rep(i, m)

    {

        int l, r, d;

        cin >> l >> r >> d;

        edge e;

        e.to = r;

        e.cost = d;

        v[l].push_back(e);

        e.to = l;

        e.cost = -d;

        v[r].push_back(e);

    }

    rep(i, n + 1)

    {

        fl[i] = INF;

    }

    bool ans = true;

    rep(i, n + 1)

    {

        if (fl[i] == INF)

        {

            fl[i] = 0;

            ans &= dfs(i);

        }

    }

    if (ans)

    {

        cout << "Yes" << endl;

    }

    else

    {

        cout << "No" << endl;

    }

}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef pair<int, int> P;

typedef long long ll;

struct edge

{

    int to;

    int cost;

};

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    int n, m;

    cin >> n >> m;

    vector<vector<edge>> v(n);

    rep(i, m)

    {

        int l, r, d;

        cin >> l >> r >> d;

        l--;

        r--;

        edge e;

        e.to = r;

        e.cost = d;

        v[l].push_back(e);

        e.to = l;

        e.cost = -d;

        v[r].push_back(e);

    }

    vector<ll> dp(n, 1e18);

    queue<int> que;

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

    {

        if(dp[i]!=1e18){

            continue;

        }

        dp[i]=0;

        que.push(i);

        while (!que.empty())

        {

            int it = que.front();

            que.pop();

            for (auto e : v[it])

            {

                if (dp[e.to] == 1e18)

                {

                    dp[e.to] = dp[it] + e.cost;

                    que.push(e.to);

                }

                else

                {

                    if (dp[e.to] != dp[it] + e.cost)

                    {

                        cout << "No" << endl;

                        return 0;

                    }

                }

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

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

using namespace std;

typedef long long ll;

typedef vector<ll> vec;

typedef vector<vec> mat;

typedef pair<ll,ll> pll;

const ll mod=1e9+7;

//const ll mod=998244353;

const ll inf=1LL<<61;



const ll N=1e5+10;

vector<map<ll,ll>> G(N);

vec al(N);

vec d(N);

ll n,m;



ll dfs(ll f) {

  ll res=1;

  ll fd=d[f];

  al[f]=1;

  for(auto p:G[f]) {

    ll td=fd+p.second;

    ll t=p.first;

    if(al[t]) {

      if(d[t]!=td)return 0;

    }

    else {

      d[t]=td;

      if(!dfs(t)) res=0;

    }

  }

  return res;

}



int main() {

  cin >> n >> m;

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

    ll l,r,d;

    cin >> l >> r >> d;

    G[l][r]=d;

    G[r][l]=-d;

  }

  ll f=1;

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

    if(!al[i]&&!dfs(i)) f=0;

  }

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

  else cout << "No" << endl;

}

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

using namespace std;

typedef long long ll;

typedef vector<ll> vec;

typedef vector<vec> mat;

typedef pair<ll,ll> pll;

const ll mod=1e9+7;

//const ll mod=998244353;

const ll inf=1LL<<61;



const ll N=1e5+10;

vector<map<ll,ll>> G(N);

vec d(N,inf);



ll dfs(ll f) {

  for(auto p:G[f]) {

    ll t=p.first;

    ll td=d[f]+p.second;

    if(d[t]==inf) {

      d[t]=td;

      if(!dfs(t)) return 0;

    }

    else if(d[t]!=td){

      return 0;

    }

  }

  return 1;

}



int main() {

  ll n,m;

  cin >> n >> m;

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

    ll l,r,d;

    cin >> l >> r >> d;

    G[l][r]=d;

    G[r][l]=-d;

  }

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

    if(d[i]!=inf) continue;

    d[i]=0;

    if(!dfs(i)) {

      cout << "No" << endl;

      return 0;

    }

  }

  cout << "Yes" << endl;

}
// 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]) 

		{

			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)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{	//题目的约束条件是= 			

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

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

				vis[v]=1;//标记该点已经松弛过 

			}

			else if(dis[v]<WQD && dis[k]<WQD && dis[v]!=dis[k]+e[i].w)	return false;//据题意理解差分系统约束条件，不相等，自相矛盾 

		}		

    }

	return true;//别掉了这句 

}

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

#include<algorithm>

#include<cstdio>

#include<cmath>

#include<cstring>

#include<queue>

using namespace std;



const int N = 100010;

const int M = 200010;

const int INFM = 0x8c8c8c8c;



struct edge{

	int v , w , n ; 

}e[M<<1];int head[N];

int dis[N];

int n , m ;

bool book[N]; 

bool con[N];

queue< int > q ; 



bool spfa( ){

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

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

		if( !con[k] ){

			q.push( k );

			dis[k] = 0 ; 

			while( q.size() ){

				int ts = q.front();

				con[ts] = 1;

				q.pop();

				book[ts] = 0 ;

				for( int i = head[ts] ; i ; i = e[i].n ){

					int tv = e[i].v;					

					if( dis[tv] == INFM ){

						dis[tv] = dis[ts]+e[i].w ;

						if( !book[tv] )q.push( tv );

					}

					else if( dis[tv] != dis[ts] + e[i].w )

						return 0 ;

				}

			}

		}

	}

	return 1 ; 

}



int main()

{

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

	int u , v , w ; 

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

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

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

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

		head[v] = i ; head[u] = i + m ;

	}

	if( spfa( ) ) printf("Yes");

	else printf("No");

	return 0;

}
// 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]) 

		{

			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)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{	//题目的约束条件是= 			

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

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

				vis[v]=1;//标记该点已经松弛过 

			}

			else if(dis[v]<WQD && dis[k]<WQD && dis[v]!=dis[k]+e[i].w)	return false;//据题意理解差分系统约束条件，不相等，自相矛盾 

		}		

    }

	return true;//别掉了这句 

}

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

	else printf("Yes");

	return 0;

}
// Slower C++ solution:
//AT3882 people on a line

//2018/6/23 

#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <queue>

#define N 100001

#define M 200001

#define INF 0x7fffffff

#define ri register int

#define in inline

using namespace std;

struct edge

{

    int to,w,next;

}e[M<<1];

//vis数组表示已松弛的点

//book数组表示进入队列的点 

int head[N],vis[N],dis[N];

int m,n;

bool book[N];

in bool spfa(int s);

int main()

{

    ios::sync_with_stdio(false);

    int u,v,w;

    cin>>n>>m;

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

    {

        cin>>u>>v>>w;

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

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

    }

    //关于对dis数组放在外面的理解:

	//从第一个点开始时，如果有的点没有被松弛，即该点无法到达，起点与该点没有关系

	//则从没有被松弛的点开始，则是两个完全无关的图，dis数组有没有置0毫无关系 

    fill(dis+1,dis+n+1,INF);

    //所有约束条件需要考虑到

    //可能有多个分块，前10个约束条件与后10个 无关联 

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

        if(!vis[i])

        {

            if(spfa(i))

            {cout<<"No"; return 0;}

            vis[i]=1;

        }	

    cout<<"Yes";

    return 0;

}

in bool spfa(int s)

{

    queue <int> q;

    int k;

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

    dis[s]=0;

    while(q.size())

    {

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

        book[k]=0;//出队列应置为0 

        for(ri i=head[k]; i ;i=e[i].next)

        {

            int v=e[i].to;

            if(!vis[v]&& dis[k]<INF)

            {

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

                if(!book[v])

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

                vis[v]=1;

            }

            else if(dis[k]<INF && dis[v]<INF && dis[v]!=dis[k]+e[i].w) return true;

            //此时相当于自相矛盾

            //思想：即先认定一开始所给条件均正确（此时松弛）

            //最后再来验证接下来的条件是否满足原先条件 

        }

    }

    return false;

}

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

	else printf("Yes");

	return 0;

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

#include<algorithm>

#include<cstdio>

#include<cmath>

#include<cstring>

#include<queue>

using namespace std;



const int N = 100010;

const int M = 200010;

const int INFM = 0x8c8c8c8c;



struct edge{

	int v , w , n ; 

}e[M<<1];int head[N];

int dis[N];

int n , m ;

bool book[N]; 

bool con[N];

queue< int > q ; 



bool spfa( ){

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

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

		if( !con[k] ){

			q.push( k );

			dis[k] = 0 ; 

			while( q.size() ){

				int ts = q.front();

				con[ts] = 1;

				q.pop();

				book[ts] = 0 ;

				for( int i = head[ts] ; i ; i = e[i].n ){

					int tv = e[i].v;					

					if( dis[tv] == INFM ){

						dis[tv] = dis[ts]+e[i].w ;

						if( !book[tv] )q.push( tv );

					}

					else if( dis[tv] != dis[ts] + e[i].w )

						return 0 ;

				}

			}

		}

	}

	return 1 ; 

}



int main()

{

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

	int u , v , w ; 

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

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

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

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

		head[v] = i ; head[u] = i + m ;

	}

	if( spfa( ) ) printf("Yes");

	else printf("No");

	return 0;

} 

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

	else printf("Yes");

	return 0;

}
// 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]) 

		{

			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)//如果v点没松弛过，就松弛更新dis[v]的值 				

			{	//题目的约束条件是= 			

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

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

				vis[v]=1;//标记该点已经松弛过 

			}

			else if(dis[v]<WQD && dis[k]<WQD && dis[v]!=dis[k]+e[i].w)	return false;//据题意理解差分系统约束条件，不相等，自相矛盾 

		}		

    }

	return true;//别掉了这句 

}

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

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;

#define LL long long

#define R register

int n,m,fa[100005],val[100005],x,y,z;

int find(int x)

{

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

	int par=fa[x];

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

	val[x]+=val[par];

	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++)

	{

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

		int xx=find(x),yy=find(y);

		if(xx==yy)

		{

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

			{

				puts("No");

				return 0;

			}

		}

		else

		{

			fa[yy]=xx;

			val[yy]=val[x]+z-val[y];

		}

	}

	puts("Yes");

	return 0;

}