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

}

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

using namespace std;

int n, m, ERR;

int f[100010], val[100010];

int find(int x) {

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

	int px = f[x];

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

	val[x] += val[px];

	return f[x];

}

void merge(int x, int y, int d) {

	int px = find(x);

	int py = find(y);

	f[px] = f[py];

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

}

int main() {

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

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

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

		int x, y, d;

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

		int px = find(x), py = find(y);

		if(px == py && val[x] - val[y] != d) ERR = 1;

		else if(px != py) {

			f[px] = py;

			val[px] = -val[x] + val[y] + d;

		}

	}

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

	else printf("Yes\n");

	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.8227105492176765x):
#include<bits/stdc++.h>

using namespace std;

int n, m, ERR;

int f[100010], val[100010];

int find(int x) {

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

	int px = f[x];

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

	val[x] += val[px];

	return f[x];

}

void merge(int x, int y, int d) {

	int px = find(x);

	int py = find(y);

	f[px] = f[py];

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

}

int main() {

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

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

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

		int x, y, d;

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

		int px = find(x), py = find(y);

		if(px == py && val[x] - val[y] != d) ERR = 1;

		else if(px != py) {

			f[px] = py;

			val[px] = -val[x] + val[y] + d;

		}

	}

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

	else printf("Yes\n");

	return 0;

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

}

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

using namespace std;

int n, m, ERR;

int f[100010], val[100010];

int find(int x) {

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

	int px = f[x];

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

	val[x] += val[px];

	return f[x];

}

void merge(int x, int y, int d) {

	int px = find(x);

	int py = find(y);

	f[px] = f[py];

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

}

int main() {

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

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

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

		int x, y, d;

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

		int px = find(x), py = find(y);

		if(px == py && val[x] - val[y] != d) ERR = 1;

		else if(px != py) {

			f[px] = py;

			val[px] = -val[x] + val[y] + d;

		}

	}

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

	else printf("Yes\n");

	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.3564777130655084x):
#include <bits/stdc++.h>

#define R register int

using namespace std;



const int N = 1e5 + 5, M = 2e5 + 5;



struct Graph

{

    int head[N], to[M << 1], next[M << 1], v[M << 1], cnt;



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

    {

        to[++cnt] = y, next[cnt] = head[x], v[cnt] = v_, head[x] = cnt;

    }

} e;

int n, m;



queue<int> q;

int dis[N];

bool vis[N];

bool spfa(int st)

{

    vis[st] = 1, dis[st] = 0, q.push(st);

    while (!q.empty())

    {

        int x = q.front();

        q.pop();

        for (R i = e.head[x]; i; i = e.next[i])

        {

            int u = e.to[i];

            if (vis[u])

            {

                if (dis[x] + e.v[i] != dis[u]) return false;

            }

            else dis[u] = min(dis[u], dis[x] + e.v[i]), vis[u] = 1, q.push(u);

        }

    }

    return true;

}



int main()

{

    memset(dis, 0x3f, sizeof dis);

    ios::sync_with_stdio(false);

    cin >> n >> m;

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

    {

        int x, y, v;

        cin >> x >> y >> v;

        e.add_edge(x, y, v), e.add_edge(y, x, -v);

    }

    bool flag = true;

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

        if (!vis[i] && !spfa(i)) { flag = false; break; }

    if (flag) cout << "Yes\n";

    else cout << "No\n";

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

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

    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: 3.6133222488199688x):
#include <bits/stdc++.h>

#define R register int

using namespace std;



const int N = 1e5 + 5, M = 2e5 + 5;



struct Graph

{

    int head[N], to[M << 1], next[M << 1], v[M << 1], cnt;



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

    {

        to[++cnt] = y, next[cnt] = head[x], v[cnt] = v_, head[x] = cnt;

    }

} e;

int n, m;



queue<int> q;

int dis[N];

bool vis[N];

bool spfa(int st)

{

    vis[st] = 1, dis[st] = 0, q.push(st);

    while (!q.empty())

    {

        int x = q.front();

        q.pop();

        for (R i = e.head[x]; i; i = e.next[i])

        {

            int u = e.to[i];

            if (vis[u])

            {

                if (dis[x] + e.v[i] != dis[u]) return false;

            }

            else dis[u] = min(dis[u], dis[x] + e.v[i]), vis[u] = 1, q.push(u);

        }

    }

    return true;

}



int main()

{

    memset(dis, 0x3f, sizeof dis);

    ios::sync_with_stdio(false);

    cin >> n >> m;

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

    {

        int x, y, v;

        cin >> x >> y >> v;

        e.add_edge(x, y, v), e.add_edge(y, x, -v);

    }

    bool flag = true;

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

        if (!vis[i] && !spfa(i)) { flag = false; break; }

    if (flag) cout << "Yes\n";

    else cout << "No\n";

    return 0;

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

}

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

#define R register int

using namespace std;



const int N = 1e5 + 5, M = 2e5 + 5;



struct Graph

{

    int head[N], to[M << 1], next[M << 1], v[M << 1], cnt;



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

    {

        to[++cnt] = y, next[cnt] = head[x], v[cnt] = v_, head[x] = cnt;

    }

} e;

int n, m;



queue<int> q;

int dis[N];

bool vis[N];

bool spfa(int st)

{

    vis[st] = 1, dis[st] = 0, q.push(st);

    while (!q.empty())

    {

        int x = q.front();

        q.pop();

        for (R i = e.head[x]; i; i = e.next[i])

        {

            int u = e.to[i];

            if (vis[u])

            {

                if (dis[x] + e.v[i] != dis[u]) return false;

            }

            else dis[u] = min(dis[u], dis[x] + e.v[i]), vis[u] = 1, q.push(u);

        }

    }

    return true;

}



int main()

{

    memset(dis, 0x3f, sizeof dis);

    ios::sync_with_stdio(false);

    cin >> n >> m;

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

    {

        int x, y, v;

        cin >> x >> y >> v;

        e.add_edge(x, y, v), e.add_edge(y, x, -v);

    }

    bool flag = true;

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

        if (!vis[i] && !spfa(i)) { flag = false; break; }

    if (flag) cout << "Yes\n";

    else cout << "No\n";

    return 0;

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

}


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

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

}


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

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

}


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

}