stablegradients/pie-gem5-pairs huggingface dataset:
// 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.369551218559337x):
#include <iostream>

#include <vector>

#include <algorithm>

#include <map>

#include <set>

#include <unordered_map>

#include <unordered_set>

#include <tuple>

#include <iomanip>

//#include <cmath>

#include <queue>

#include <deque>

#include <stack>

//#pragma GCC optimize("Ofast")

//#pragma GCC target("avx,avx2")

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

typedef long double ld;

typedef long long ll;

typedef double db;

#define endl '\n'

#define pb push_back

#define pii pair<ll, ll>

#define ff first

#define ss second

#define sz(x) (ll)(x).size()

#define optimize1() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#pragma warning(disable:4996)



using namespace std;



const ll MX = 2e5 + 10, INF = 1e9 + 1;

ll n, m, t, d[MX], l[MX], r[MX], a[MX];

bool used[MX];

vector<vector<int>> g;



bool dfs(int v) {

	used[v] = true;

	bool f = true;

	for (auto c : g[v]) {

		int tmp = a[v], pos = l[c] + r[c] - v;

		if (l[c] == v)

			tmp += d[c];

		else

			tmp -= d[c];

		if (used[pos])

			f = f && (a[pos] == tmp);

		else {

			a[pos] = tmp;

			f = f && dfs(pos);

		}

	}

	return f;

}



signed main() {

	optimize1();

	cin >> n >> m;

	g.resize(n + 1);

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

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

		g[l[i]].pb(i);

		g[r[i]].pb(i);

	}

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

		if (used[i])

			continue;

		a[i] = 0;

		if (!dfs(i)) {

			cout << "No" << endl;

			return 0;

		}

	}

	cout << "Yes" << endl;

	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: 1.2240744305069744x):
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<vector>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<queue>

#include<ctime>

#include<cassert>

#include<complex>

#include<string>

#include<cstring>

#include<chrono>

#include<random>

#include<bitset>



#define fi first

#define se second

#define mp make_pair

#define eb emplace_back

#define all(v) v.begin(), v.end()

#define sz(v) (int) v.size()



using namespace std;



mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());



typedef long long lox;

typedef long double lod;

typedef unsigned long long pox;

typedef pair<int, int> pii;



const int MAX_N = 1e5 + 5;



int n, m;

vector<pii> g[MAX_N];

int x[MAX_N], deg_in[MAX_N];

bool used[MAX_N];



void dfs(int v) {

	used[v] = true;

	for (auto &e : g[v]) {

		int u = e.fi, w = e.se;

		if (!used[u]) {

			x[u] = x[v] + w;

			dfs(u);

		}

	}

}



signed main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cin >> n >> m;

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

		int l, r, d;

		cin >> l >> r >> d;

		g[l].eb(r, d);

		deg_in[r]++;

	}

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

		if (!deg_in[i]) {

			dfs(i);

		}

	}

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

		for (auto &e : g[v]) {

			int u = e.fi, w = e.se;

			if (x[u] != x[v] + w) {

				cout << "No\n";

				return 0;

			}

		}

	}

	cout << "Yes\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: 1.2340402240629706x):
#include<bits/stdc++.h>

using namespace std;

#define mp make_pair

#define pb push_back

#define fi first

#define se second

#define ll long long

typedef pair<int,int> par;

typedef vector<int> vct;

typedef vector<par> vp;

const ll mn=-1e18,mx=1e18;

const int mod=1e9+7;

int color[100007],n,m,f=1,dis[100007];

vp HMS[100007];

void dfs(int u)

{

	for(int i=0;i<HMS[u].size();i++)

	{

		int v=HMS[u][i].fi;

		int w=HMS[u][i].se;

		if(dis[v]==mod)

		{

			dis[v]=dis[u]+w;

			dfs(v);

		}

		else if(dis[v]!=dis[u]+w)

		{

			f=0;

		}

	}

}

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		int w;

		cin>>u>>v>>w;

		HMS[u].pb(mp(v,w));

		HMS[v].pb(mp(u,-w));

	}

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

	{

		dis[i]=mod;

	}

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

	{

		if(dis[i]==mod)

		{

			dis[i]=0;

			dfs(i);

		}

	}

	if(f) cout<<"Yes";

	else cout<<"No";

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

using namespace std;

typedef long long ll;

typedef long double ld;

#define X first

#define Y second

#define pb push_back

#define sz(a) (int)a.size()



void solve();



signed main() {

#ifdef ONPC

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

#endif // ONPC

    //freopen("time.in", "r", stdin);

    //freopen("time.out", "w", stdout);

    ios_base::sync_with_stdio(0), cin.tie(0);

    int t = 1;

    //cin >> t;

    while (t--) solve();

    return 0;

}



const int N = 1e5 + 5;

int n, m, kek, dist[N];

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

bool used[N];

vector<int> ord;



void dfs(int v) {

    used[v] = 1;

    for (auto d : g[v]) {

        if (!used[d.X]) {

            dfs(d.X);

        }

    }

    ord.pb(v);

}



void dfs2(int v) {

    used[v] = 1;

    for (auto d : gr[v]) {

        if (!used[d.X]) {

            dfs2(d.X);

        }

    }

    ++kek;

}



void solve() {

    cin >> n >> m;

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

        int l, r, d;

        cin >> l >> r >> d;

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

        gr[r].pb({l, d});

    }

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

        if (!used[i]) {

            dfs(i);

        }

    }

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

    memset(used, 0, sizeof used);

    for (auto d : ord) {

        if (!used[d]) {

            kek = 0;

            dfs2(d);

            if (kek > 1) {

                cout << "No\n";

                return;

            }

        }

    }

    for (auto d : ord) {

        if (d == ord[0]) continue;

        dist[d] = -1;

        for (auto t : gr[d]) {

            int nw = dist[t.X] + t.Y;

            if (dist[d] != -1 && nw != dist[d]) {

                cout << "No\n";

                return;

            }

            dist[d] = dist[t.X] + t.Y;

        }

    }

    cout << "Yes\n";

}


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

using namespace std;

#define mp make_pair

#define pb push_back

#define fi first

#define se second

#define ll long long

typedef pair<int,int> par;

typedef vector<int> vct;

typedef vector<par> vp;

const ll mn=-1e18,mx=1e18;

const int mod=1e9+7;

int color[100007],n,m,f=1,dis[100007];

vp HMS[100007];

void dfs(int u)

{

	for(int i=0;i<HMS[u].size();i++)

	{

		int v=HMS[u][i].fi;

		int w=HMS[u][i].se;

		if(dis[v]==mod)

		{

			dis[v]=dis[u]+w;

			dfs(v);

		}

		else if(dis[v]!=dis[u]+w)

		{

			f=0;

		}

	}

}

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		int w;

		cin>>u>>v>>w;

		HMS[u].pb(mp(v,w));

		HMS[v].pb(mp(u,-w));

	}

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

	{

		dis[i]=mod;

	}

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

	{

		if(dis[i]==mod)

		{

			dis[i]=0;

			dfs(i);

		}

	}

	if(f) cout<<"Yes";

	else cout<<"No";

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

#include<stdio.h>

#include<vector>

#include<cmath>

#include<queue>

#include<string.h>

#include<map>

#include<set>

#include<algorithm>

#define ll long long

#define pi pair < ll,ll >

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

#define mid (low+high)/2

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

#define N 300004

#define INF 1e9+7



using namespace std;



ll n,m;

vector < pi > info[N];

ll a,b,c;

ll dist[N];

bool vis[N];

bool can = true;



void dfs(ll cur,ll d)

{

    if(vis[cur])

    {

        if(dist[cur] != d)

            can = false;



        return;

    }



    vis[cur] = true;

    dist[cur] = d;



    rep(i,0,info[cur].size())

    {

        dfs(info[cur][i].first,d+info[cur][i].second);

    }



    return;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);



    cin >> n >> m;



    rep(i,0,m)

    {

        cin >> a >> b >> c;



        info[a].push_back(mp(b,c));

        info[b].push_back(mp(a,-c));

    }



    rep(i,1,n+1)

    {

        if(!vis[i])

        {

            dfs(i,0);

        }

    }



    if(can)

        cout << "Yes\n";

    else

        cout << "No\n";



    return 0;

}




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

using namespace std;

#define mp make_pair

#define pb push_back

#define fi first

#define se second

#define ll long long

typedef pair<int,int> par;

typedef vector<int> vct;

typedef vector<par> vp;

const ll mn=-1e18,mx=1e18;

const int mod=1e9+7;

int color[100007],n,m,f=1,dis[100007];

vp HMS[100007];

void dfs(int u)

{

	for(int i=0;i<HMS[u].size();i++)

	{

		int v=HMS[u][i].fi;

		int w=HMS[u][i].se;

		if(dis[v]==mod)

		{

			dis[v]=dis[u]+w;

			dfs(v);

		}

		else if(dis[v]!=dis[u]+w)

		{

			f=0;

		}

	}

}

int main()

{

	cin>>n>>m;

	while(m--)

	{

		int u,v;

		int w;

		cin>>u>>v>>w;

		HMS[u].pb(mp(v,w));

		HMS[v].pb(mp(u,-w));

	}

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

	{

		dis[i]=mod;

	}

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

	{

		if(dis[i]==mod)

		{

			dis[i]=0;

			dfs(i);

		}

	}

	if(f) cout<<"Yes";

	else cout<<"No";

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



#define F first 

#define S second



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;



const int N = 100010, M = 4 * N, INF = 0x3f3f3f3f;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int dist[N], q[N];



bool bfs(int u)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = u;

	dist[u] = 0;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			if (dist[j] == INF)

			{

				q[ ++ tt] = j;

				dist[j] = dist[t] + w[i];

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	return true;

}



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



int main()

{

	memset(h, -1, sizeof h);

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

	while (m -- )

	{

		int a, b, c;

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

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

	}

	

	memset(dist, 0x3f, sizeof dist);

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

		if (dist[i] == INF && !bfs(i))

		{

			puts("No");

			return 0;

		}

	puts("Yes");

    return 0;

}

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



#define F first 

#define S second



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;



const int N = 100010, M = 4 * N, INF = 0x3f3f3f3f;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int dist[N], q[N];

bool st[N];



bool bfs(int u)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = u;

	st[u] = true;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		// st[t] = true;

		

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			if (!st[j])

			{

				q[ ++ tt] = j;

				st[j] = true;

				dist[j] = dist[t] + w[i];

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	return true;

}



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



int main()

{

	memset(h, -1, sizeof h);

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

	while (m -- )

	{

		int a, b, c;

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

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

	}

	

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

		if (!st[i] && !bfs(i))

		{

			puts("No");

			return 0;

		}

	puts("Yes");

    return 0;

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



#define F first 

#define S second



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;



const int N = 100010, M = 4 * N, INF = 0x3f3f3f3f;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int dist[N], q[N];



bool bfs(int u)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = u;

	dist[u] = 0;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			if (dist[j] == INF)

			{

				q[ ++ tt] = j;

				dist[j] = dist[t] + w[i];

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	return true;

}



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



int main()

{

	memset(h, -1, sizeof h);

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

	while (m -- )

	{

		int a, b, c;

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

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

	}

	

	memset(dist, 0x3f, sizeof dist);

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

		if (dist[i] == INF && !bfs(i))

		{

			puts("No");

			return 0;

		}

	puts("Yes");

    return 0;

}

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



#define F first 

#define S second

#define PB push_back



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;

typedef priority_queue<int> HEAP;

typedef priority_queue<int, vector<int>, greater<int> > RHEAP;



const int N = 100010, M = 400010;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int q[M];

int dist[N];

bool st[N];



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



bool bfs(int u)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = u;

	dist[u] = 0;

	st[u] = true;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			if (!st[j])

			{

				q[ ++ tt] = j;

				st[j] = true;

				dist[j] = dist[t] + w[i];

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	return true;

}



int main()

{

	memset(h, -1, sizeof h);

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

	

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

	{

		int a, b, c;

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

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

	}

	

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

		if (!st[i] && !bfs(i))

		{

			puts("No");

			return 0;

		}

	puts("Yes");

    return 0;

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



#define F first

#define S second

#define PB push_back



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;

typedef priority_queue<int> HEAP;

typedef priority_queue<int, vector<int>, greater<int> > RHEAP;



const int N = 100010, M = 4 * N, INF = 0x3f3f3f3f;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int q[N], dist[N];



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



bool bfs(int st)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = st;

	dist[st] = 0;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			

			if (dist[j] == INF)

			{

				dist[j] = dist[t] + w[i];

				q[ ++ tt] = j;

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	

	return true;

}



int main()

{

	memset(h, -1, sizeof h);

	memset(dist, 0x3f, sizeof dist);

	

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

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

	{

		int a, b, c;

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

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

	}

	

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

		if (dist[i] == INF && !bfs(i))

		{

			puts("No");

			return 0;

		}

	

	puts("Yes");

	return 0;

}

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



#define F first 

#define S second

#define PB push_back



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;

typedef priority_queue<int> HEAP;

typedef priority_queue<int, vector<int>, greater<int> > RHEAP;



const int N = 100010, M = 400010;



int n, m;

int h[N], e[M], w[M], ne[M], idx;

int q[M];

int dist[N];

bool st[N];



void add(int a, int b, int c)

{

	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;

}



bool bfs(int u)

{

	int hh = 0, tt = -1;

	q[ ++ tt] = u;

	dist[u] = 0;

	st[u] = true;

	

	while (hh <= tt)

	{

		int t = q[hh ++ ];

		

		for (int i = h[t]; ~i; i = ne[i])

		{

			int j = e[i];

			if (!st[j])

			{

				q[ ++ tt] = j;

				st[j] = true;

				dist[j] = dist[t] + w[i];

			}

			else if (dist[j] != dist[t] + w[i]) return false;

		}

	}

	return true;

}



int main()

{

	memset(h, -1, sizeof h);

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

	

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

	{

		int a, b, c;

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

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

	}

	

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

		if (!st[i] && !bfs(i))

		{

			puts("No");

			return 0;

		}

	puts("Yes");

    return 0;

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



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

using namespace std;



typedef long long ll;



const int INF = 1 << 30;

const ll LLINF = 1LL << 62;



int mod = 1000000007;



struct edge{

    int to;

    ll cost;

};



vector<edge> nodes[100000];

ll zahyou[100000];



void init(){

    rep(i, 100000) zahyou[i] = -mod;

}



bool dfs(int p, int s){

    bool ret = true;

    rep(i, nodes[s].size()){

        int to = nodes[s][i].to;

        ll cost = nodes[s][i].cost;

        if(zahyou[to] == -mod){

            zahyou[to] = zahyou[s] + cost;

            ret &= dfs(s, to);

        }

        else{

            if(zahyou[to] != zahyou[s] + cost) ret = false;

        }

    }

    return ret;

}



int main(void){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    init();

    int N, M; cin >> N >> M;

    rep(i, M){

        int l, r; ll c;

        cin >> l >> r >> c; l--; r--;

        edge e; e.cost = c; e.to = r;

        nodes[l].push_back(e);

        e.cost *= -1; e.to = l;

        nodes[r].push_back(e);

    }

    bool ok = true;

    rep(i, N){

        if(zahyou[i] == -mod){

            zahyou[i] = 0;

            ok &= dfs(-1, i);

        }

    }

    if(ok) cout << "Yes" << endl;

    else cout << "No" << endl;

    return 0;

}

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



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

using namespace std;



typedef long long ll;



const int INF = 1 << 30;

const ll LLINF = 1LL << 62;



int mod = 1000000007;



struct edge{

    int to;

    ll cost;

};



vector<edge> nodes[100000];

int visited[100000] = {};

ll zahyou[100000];



void init(){

    rep(i, 100000) zahyou[i] = -1;

}



bool dfs(int p, int s){

    bool ret = true;

    visited[s] = true;

    rep(i, nodes[s].size()){

        int to = nodes[s][i].to;

        ll cost = nodes[s][i].cost;

        if(to == p) continue;

        if(visited[to]){

            if(zahyou[to] != zahyou[s] + cost) ret = false;

        }

        else{

            zahyou[to] = zahyou[s] + cost;

            ret &= dfs(s, to);

        }

    }

    return ret;

}



int main(void){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int N, M; cin >> N >> M;

    rep(i, M){

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

        l--; r--;

        ll d; cin >> d;

        edge e;

        e.to = r; e.cost = d;

        nodes[l].push_back(e);

        e.to = l; e.cost = -d;

        nodes[r].push_back(e);

    }



    bool res = true;

    rep(i, N){

        if(!visited[i]){

            zahyou[i] = 0;

            res &= dfs(-1, i);

        }

    }

    if(res) cout << "Yes" << endl;

    else cout << "No" << endl;

    return 0;

}