stablegradients/pie-gem5-pairs huggingface dataset:
// 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()

{

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

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

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

    {

        scanf("%d%d%d",&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)printf("Yes");

    else printf("No");

}


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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

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].l] != dis[s[k].r])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

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

using namespace std;



#define ll long long



int n,m;

int L,R,D;

vector<pair<int,int> >mp[100100];

int dis[100100];

bool vis[100100];

int flag;



void dfs(int u,int pre)

{

    if(!flag) return;

     int sz=mp[u].size();

     pair<int,int>tmp;

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

     {

        tmp=mp[u][i];

        if(vis[tmp.first])

        {

            if(dis[u]+tmp.second!=dis[tmp.first])

            {

                flag=0;return;

            }

        }

        else

        {

            vis[tmp.first]=1;

            dis[tmp.first]=dis[u]+tmp.second;

            dfs(tmp.first,u);

        }

     }

}



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

{

    /* code */

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

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

    {

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

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

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

    }

    flag=1;

    for(int i=1;i<=n;++i) if(!vis[i])

    {

        vis[i]=1;

        dfs(i,-1);

    }

    if(flag) puts("Yes");else puts("No");

    return 0;

}

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

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].l] != dis[s[k].r])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

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

{

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

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

    memset(dis,2000000000,sizeof(dis));

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

}


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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

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].l] != dis[s[k].r])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

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

using namespace std;



#define ll long long



int n,m;

int L,R,D;

vector<pair<int,int> >mp[100100];

int dis[100100];

bool vis[100100];

int flag;



void dfs(int u,int pre)

{

    if(!flag) return;

     int sz=mp[u].size();

     pair<int,int>tmp;

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

     {

        tmp=mp[u][i];

        if(vis[tmp.first])

        {

            if(dis[u]+tmp.second!=dis[tmp.first])

            {

                flag=0;return;

            }

        }

        else

        {

            vis[tmp.first]=1;

            dis[tmp.first]=dis[u]+tmp.second;

            dfs(tmp.first,u);

        }

     }

}



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

{

    /* code */

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

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

    {

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

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

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

    }

    flag=1;

    for(int i=1;i<=n;++i) if(!vis[i])

    {

        vis[i]=1;

        dfs(i,-1);

    }

    if(flag) puts("Yes");else puts("No");

    return 0;

}

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

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].l] != dis[s[k].r])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

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

using namespace std;



#define ll long long



int n,m;

int L,R,D;

vector<pair<int,int> >mp[100100];

int dis[100100];

bool vis[100100];

int flag;



void dfs(int u,int pre)

{

    if(!flag) return;

     int sz=mp[u].size();

     pair<int,int>tmp;

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

     {

        tmp=mp[u][i];

        if(vis[tmp.first])

        {

            if(dis[u]!=dis[tmp.first]-tmp.second)

            {

                flag=0;return;

            }

        }

        else

        {

            vis[tmp.first]=1;

            dis[tmp.first]=dis[u]+tmp.second;

            dfs(tmp.first,u);

        }

     }

}



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

{

    /* code */

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

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

    {

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

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

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

    }

    flag=1;

    for(int i=1;i<=n;++i) if(!vis[i])

    {

        vis[i]=1;

        dfs(i,-1);

    }

    if(flag) puts("Yes");else puts("No");

    return 0;

}

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

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].l] != dis[s[k].r])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

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

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

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

    {

        scanf("%d%d%d",&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;

            dfs(i);

        }

    }

    if(flag)printf("Yes");

    else printf("No");

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

# include <cstdio>

# include <iostream>

# include <queue>

# define N 100050

# define M 200050 

# define WQD 0x7f7f7f7f

int n;

int m;

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

int head[N+1]; 

int dis[N+1];

bool vis[N+1];

bool book[ N + 1 ] ;

bool spfa(int s);

using namespace std;

int main()

{

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

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

	ios::sync_with_stdio ( false ) ;

	int u,v,w;

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

	cin>>n>>m;

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

	{

		cin>>u>>v>>w;

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

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

	}

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

		if(!vis[i]) 

		{

			vis[i]=1;

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

		}

	cout<<"Yes";

	return 0;

}

bool spfa(int s)

{

 	queue<int>q;

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

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

	dis[s]=0;

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

	while(q.size()>0)

	{

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

		book[k]=0;

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

		{

			int v=e[i].to;

			if(dis[v]==WQD ) 

			{				

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

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

				vis[v]=1;

			}

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

		}		

    }

	return true;

}

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

	}

	//一组数据为一个问题，不能将fill数组放在spfa里 

	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;

}
// 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 )//如果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]!=dis[k]+e[i].w)	return false;//这里也可以用不等于，根据题意理解差分系统约束条件 

		}		

    }

	return true;//别掉了这句 

}

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

	}

	//一组数据为一个问题，不能将fill数组放在spfa里 

	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;

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

# include <iostream>

# include <cstring>

# include <algorithm>

# include <queue>

# define N 100001

# define M 200001

# define _cin ios::sync_with_stdio(false)

# define Inf 0x7f7f7f7f

using namespace std;

struct ege

{

	int next;

	int to;

	int w;

}e[M<<1];

int n;//人数 

int m;//信息数 

int book[N+1];

int head[N+1];

int dis[N+1];

bool vis[N+1];

bool spfa(int x);

int main()

{

	_cin;

	

	int u,v,w;

	cin>>n>>m;

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

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

	{

		cin>>u>>v>>w;

		//建立双向边 

		/*e[i].next=head[u];

		e[i].to=v;

		e[i].w=w;

		head[u]=i;

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

		e[i+m].to=u;

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

		head[v]=i+m;

		*/

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

		e[m+i]=(ege){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"<<endl;return 0;}

	} 

	cout<<"Yes"<<endl;

	return 0;

}

bool spfa(int x)

{

	queue <int>q;

	dis[x]=0;

	q.push(x);book[x]=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)

		{

			if(dis[e[i].to]==Inf && dis[k]<Inf)

			{

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

				//如果下一个点没有被标记，将这个点标记，进队列 

				if(book[e[i].to]==0)	{q.push(e[i].to);book[e[i].to]=1;}

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

			} 

			//与已知矛盾，即任意两点的距离都不能有两个值 

			//表示k点已被改变值，下一个点也已经改变值，但是新值与旧值不等 

			else if(dis[k]<Inf && dis[e[i].to]<Inf && dis[e[i].to]!=dis[k]+e[i].w) return false;

		} 

	}

	return true;

}

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

	}

	//一组数据为一个问题，不能将fill数组放在spfa里 

	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;

}