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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <queue>

#include <vector>

using namespace std;

struct node{

    int u,w;

};

node p;

vector<node>q[100005];

int visit[100005];

int dis[100005],flag;

void query(int x){

    if(flag==1) return ;

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

        node y=q[x][i];

        if(visit[y.u]==1){

            if(dis[y.u]!=dis[x]+y.w) {

                flag=1;

                return ;

            }

            else continue;

        }

        visit[y.u]=1;

        dis[y.u]=dis[x]+y.w;

        query(y.u);

    }



}

int main(){

    int n,m,k,u,w;

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

        memset(visit,0,sizeof(visit));

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

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

            p.u=u,p.w=w;

            q[k].push_back(p);

            p.u=k,p.w=-w;

            q[u].push_back(p);

        }

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

            if(visit[i]==0){

                flag=0;

                query(i);

            }

            if(flag==1){

                printf("No\n");

                break;

            }



        }

        if(flag==0) printf("Yes\n");

    }

    return 0;

}




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

#define MAXN 101000

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

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

	{

		int x, y, z;

		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;

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

#include <queue>

#include <stdio.h>

using namespace std;

int f=0;

int seedis(int x,int *arr,long long *dis)

{

    if(arr[x]==0) return 0;

    else return seedis(arr[x],arr,dis) + dis[x];

}

int seef(int l,int *arr,long long *dis)

{

    if(arr[l]==0) return l;

    int a1=arr[l];

    int a2=seef(arr[l],arr,dis);

    arr[l]=a2;

    dis[l]=dis[l]+dis[a1];

    return a2;

}

void hebing(int l,int r,int d,int *arr,long long *dis)

{

    int lf=seef(l,arr,dis);

    int rf=seef(r,arr,dis);

    if(rf==lf)

    {

        if(seedis(r,arr,dis)-seedis(l,arr,dis)!=d)

            f=1;

        return;

    }

    arr[rf]=lf;

    dis[rf]=seedis(l,arr,dis)+d-seedis(r,arr,dis);

}

int main()

{

    int n,m;

    int arr[100500];

    long long dis[100500];

    cin>>n>>m;

    int l,r,d;

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

    {

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

        hebing(l,r,d,arr,dis);

        if(f==1) break;

    }

    if(f) cout<<"No";

    else cout<<"Yes";

    return 0;

}


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

#define MAXN 101000

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

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

	{

		int x, y, z;

		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;

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn=1e5+5; 

vector <pair<int, int> > adj[maxn];

bool vis[maxn];

int x[maxn];



void dfs(int now,int dist)

{

	vis[now] = true;

	x[now] = dist;

 

	for (int i = 0; i < adj[now].size(); i++)

	{

		int next = adj[now][i].first;

		int d = adj[now][i].second;

		if (vis[next] == false)

		{

			dfs(next, dist + d);

		}

	}

}

 

int main()

{

	int n, m;

	int l, r, d;

	bool ok = true;

 

	cin.tie(0);

 

	while(cin >> n >> m)

	{

		ok=true;

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

	{

		cin >> l >> r >> d;

		adj[l].push_back(make_pair(r, d)); //关联点 

		adj[r].push_back(make_pair(l, -d));

	}

 

	memset(x, 0, sizeof(x));

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

 

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

	{

		if (vis[i]==false)

		{

			dfs(i, 0);

		}

	}



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

	{

		for (int j = 0; j < adj[i].size(); j++)

		{

			int v = adj[i][j].first;

			if (adj[i][j].second != x[v]-x[i])

			{

				ok = false;

				break;

			}

		}

		if (ok == false)

		{

			cout << "No" << '\n';

			break;

		}

 

	}

 

	if (ok == true)

	{

		cout << "Yes" << '\n';

	}

	}

	return 0;

}

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

#define MAXN 101000

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

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

	{

		int x, y, z;

		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;

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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <queue>

#include <vector>

using namespace std;

struct node{

    int u,w;

};

node p;

vector<node>q[100005];

int visit[100005];

int dis[100005],flag;

void query(int x){

    if(flag==1) return ;

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

        node y=q[x][i];

        if(visit[y.u]==1){

            if(dis[y.u]!=dis[x]+y.w) {

                flag=1;

                return ;

            }

            else continue;

        }

        visit[y.u]=1;

        dis[y.u]=dis[x]+y.w;

        query(y.u);

    }



}

int main(){

    int n,m,k,u,w;

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

        memset(visit,0,sizeof(visit));

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

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

            p.u=u,p.w=w;

            q[k].push_back(p);

            p.u=k,p.w=-w;

            q[u].push_back(p);

        }

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

            if(visit[i]==0){

                flag=0;

                query(i);

            }

            if(flag==1){

                printf("No\n");

                break;

            }



        }

        if(flag==0) printf("Yes\n");

    }

    return 0;

}




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

#include <string>  

#include <cstdio>  

#include <cmath>  

#include<cstring>  

#define max(x,y) x>y?x:y  

using namespace std;  

const int maxn=1011111;  

int f[maxn];  

int d[maxn];  

int find(int x)  

{  

    if(f[x]==x)  

        return x;  

  

    int r=find(f[x]);  

    d[x]=d[f[x]]+d[x];//当前x到她老祖宗的距离等于他现在的距离加上她爸爸到老祖宗的距离  

  

    return f[x]=r; //把每个子节点合并到他的祖先  

}  

  

  

int fix(int x,int y,int z)  

{  

    int fx=find(x);  

    int fy=find(y);  

    if(fx==fy)  

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

    f[fy]=fx;//把x当作y的爸爸；  

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

    return 1;  

}  

  

  

  

  

  

int main()  

{  

    int n,m;  

    int lose=1;  

    cin>>n>>m;  

    for(int i=1;i<=n;i++)//初始化：先把所有点的爸爸都设为自己；  

        f[i]=i;  

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

    {  

        int x,y,z;  

        cin>>x>>y>>z;  

  

        if(lose)  

            lose=fix(x,y,z);  

  

    }  

  

    if(lose)  

        cout<<"Yes"<<endl;  

    else  

        cout<<"No"<<endl;  

    return 0;  

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn=1e5+5; 

vector <pair<int, int> > adj[maxn];

bool vis[maxn];

int x[maxn];



void dfs(int now,int dist)

{

	vis[now] = true;

	x[now] = dist;

 

	for (int i = 0; i < adj[now].size(); i++)

	{

		int next = adj[now][i].first;

		int d = adj[now][i].second;

		if (vis[next] == false)

		{

			dfs(next, dist + d);

		}

	}

}

 

int main()

{

	int n, m;

	int l, r, d;

	bool ok = true;

 

	cin.tie(0);

 

	while(cin >> n >> m)

	{

		ok=true;

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

	{

		cin >> l >> r >> d;

		adj[l].push_back(make_pair(r, d)); //关联点 

		adj[r].push_back(make_pair(l, -d));

	}

 

	memset(x, 0, sizeof(x));

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

 

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

	{

		if (vis[i]==false)

		{

			dfs(i, 0);

		}

	}



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

	{

		for (int j = 0; j < adj[i].size(); j++)

		{

			int v = adj[i][j].first;

			if (adj[i][j].second != x[v]-x[i])

			{

				ok = false;

				break;

			}

		}

		if (ok == false)

		{

			cout << "No" << '\n';

			break;

		}

 

	}

 

	if (ok == true)

	{

		cout << "Yes" << '\n';

	}

	}

	return 0;

}

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

#include <string>  

#include <cstdio>  

#include <cmath>  

#include<cstring>  

#define max(x,y) x>y?x:y  

using namespace std;  

const int maxn=1011111;  

int f[maxn];  

int d[maxn];  

int find(int x)  

{  

    if(f[x]==x)  

        return x;  

  

    int r=find(f[x]);  

    d[x]=d[f[x]]+d[x];//当前x到她老祖宗的距离等于他现在的距离加上她爸爸到老祖宗的距离  

  

    return f[x]=r; //把每个子节点合并到他的祖先  

}  

  

  

int fix(int x,int y,int z)  

{  

    int fx=find(x);  

    int fy=find(y);  

    if(fx==fy)  

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

    f[fy]=fx;//把x当作y的爸爸；  

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

    return 1;  

}  

  

  

  

  

  

int main()  

{  

    int n,m;  

    int lose=1;  

    cin>>n>>m;  

    for(int i=1;i<=n;i++)//初始化：先把所有点的爸爸都设为自己；  

        f[i]=i;  

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

    {  

        int x,y,z;  

        cin>>x>>y>>z;  

  

        if(lose)  

            lose=fix(x,y,z);  

  

    }  

  

    if(lose)  

        cout<<"Yes"<<endl;  

    else  

        cout<<"No"<<endl;  

    return 0;  

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

using namespace std;

#define fi first

#define se second

typedef vector<int> VI;

typedef pair<int,int> PII;

typedef long long ll;

#define mp make_pair

#define pb push_back

void solve();

void read_input(){



}

void solve(){



}

vector<PII> edges[100005];



vector<int> order;

bool visited[100005];

ll pos[100005];

void dfs(int i){

	if (visited[i])

		return ;

	visited[i] = true;

	for(PII p:edges[i]){

		if (!visited[p.fi])

			dfs(p.fi);

	}

	order.pb(i);

}

int main(){

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

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

		edges[i] = vector<PII>();

	}	

	int N,M;

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

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

		int a,b,c;

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

		edges[a].pb({b,c});

	}

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

		if (!visited[i]){

			dfs(i);

		}

	} 

	reverse(order.begin(),order.end());

	for(int i:order){

		if (pos[i] == -1){

			pos[i] = 0;

		}

		for(PII p:edges[i]){

			if (pos[p.fi] == -1){

				pos[p.fi] = pos[i]+p.se;

			}else{

				if (pos[p.fi] != pos[i]+p.se){

					cout<<"No"<<endl;

					return 0;

				}

			}

		}

	}

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

		if (pos[i]>1000000000){

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

}

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

using namespace std;



typedef pair<int, int> pi;



int n, m, l, r, d, dist[100001], vis[100001];

vector<pi> adj[100001];



void dfs(int node){

	vis[node] = 1;

	for(auto it:adj[node]){

		if(vis[it.first] != -1){

			if(dist[it.first] != dist[node]+it.second){ cout << "No"; exit(0); }

			continue;

		}

		vis[it.first] = 1;

		dist[it.first] = dist[node]+it.second;

		dfs(it.first);

	}

}



int main(){

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

	cin >> n >> m;

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

		cin >> l >> r >> d;

		adj[l].push_back(pi(r, d));

		adj[r].push_back(pi(l, -d));

	}

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

		if(vis[i] == -1) dfs(i);

	}

	cout << "Yes";

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

#include <iostream>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <queue>

#include <vector>

using namespace std;

struct node{

    int u,w;

};

node p;

vector<node>q[100005];

int visit[100005];

int dis[100005],flag;

void query(int x){

    if(flag==1) return ;

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

        node y=q[x][i];

        if(visit[y.u]==1){

            if(dis[y.u]!=dis[x]+y.w) {

                flag=1;

                return ;

            }

            else continue;

        }

        visit[y.u]=1;

        dis[y.u]=dis[x]+y.w;

        query(y.u);

    }



}

int main(){

    int n,m,k,u,w;

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

        memset(visit,0,sizeof(visit));

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

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

            p.u=u,p.w=w;

            q[k].push_back(p);

            p.u=k,p.w=-w;

            q[u].push_back(p);

        }

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

            if(visit[i]==0){

                flag=0;

                query(i);

            }

            if(flag==1){

                printf("No\n");

                break;

            }



        }

        if(flag==0) printf("Yes\n");

    }

    return 0;

}




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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,l,r,d, dis[N], vis[N], v=1;

vector <pair<int, int>> g[N];

stack <int> st;

bool f;



void dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

        if(vis[pa.f] != v)

        {

            dis[pa.f] = dis[from] + pa.s;

            dfs(pa.f);

        }else if(dis[pa.f] != dis[from] + pa.s)

        {

            f =1;

        }

    }

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

        if(vis[pa.f] != v)

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    #   ifdef LOCAL_PROJECT

    freopen("in.txt", "r", stdin);

    #   endif

    cin >> n >> m;

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

    {

        cin >> l >> r >> d;

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

    }

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

    {

        if(vis[i] != v) 

        {

            st.push(i);

            dfs1(i);

        }

    }

    v++;

    while(!st.empty())

    {

        int i = st.top();

        if(vis[i] != v)

        {

            for(auto pa : g[i])

            {

                if(vis[pa.f] == v)

                {

                    dis[i] = dis[pa.f] - pa.s;

                    break;

                }

            }

            dfs(i);

        }

        st.pop();

    }

    if(f) cout << "No";

    else cout << "Yes";

    return 0;

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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn=1e5+5; 

vector <pair<int, int> > adj[maxn];

bool vis[maxn];

int x[maxn];



void dfs(int now,int dist)

{

	vis[now] = true;

	x[now] = dist;

 

	for (int i = 0; i < adj[now].size(); i++)

	{

		int next = adj[now][i].first;

		int d = adj[now][i].second;

		if (vis[next] == false)

		{

			dfs(next, dist + d);

		}

	}

}

 

int main()

{

	int n, m;

	int l, r, d;

	bool ok = true;

 

	cin.tie(0);

 

	while(cin >> n >> m)

	{

		ok=true;

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

	{

		cin >> l >> r >> d;

		adj[l].push_back(make_pair(r, d)); //关联点 

		adj[r].push_back(make_pair(l, -d));

	}

 

	memset(x, 0, sizeof(x));

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

 

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

	{

		if (vis[i]==false)

		{

			dfs(i, 0);

		}

	}



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

	{

		for (int j = 0; j < adj[i].size(); j++)

		{

			int v = adj[i][j].first;

			if (adj[i][j].second != x[v]-x[i])

			{

				ok = false;

				break;

			}

		}

		if (ok == false)

		{

			cout << "No" << '\n';

			break;

		}

 

	}

 

	if (ok == true)

	{

		cout << "Yes" << '\n';

	}

	}

	return 0;

}

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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,l,r,d, dis[N], vis[N], v=1;

vector <pair<int, int>> g[N];

stack <int> st;

bool f;



void dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

        if(vis[pa.f] != v)

        {

            dis[pa.f] = dis[from] + pa.s;

            dfs(pa.f);

        }else if(dis[pa.f] != dis[from] + pa.s)

        {

            f =1;

        }

    }

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

        if(vis[pa.f] != v)

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    #   ifdef LOCAL_PROJECT

    freopen("in.txt", "r", stdin);

    #   endif

    cin >> n >> m;

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

    {

        cin >> l >> r >> d;

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

    }

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

    {

        if(vis[i] != v) 

        {

            st.push(i);

            dfs1(i);

        }

    }

    v++;

    while(!st.empty())

    {

        int i = st.top();

        if(vis[i] != v)

        {

            for(auto pa : g[i])

            {

                if(vis[pa.f] == v)

                {

                    dis[i] = dis[pa.f] - pa.s;

                    break;

                }

            }

            dfs(i);

        }

        st.pop();

    }

    if(f) cout << "No";

    else cout << "Yes";

    return 0;

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

using namespace std;

#define fi first

#define se second

typedef vector<int> VI;

typedef pair<int,int> PII;

typedef long long ll;

#define mp make_pair

#define pb push_back

void solve();

void read_input(){



}

void solve(){



}

vector<PII> edges[100005];



vector<int> order;

bool visited[100005];

ll pos[100005];

void dfs(int i){

	if (visited[i])

		return ;

	visited[i] = true;

	for(PII p:edges[i]){

		if (!visited[p.fi])

			dfs(p.fi);

	}

	order.pb(i);

}

int main(){

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

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

		edges[i] = vector<PII>();

	}	

	int N,M;

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

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

		int a,b,c;

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

		edges[a].pb({b,c});

	}

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

		if (!visited[i]){

			dfs(i);

		}

	} 

	reverse(order.begin(),order.end());

	for(int i:order){

		if (pos[i] == -1){

			pos[i] = 0;

		}

		for(PII p:edges[i]){

			if (pos[p.fi] == -1){

				pos[p.fi] = pos[i]+p.se;

			}else{

				if (pos[p.fi] != pos[i]+p.se){

					cout<<"No"<<endl;

					return 0;

				}

			}

		}

	}

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

		if (pos[i]>1000000000){

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

}

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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,l,r,d, dis[N], vis[N], v=1;

vector <pair<int, int>> g[N];

stack <int> st;

bool f;



void dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

        if(vis[pa.f] != v)

        {

            dis[pa.f] = dis[from] + pa.s;

            dfs(pa.f);

        }else if(dis[pa.f] != dis[from] + pa.s)

        {

            f =1;

        }

    }

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

        if(vis[pa.f] != v)

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    #   ifdef LOCAL_PROJECT

    freopen("in.txt", "r", stdin);

    #   endif

    cin >> n >> m;

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

    {

        cin >> l >> r >> d;

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

    }

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

    {

        if(vis[i] != v) 

        {

            st.push(i);

            dfs1(i);

        }

    }

    v++;

    while(!st.empty())

    {

        int i = st.top();

        if(vis[i] != v)

        {

            for(auto pa : g[i])

            {

                if(vis[pa.f] == v)

                {

                    dis[i] = dis[pa.f] - pa.s;

                    break;

                }

            }

            dfs(i);

        }

        st.pop();

    }

    if(f) cout << "No";

    else cout << "Yes";

    return 0;

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

using namespace std;



typedef pair<int, int> pi;



int n, m, l, r, d, dist[100001], vis[100001];

vector<pi> adj[100001];



void dfs(int node){

	vis[node] = 1;

	for(auto it:adj[node]){

		if(vis[it.first] != -1){

			if(dist[it.first] != dist[node]+it.second){ cout << "No"; exit(0); }

			continue;

		}

		vis[it.first] = 1;

		dist[it.first] = dist[node]+it.second;

		dfs(it.first);

	}

}



int main(){

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

	cin >> n >> m;

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

		cin >> l >> r >> d;

		adj[l].push_back(pi(r, d));

		adj[r].push_back(pi(l, -d));

	}

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

		if(vis[i] == -1) dfs(i);

	}

	cout << "Yes";

}

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

using namespace std;

const int N = 1e5 + 25, M = 1e6 + 25, oo = 1e9+7, mxmsk = 1 << 16;

const long long OO = 1e18+25;

    

#define f first

#define s second

    

int n,m,l,r,d, dis[N], vis[N], v=1;

vector <pair<int, int>> g[N];

stack <int> st;

bool f;



void dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

        if(vis[pa.f] != v)

        {

            dis[pa.f] = dis[from] + pa.s;

            dfs(pa.f);

        }else if(dis[pa.f] != dis[from] + pa.s)

        {

            f =1;

        }

    }

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

        if(vis[pa.f] != v)

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    #   ifdef LOCAL_PROJECT

    freopen("in.txt", "r", stdin);

    #   endif

    cin >> n >> m;

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

    {

        cin >> l >> r >> d;

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

    }

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

    {

        if(vis[i] != v) 

        {

            st.push(i);

            dfs1(i);

        }

    }

    v++;

    while(!st.empty())

    {

        int i = st.top();

        if(vis[i] != v)

        {

            for(auto pa : g[i])

            {

                if(vis[pa.f] == v)

                {

                    dis[i] = dis[pa.f] - pa.s;

                    break;

                }

            }

            dfs(i);

        }

        st.pop();

    }

    if(f) cout << "No";

    else cout << "Yes";

    return 0;

}