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

#include<cstdio>

#include<algorithm>

#include<vector>

#define Max 100010

using namespace std;

struct node

{

    int n,d;

}push;

vector<node>  e[Max];

bool vis[Max];

int flag=0;

int dis[Max];

void dfs(int x)

{

    vis[x]=true;

    if(flag)

    return;

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

    {

        node tmp;

        tmp=e[x][i];

        if(vis[tmp.n])

        {

            if(dis[tmp.n]!=dis[x]+tmp.d)

            {

                flag=1;

                break;

            }

        }

        else

        {

            dis[tmp.n]=dis[x]+tmp.d;

            dfs(tmp.n);

        }

    }

}

int main()

{

    int N,M;

    int l,r,d;

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

    int i;

    for(i=0;i<M;i++)

    {

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

        push.n=r;

        push.d=d;

        e[l].push_back(push);

        push.n=l;

        push.d=-1*d;

        e[r].push_back(push);

    }

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

    {

        if(vis[i]==false)

        {

            dfs(i);

        }

        if(flag)

            break;

    }

    if(flag)

        printf("No\n");

    else printf("Yes\n");

    return 0;

}


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

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <iostream>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <bitset>

#include <vector>

using namespace std;



#define bll long long

#define dou double

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

#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)

#define Mem(a,b) memset(a,b,sizeof(a))

#define Cpy(a,b) memcpy(a,b,sizeof(b))



const int maxn=1e5+100;

int N,M,u[maxn],link[maxn];

int dlt[maxn];



int get_u(int x,int &ret)

{

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

    u[x]=get_u(u[x],ret);

    ret+=dlt[x];

    dlt[x]=ret;

    return u[x];

}



bool check(int x,int y,int d)

{

    int rx=0,ry=0;

    int a=get_u(x,rx),b=get_u(y,ry);

    if (a==b) return (dlt[y]-dlt[x]==d);

    if (link[a]<link[b]) swap(a,b),swap(x,y),d=-d;

    u[b]=a;

    link[a]+=(link[a]==link[b]);

    dlt[b]=dlt[x]-dlt[y]+d;

    return 1;

}



int main()

{

    for (; scanf("%d%d",&N,&M)!=EOF; )

    {

        bool flag=1;

        For(i,1,N) u[i]=i,link[i]=0,dlt[i]=0;

        For(i,1,M)

        {

            int x,y,d;

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

            if (flag) flag=check(x,y,d);

        }

        if (flag)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

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

#include <cstring>

#include <cstdio>

#include <vector>



using namespace std;



#define MAXN 100005



int N,M;

bool mark[MAXN];



vector<pair<int,int>> v[MAXN];

int len[MAXN];



bool DFS(int t)

{

	vector<pair<int,int>>::iterator it;

	for(it = v[t].begin() ; it!=v[t].end() ; it++)

	{

		int p = (*it).first;

		int val = (*it).second;

		if(!mark[p])

		{

			mark[p] = true;

			len[p] = len[t]+val;

			if(!DFS(p))return false;

		}

		else if(len[p] != len[t]+val)return false;

	}

	return true;

}



int main()

{

	cin>>N>>M;

	int L,R,D;

	while(M--)

	{

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

		v[L].push_back(make_pair(R,D));

		v[R].push_back(make_pair(L,-D));

	}

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

	{

		if(!mark[i])

		{

			mark[i] = true;

			if(!DFS(i))

			{

				printf("No\n");

				return 0;

			}

		}

	}

	printf("Yes\n");

	return 0;

}

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

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <iostream>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <bitset>

#include <vector>

using namespace std;



#define bll long long

#define dou double

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

#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)

#define Mem(a,b) memset(a,b,sizeof(a))

#define Cpy(a,b) memcpy(a,b,sizeof(b))



const int maxn=1e5+100;

int N,M,u[maxn],link[maxn];

int dlt[maxn];



int get_u(int x,int &ret)

{

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

    u[x]=get_u(u[x],ret);

    ret+=dlt[x];

    dlt[x]=ret;

    return u[x];

}



bool check(int x,int y,int d)

{

    int rx=0,ry=0;

    int a=get_u(x,rx),b=get_u(y,ry);

    if (a==b) return (dlt[y]-dlt[x]==d);

    if (link[a]<link[b]) swap(a,b),swap(x,y),d=-d;

    u[b]=a;

    link[a]+=(link[a]==link[b]);

    dlt[b]=dlt[x]-dlt[y]+d;

    return 1;

}



int main()

{

    for (; scanf("%d%d",&N,&M)!=EOF; )

    {

        bool flag=1;

        For(i,1,N) u[i]=i,link[i]=0,dlt[i]=0;

        For(i,1,M)

        {

            int x,y,d;

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

            if (flag) flag=check(x,y,d);

        }

        if (flag)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

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

}

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

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <iostream>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <bitset>

#include <vector>

using namespace std;



#define bll long long

#define dou double

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

#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)

#define Mem(a,b) memset(a,b,sizeof(a))

#define Cpy(a,b) memcpy(a,b,sizeof(b))



const int maxn=1e5+100;

int N,M,u[maxn],link[maxn];

int dlt[maxn];



int get_u(int x,int &ret)

{

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

    u[x]=get_u(u[x],ret);

    ret+=dlt[x];

    dlt[x]=ret;

    return u[x];

}



bool check(int x,int y,int d)

{

    int rx=0,ry=0;

    int a=get_u(x,rx),b=get_u(y,ry);

    if (a==b) return (dlt[y]-dlt[x]==d);

    if (link[a]<link[b]) swap(a,b),swap(x,y),d=-d;

    u[b]=a;

    link[a]+=(link[a]==link[b]);

    dlt[b]=dlt[x]-dlt[y]+d;

    return 1;

}



int main()

{

    for (; scanf("%d%d",&N,&M)!=EOF; )

    {

        bool flag=1;

        For(i,1,N) u[i]=i,link[i]=0,dlt[i]=0;

        For(i,1,M)

        {

            int x,y,d;

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

            if (flag) flag=check(x,y,d);

        }

        if (flag)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

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

#include <cstdio>

#include <map>

#include <algorithm>

#include <cstring>

#include <string>

#include <cmath>



using namespace std;



const int N = 1e5+5;

int n,m;

int fa[N],r[N];



void init()

{

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

    {

        fa[i] = i;

    }

    memset(r,0,sizeof(r));

}



int Find(int x)

{

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

    int root = Find(fa[x]);

    r[x] += r[fa[x]];

    return fa[x] = root;

}



bool Merge(int x, int y, int d)

{

    int fx = Find(x);

    int fy = Find(y);

    if(fx==fy)

    {

        return d == r[y]-r[x];

    }

    fa[fy] = fx;

    r[fy] = r[x] + d - r[y];

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&m) != EOF)

    {

        int ok = 1;

        int L,R,D;

        init();

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

        {

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

            if(ok && !Merge(L,R,D))

            {

                ok = 0;

            }

        }

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

        else printf("No\n");

    }

    return 0;

}


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

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <iostream>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <bitset>

#include <vector>

using namespace std;



#define bll long long

#define dou double

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

#define Rof(i,a,b) for (int i=(a),_##i=(b); i>=_##i; --i)

#define Mem(a,b) memset(a,b,sizeof(a))

#define Cpy(a,b) memcpy(a,b,sizeof(b))



const int maxn=1e5+100;

int N,M,u[maxn],link[maxn];

int dlt[maxn];



int get_u(int x,int &ret)

{

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

    u[x]=get_u(u[x],ret);

    ret+=dlt[x];

    dlt[x]=ret;

    return u[x];

}



bool check(int x,int y,int d)

{

    int rx=0,ry=0;

    int a=get_u(x,rx),b=get_u(y,ry);

    if (a==b) return (dlt[y]-dlt[x]==d);

    if (link[a]<link[b]) swap(a,b),swap(x,y),d=-d;

    u[b]=a;

    link[a]+=(link[a]==link[b]);

    dlt[b]=dlt[x]-dlt[y]+d;

    return 1;

}



int main()

{

    for (; scanf("%d%d",&N,&M)!=EOF; )

    {

        bool flag=1;

        For(i,1,N) u[i]=i,link[i]=0,dlt[i]=0;

        For(i,1,M)

        {

            int x,y,d;

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

            if (flag) flag=check(x,y,d);

        }

        if (flag)

            printf("Yes\n");

        else

            printf("No\n");

    }

    return 0;

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

#include <vector>

using namespace std;

 

int n,m; //input intergers:n,m

vector<pair<int,int> > e[100010]; //the vector array of DFS

long long pos[100010]; //the positions of each person 

bool vis[100010]; //the bool array of DFS to know each person has visited or not

 

inline void dfs(int x) //DFS

{

	vis[x]=1; //visited this person(x)

	for(int i=0;i<e[x].size();i++) //visit the person who has relation of this person(x)

	{

		pair<int,int> cur=e[x][i];

		if(vis[cur.first]) //if(this person(e[x][i])'s position has known)

		{

			if(pos[cur.first]-pos[x]!=cur.second) {cout<<"No";exit(0);} //if this person(e[x][i]) is wrongful then exit

		}

		else //if this person(e[x][i])'s position hasn't known then take notes and DFS

		{

			pos[cur.first]=pos[x]+cur.second;

			dfs(cur.first);

		}

	}

}

 

int main()

{

	cin>>n>>m; //input

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

	{

		int l,r,d;

		cin>>l>>r>>d; //input

		e[l].push_back({r,d}); //take notes

		e[r].push_back({l,-d}); //take notes

	}

	

	for(int i=0;i<n;i++) if(!vis[i]) dfs(i); //DFS

	

	cout<<"Yes"; //if every one isn't wrongful then print 'Yes'

	

	return 0; //END

}

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

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( ll (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( ll (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( ll (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define prll cout << endl

#define tri pair<pair<ll, ll>, ll>

using namespace std;



int n,m;

int cnt[100002], ans[100002];



void find(int x){

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

    find(ans[x]);

    cnt[x] += cnt[ans[x]], ans[x] = ans[ans[x]];

}



bool has = false;



int main(){

    while ( cin >> n >> m ) {

        MSET(cnt); MSET(ans); has = false;

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

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

            int l, r, d;

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

            find(l); find(r);

            if ( ans[l] == ans[r] ){

                if ( cnt[l] + d != cnt[r] ) {

                    has = true;

                }

            }

            else {

                cnt[ans[r]] = cnt[l] + d - cnt[r];

                ans[ans[r]] = ans[l];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

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

#include<cstdio>

#include<algorithm>

#include<vector>

#define Max 100010

using namespace std;

struct node

{

    int n,d;

}push;

vector<node>  e[Max];

bool vis[Max];

int flag=0;

int dis[Max];

void dfs(int x)

{

    vis[x]=true;

    if(flag)

    return;

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

    {

        node tmp;

        tmp=e[x][i];

        if(vis[tmp.n])

        {

            if(dis[tmp.n]!=dis[x]+tmp.d)

            {

                flag=1;

                break;

            }

        }

        else

        {

            dis[tmp.n]=dis[x]+tmp.d;

            dfs(tmp.n);

        }

    }

}

int main()

{

    int N,M;

    int l,r,d;

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

    int i;

    for(i=0;i<M;i++)

    {

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

        push.n=r;

        push.d=d;

        e[l].push_back(push);

        push.n=l;

        push.d=-1*d;

        e[r].push_back(push);

    }

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

    {

        if(vis[i]==false)

        {

            dfs(i);

        }

        if(flag)

            break;

    }

    if(flag)

        printf("No\n");

    else printf("Yes\n");

    return 0;

}


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

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( ll (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( ll (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( ll (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define prll cout << endl

#define tri pair<pair<ll, ll>, ll>

using namespace std;



int n,m;

int cnt[100002], ans[100002];



void find(int x){

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

    find(ans[x]);

    cnt[x] += cnt[ans[x]], ans[x] = ans[ans[x]];

}



bool has = false;



int main(){

    while ( cin >> n >> m ) {

        MSET(cnt); MSET(ans); has = false;

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

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

            int l, r, d;

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

            find(l); find(r);

            if ( ans[l] == ans[r] ){

                if ( cnt[l] + d != cnt[r] ) {

                    has = true;

                }

            }

            else {

                cnt[ans[r]] = cnt[l] + d - cnt[r];

                ans[ans[r]] = ans[l];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

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

#include <cstring>

#include <cstdio>

#include <vector>



using namespace std;



#define MAXN 100005



int N,M;

bool mark[MAXN];



vector<pair<int,int>> v[MAXN];

int len[MAXN];



bool DFS(int t)

{

	vector<pair<int,int>>::iterator it;

	for(it = v[t].begin() ; it!=v[t].end() ; it++)

	{

		int p = (*it).first;

		int val = (*it).second;

		if(!mark[p])

		{

			mark[p] = true;

			len[p] = len[t]+val;

			if(!DFS(p))return false;

		}

		else if(len[p] != len[t]+val)return false;

	}

	return true;

}



int main()

{

	cin>>N>>M;

	int L,R,D;

	while(M--)

	{

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

		v[L].push_back(make_pair(R,D));

		v[R].push_back(make_pair(L,-D));

	}

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

	{

		if(!mark[i])

		{

			mark[i] = true;

			if(!DFS(i))

			{

				printf("No\n");

				return 0;

			}

		}

	}

	printf("Yes\n");

	return 0;

}

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

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( ll (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( ll (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( ll (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define prll cout << endl

#define tri pair<pair<ll, ll>, ll>

using namespace std;



int n,m;

int cnt[100002], ans[100002];



void find(int x){

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

    find(ans[x]);

    cnt[x] += cnt[ans[x]], ans[x] = ans[ans[x]];

}



bool has = false;



int main(){

    while ( cin >> n >> m ) {

        MSET(cnt); MSET(ans); has = false;

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

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

            int l, r, d;

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

            find(l); find(r);

            if ( ans[l] == ans[r] ){

                if ( cnt[l] + d != cnt[r] ) {

                    has = true;

                }

            }

            else {

                cnt[ans[r]] = cnt[l] + d - cnt[r];

                ans[ans[r]] = ans[l];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

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

}

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

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <algorithm>

#include <limits.h>

#include <vector>

#include <map>

#include <stack>

#include <queue>

#include <math.h>

#define ll long long

#define MSET(x) memset((x), 0, sizeof((x)))

#define MMSET(x) memset((x), -1, sizeof((x)))

#define rep(i, a, b) for ( ll (i) = a; (i) < (b) ; (i) ++ )

#define repA(i, a, b) for ( ll (i) = a ;  (i) <= (b) ; (i) ++ )

#define repD(i, a, b) for ( ll (i) = a; (i) >= (b) ; (i) -- )

#define fi first

#define se second

#define pb push_back

#define mfind(m, item) ((m).find((item)) != (m).end())

#define mp(a,b) make_pair((a),(b))

#define mt(a,b,c) make_pair(make_pair((a),(b)), (c))

#define tfi first.first

#define tse first.second

#define tth second

#define prll cout << endl

#define tri pair<pair<ll, ll>, ll>

using namespace std;



int n,m;

int cnt[100002], ans[100002];



void find(int x){

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

    find(ans[x]);

    cnt[x] += cnt[ans[x]], ans[x] = ans[ans[x]];

}



bool has = false;



int main(){

    while ( cin >> n >> m ) {

        MSET(cnt); MSET(ans); has = false;

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

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

            int l, r, d;

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

            find(l); find(r);

            if ( ans[l] == ans[r] ){

                if ( cnt[l] + d != cnt[r] ) {

                    has = true;

                }

            }

            else {

                cnt[ans[r]] = cnt[l] + d - cnt[r];

                ans[ans[r]] = ans[l];

            }

        }

        cout << (has ? "No" : "Yes" ) << endl;

    }

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

#include <vector>

using namespace std;

 

int n,m; //input intergers:n,m

vector<pair<int,int> > e[100010]; //the vector array of DFS

long long pos[100010]; //the positions of each person 

bool vis[100010]; //the bool array of DFS to know each person has visited or not

 

inline void dfs(int x) //DFS

{

	vis[x]=1; //visited this person(x)

	for(int i=0;i<e[x].size();i++) //visit the person who has relation of this person(x)

	{

		pair<int,int> cur=e[x][i];

		if(vis[cur.first]) //if(this person(e[x][i])'s position has known)

		{

			if(pos[cur.first]-pos[x]!=cur.second) {cout<<"No";exit(0);} //if this person(e[x][i]) is wrongful then exit

		}

		else //if this person(e[x][i])'s position hasn't known then take notes and DFS

		{

			pos[cur.first]=pos[x]+cur.second;

			dfs(cur.first);

		}

	}

}

 

int main()

{

	cin>>n>>m; //input

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

	{

		int l,r,d;

		cin>>l>>r>>d; //input

		e[l].push_back({r,d}); //take notes

		e[r].push_back({l,-d}); //take notes

	}

	

	for(int i=0;i<n;i++) if(!vis[i]) dfs(i); //DFS

	

	cout<<"Yes"; //if every one isn't wrongful then print 'Yes'

	

	return 0; //END

}

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

#pragma GCC optimize("Ofast")

#pragma GCC optimize("no-stack-protector")

#pragma GCC optimize("O3")

#pragma GCC optimize("fast-math")



using namespace std;



#define int long long



vector<vector<pair<int, int>>> g;

vector<int> used;

const int INF = 1e18;



void dfs(int v, int t) {

    used[v] = t;

    for (auto it: g[v]) {

        if (used[it.first] == INF) dfs(it.first, t + it.second);

        else if (used[it.first] != t + it.second) {

            cout << "No";

            exit(0);

        }

    }

}



signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    int n, m; cin >> n >> m;

    g.resize(n);

    used.assign(n, INF);

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

        int l, r, d; cin >> l >> r >> d;

        l--; r--;

        g[l].push_back({r, d});

        g[r].push_back({l, -d});

    }

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

        for (int j = 0; j < g[i].size(); ++j) cout << g[i][j].first << " " << g[i][j].second << "   ";

        cout << endl;

    }*/

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

        if (used[i] == INF) {

            used[i] = 0;

            dfs(i, 0);

        }

    }

    cout << "Yes";

}