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

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;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef double lf;

#define reg register

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

#define input(a){a=0;char c=gc();int f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc();}while(c>='0'&&c<='9'){a=(a<<3)+(a<<1)+(c&15);c=gc();}a*=f;}

static char buf[1<<21],*p1=buf,*p2=buf;

char bu[1<<21],cha[20];int p,pp=-1;

#define flush(){fwrite(bu,1,pp+1,stdout),pp=-1;}

#define output(x){if(pp>1<<20)flush();if(x<0)bu[++pp]=45,x=-x;do{cha[++p]=x%10+48;}while(x/=10);do{bu[++pp]=cha[p];}while(--p);}

#define Endl bu[++pp]='\n'

#define Space bu[++pp]=' '

#define pc(c) bu[++pp]=c

const int N=100010;

int n,m,f[N],d[N];

bool ok=1;

int find(int x)

{

    if(f[x]!=x){int ff=f[x];f[x]=find(f[x]);d[x]+=d[ff];}

    return f[x];

}

int main()

{

    input(n);input(m);

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

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

	{

        int l,r,x;

        input(l);input(r);input(x);

        int fl=find(l),fr=find(r);

        if(fl==fr&&d[l]-d[r]!=x){ok=0;break;}

        if(fl!=fr){f[fl]=fr;d[fl]=x+d[r]-d[l];}

    }

    if(ok)puts("Yes");

    else puts("No");

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef double lf;

#define reg register

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

#define input(a){a=0;char c=gc();int f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc();}while(c>='0'&&c<='9'){a=(a<<3)+(a<<1)+(c&15);c=gc();}a*=f;}

static char buf[1<<21],*p1=buf,*p2=buf;

char bu[1<<21],cha[20];int p,pp=-1;

#define flush(){fwrite(bu,1,pp+1,stdout),pp=-1;}

#define output(x){if(pp>1<<20)flush();if(x<0)bu[++pp]=45,x=-x;do{cha[++p]=x%10+48;}while(x/=10);do{bu[++pp]=cha[p];}while(--p);}

#define Endl bu[++pp]='\n'

#define Space bu[++pp]=' '

#define pc(c) bu[++pp]=c

const int N=100010;

int n,m,f[N],d[N];

bool ok=1;

int find(int x)

{

    if(f[x]!=x){int ff=f[x];f[x]=find(f[x]);d[x]+=d[ff];}

    return f[x];

}

int main()

{

    input(n);input(m);

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

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

	{

        int l,r,x;

        input(l);input(r);input(x);

        int fl=find(l),fr=find(r);

        if(fl==fr&&d[l]-d[r]!=x){ok=0;break;}

        if(fl!=fr){f[fl]=fr;d[fl]=x+d[r]-d[l];}

    }

    if(ok)puts("Yes");

    else puts("No");

    return 0;

}

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

#include<queue>

#include<iostream>

#include<cstring>

using namespace std;

int hd[100010],nxt[400010],edg[400010],ver[400010];

int n,m;

int l,r,dd;

int tot,nuuum;

int dist[100010],visnum[100010];

bool ins[100010];

int mmnb;

inline int rd()

{

    char ch;

    int res;

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

    res=ch-'0';

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

    return res;

}

inline void add_edge(int x,int y,int z)

{

    ver[++tot]=y,edg[tot]=z,nxt[tot]=hd[x],hd[x]=tot;

}

int main()

{

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

    n=rd(),m=rd();

    if(m==0) {printf("Yes");return 0;}

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

    {

        l=rd(),r=rd(),dd=rd();

        add_edge(r,l,dd);

        add_edge(l,r,-dd);

    }

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

    {

        if(dist[ttt]==1061109567)

        {

            ++nuuum;

            int s=ttt;

            queue<int> que;

    int summ=1;

    que.push(s);

    visnum[s]=nuuum;

    while(!que.empty())

    {

        int ttemp=que.front();

        que.pop();

        for(int m=hd[ttemp];m;m=nxt[m])

        {

            int y=ver[m];

            if(visnum[y]==0) {visnum[y]=nuuum,++summ;que.push(y);}

        }

    }

    queue<int> q;

    q.push(s);

    dist[s]=0;

    while(!q.empty())

    {

        int y=q.front();

        q.pop();

        ins[y]=0;

        for(int i=hd[y];i;i=nxt[i])

        {

            int z=ver[i];

            if(visnum[z]==nuuum)

            {

                if(dist[z]>dist[y]+edg[i])

                {

                	if(dist[z]==1061109567) {dist[z]=dist[y]+edg[i];q.push(z);}

                }

                else

                if(dist[z]!=dist[y]+edg[i]) {printf("No");return 0;}

            }

        }

    }

        }

    }

    printf("Yes");

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef double lf;

#define reg register

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

#define input(a){a=0;char c=gc();int f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc();}while(c>='0'&&c<='9'){a=(a<<3)+(a<<1)+(c&15);c=gc();}a*=f;}

static char buf[1<<21],*p1=buf,*p2=buf;

char bu[1<<21],cha[20];int p,pp=-1;

#define flush(){fwrite(bu,1,pp+1,stdout),pp=-1;}

#define output(x){if(pp>1<<20)flush();if(x<0)bu[++pp]=45,x=-x;do{cha[++p]=x%10+48;}while(x/=10);do{bu[++pp]=cha[p];}while(--p);}

#define Endl bu[++pp]='\n'

#define Space bu[++pp]=' '

#define pc(c) bu[++pp]=c

const int N=100010;

int n,m,f[N],d[N];

bool ok=1;

int find(int x)

{

    if(f[x]!=x){int ff=f[x];f[x]=find(f[x]);d[x]+=d[ff];}

    return f[x];

}

int main()

{

    input(n);input(m);

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

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

	{

        int l,r,x;

        input(l);input(r);input(x);

        int fl=find(l),fr=find(r);

        if(fl==fr&&d[l]-d[r]!=x){ok=0;break;}

        if(fl!=fr){f[fl]=fr;d[fl]=x+d[r]-d[l];}

    }

    if(ok)puts("Yes");

    else puts("No");

    return 0;

}

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

#include<cstring>

#include<iostream>

#include<string>

#include<map>

#include<vector>

#include<queue>

#include<set>

#include<stack>

#include<sstream>

#include<cmath>

#include<algorithm>

using namespace std;

typedef long long ll;

const int N=100000+10;

int n,m,r[N],D[N],L,R,d;

bool flag;

int find(int now){

    if(r[now]==-1) return now;

    int root=find(r[now]);

    D[now]+=D[r[now]];

    r[now]=root;

    return root;

}

int main() {

    while(~scanf("%d%d",&n,&m)){

        flag=0;

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

        memset(D,0,sizeof(D));

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

            scanf("%d%d%d",&L,&R,&d);

            if(flag) continue;

            int a=find(L),b=find(R);

            if(a==b){

                if(abs(D[R]-D[L])!=d) {flag=1;}

            }

            else {

                r[a]=b;

                D[a]=D[R]-D[L]+d;

            }

        }

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



    }

    return 0;

}


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

#include<cmath>

#include<cstring>

using namespace std;

const int N=100000+10;

int n,m,R[N],D[N],l,r,d;

bool flag;

int find(int now){

    if(R[now]==-1) return now;

    int root=find(R[now]);

    D[now]+=D[R[now]];//更新距离

    R[now]=root;

    return root;

}

int main() {

    while(~scanf("%d%d",&n,&m)){

        flag=0;

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

        memset(D,0,sizeof(D));

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

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

            if(flag) continue;

            int a=find(l),b=find(r);

            if(a==b){

                if(abs(D[r]-D[l])!=d) flag=1;

            }

            else {

                R[a]=b;

                D[a]=D[r]-D[l]+d;//直接更新点L的根相对于点R的根相对距离

            }

        }

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



    }

    return 0;

}


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

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

#define REPI(itr, v) for(auto itr = (v).begin(); itr != (v).end(); ++itr)

#define MAX_N 100000



using namespace std;





vector<pair<int, int>> G_1[MAX_N], G_2[MAX_N];



bool is_line(int N){



  vector<int> is_visit(N, 0);

  vector<int> x(N, 0);



  REP(i, N){

    if(is_visit[i]){

      continue;

    }

    

    queue<int> que;



    que.push(i);

    is_visit[i] = 1;

    while(!que.empty()){

      int j = que.front();

      que.pop();

      

      REPI(itr, G_1[j]){

	int k = itr->first;

	int x_new = x[j] + itr->second;

	if(is_visit[k]){

	  if(x[k] != x_new){

	    return false;

	  }

	}else{

	  que.push(k);

	  is_visit[k] = 1;

	  x[k] = x_new;

	}

      }



      REPI(itr, G_2[j]){

	int k = itr->first;

	int x_new = x[j] - itr->second;

	if(is_visit[k]){

	  if(x[k] != x_new){

	    return false;

	  }

	}else{

	  que.push(k);

	  is_visit[k] = 1;

	  x[k] = x_new;

	}

      }

    }

  }



  return true;

}



int main(void){



  int N, M;

  cin >> N >> M;



  REP(i, M){

    int L, R, D;

    cin >> L >> R >> D;

    G_1[L-1].push_back({R-1, D});

    G_2[R-1].push_back({L-1, D});

  }



  cout << (is_line(N) ? "Yes" : "No") << endl;



  return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

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

#define REPI(itr, v) for(auto itr = (v).begin(); itr != (v).end(); ++itr)



using namespace std;



typedef struct{

  int to,cost;

} edge;





bool is_line(vector<vector<edge>> G){



  int N = G.size();

  vector<bool> is_visit(N, false);

  vector<int> x(N, 0);



  REP(i, N){

    if(is_visit[i]){

      continue;

    }

    

    queue<int> que;

    que.push(i);

    while(!que.empty()){

      int j = que.front();

      que.pop();

      REPI(itr, G[j]){

	int k = itr->to;

	int x_new = x[j] + itr->cost;

	if(is_visit[k]){

	  if(x[k] != x_new){

	    return false;

	  }

	}else{

	  que.push(k);

	  is_visit[k] = true;

	  x[k] = x_new;

	}

      }

    }

  }



  return true;

}



int main(void){



  cin.tie(0);

  ios::sync_with_stdio(false);



  int N, M;

  cin >> N >> M;



  vector<vector<edge>> G(N);

  REP(i, M){

    int L, R, D;

    cin >> L >> R >> D;

    edge e1, e2;

    e1.to = R-1, e1.cost = D;

    e2.to = L-1, e2.cost = -1 * D;

    G[L-1].push_back(e1);

    G[R-1].push_back(e2);

  }



  cout << (is_line(G) ? "Yes" : "No") << endl;



  return 0;

}


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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

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

        {

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

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

            dfs(s[k].r);

        }

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

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

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

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

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

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

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

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

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

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

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

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#define MAXN 100500

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

		return z == (dis[y] - dis[x]);

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();int x, y, z;

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

	{

		

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}

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

#include <algorithm>

#include <string.h>

#include <stdio.h>

using namespace std;

#define ll long long

const int pmax = 100005;

const int emax = 400005;

struct node { int to; int w; int next; } edge[emax];

int head[pmax], id = 0, vis[pmax];

int dis[pmax];

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

    edge[id].to = y;

    edge[id].w = w;

    edge[id].next = head[x];

    head[x] = id++;

}



bool dfs(int u) {

    vis[u] = 1;

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

        int v = edge[i].to;

        if (!vis[v]) {

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

            if (!dfs(v)) return false;

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

    }

    return true;

}

int main(int argc, char const *argv[])

{

    int n, m;

    int a, b, c;

    memset(head, -1, sizeof head);

    memset(vis, 0, sizeof vis);

    memset(dis, 0, sizeof dis);

    cin >> n >> m;

    while (m--) {

        cin >> a >> b >> c;

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

    }

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

        if (!vis[i]) {

            if (!dfs(i)) return 0*printf("No\n");

        }

    printf("Yes\n");

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#define MAXN 100500

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

		return z == (dis[y] - dis[x]);

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();int x, y, z;

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

	{

		

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}

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

#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

const int MAXN=1e5+5;

int p[MAXN],vis[MAXN],error;

struct node{

	int x,y;

}push;

int n,m;

vector <node> vec[MAXN];

void solve(int x){

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

		node g=vec[x][i];

		if (vis[g.x])

		  if (g.y+p[x]!=p[g.x]){

		  	error=1;		

		  	return;

		  }

		  else continue;

		 vis[g.x]=1;

		 p[g.x]=p[x]+g.y;

		 solve(g.x);

		 if (error) return;

	}

}

int main(){

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

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

		memset(p,0,sizeof(p));

		error=0;

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

			int a,b,l;

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

			push.x=b;

			push.y=l;

			vec[a].push_back(push);

			push.x=a;

			push.y=-l;

			vec[b].push_back(push);

		}

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

		  if (!vis[i]){

		  	solve(i);

		  	if (error) break;

		  }

		if (!error) printf("Yes\n");

		else printf("No\n");

	

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#define MAXN 100500

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

		return z == (dis[y] - dis[x]);

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();int x, y, z;

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

	{

		

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}

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

#include<cstdio>

using namespace std;

int fa[100005],dis[100005];



int find(int x)

{

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

	int fx=find(fa[x]);

	dis[x]+=dis[fa[x]];

	fa[x]=fx;

	return fx;

}

int main()

{

	int n,m;

	bool f=true;

	int l,r,d;

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

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

	{

		fa[i]=i;dis[i]=0;

	}

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

	{

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

		if (!f) continue;

		int fl=find(l),fr=find(r);

		if (fl==fr)

		{

			if (dis[l]-dis[r]!=d) f=false;

	    }

	    else 

	    {

	    	fa[fl]=fr;

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

	   }

   }

   if (f) printf("Yes\n");

   else printf("No\n");

   return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#define MAXN 100500

int N, M;

int f[MAXN], dis[MAXN];

void init()

{

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

	{

		f[i] = i;

	}

}

int find(int x)

{

	if (f[x] == x)

		return x;

	else

	{

		int r = find(f[x]);

		dis[x] = dis[f[x]] + dis[x];

		return f[x] = r;

	}

}

int find_father(int x,int y,int z)

{

	int fx = find(x);

	int fy = find(y);

	if (fx == fy)

	{

		return z == (dis[y] - dis[x]);

	}

	f[fy] = fx;

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

	return 1;

}

int main()

{

	int a = 1;

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

	init();int x, y, z;

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

	{

		

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

		if (a)

			a = find_father(x, y, z);

	}

	if (a)

		printf("Yes\n");

	else

		printf("No\n");

	return 0;

}

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

using namespace std;



int n,m;

vector <pair<int,int> > a[200002];

bool vis[100001];

int d[100001];



bool dfs(int x,int y) {

	if (vis[x]) return d[x]!=y;

	vis[x]=true;

	d[x]=y;

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

		if (dfs(a[x][i].first,d[x]+a[x][i].second)) return true;

	}

	return false;

}



int main(){

	memset(vis,false,sizeof(vis));

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

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

		int x,y,z;

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

		x--;y--; 

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

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

	}

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

		if (!vis[i]&&dfs(i,0)) {

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<string.h>

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

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

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

   if(lose) printf("Yes\n");

   else     printf("No\n");

}

// Fastest known solution for this problem: