stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

#define X first

#define Y second

#define pb push_back

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

#define int long long



using namespace std;



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



vector <vector <int>> v;



int a[100010];

int used[100010];



void dfs(int vv) {

    used[vv] = 1;

    for (auto u : g[vv]) {

        if (used[u.X] == 1)

            continue;

        a[u.X] = a[vv] + u.Y;

        dfs(u.X);

    }

}



signed main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int n, m;

    cin >> n >> m;

    int l, r, d;

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

        cin >> l >> r >> d;

        l--;

        r--;

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

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

        v.pb({l, r, d});

    }

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

        if (used[i] == 0) {

            dfs(i);

        }

    }

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

        if (a[v[i][1]] - a[v[i][0]] != v[i][2]) {

            cout << "No" << endl;

            exit(0);

        }

    }

    cout << "Yes";

    return 0;

}


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

#include <cstdio>

#include <algorithm>

#define N 400005

#define inf 0x7fffffff

using namespace std;

 

struct E {int next, to, dis;} e[N];

int n, m, num, flag, cnt;

int h[N], a[N], pos[N], obj[N];

bool vis[N];

 

int read()

{

    int x = 0; char c = getchar();

    while(c < '0' || c > '9') c = getchar();

    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}

    return x;

}

 

void add(int u, int v, int w)

{

    e[++num].next = h[u];

    e[num].to = v;

    e[num].dis = w;

    h[u] = num;

}

 

void dfs(int x)

{

    if(flag) return;

    vis[x] = 1;

    for(int i = h[x]; i != 0; i = e[i].next)

    {

        int t = pos[x] + e[i].dis;

        if(pos[e[i].to] == inf) pos[e[i].to] = t, dfs(e[i].to);

        else if(t != pos[e[i].to]) {flag = 1; return;}

    }

}

 

int main()

{

    cin >> n >> m;

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

    {

        int a = read(), b = read(), c = read();

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

        pos[a] = pos[b] = inf;

        obj[++cnt] = a, obj[++cnt] = b;

    }

    sort(obj + 1, obj + 1 + cnt);

    cnt = unique(obj + 1, obj + 1 + cnt) - obj - 1;

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

        if(!vis[obj[i]])

        {

            flag = 0;

            pos[obj[i]] = 0;

            dfs(obj[i]);

            if(flag) {cout << "No"; return 0;}

        }

    cout << "Yes";

    return 0;

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

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

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

bool dfs(int u){

    vis[u] = 1;

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

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

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

            if(!dfs(v))

                return false;

        }

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

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

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

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

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

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


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

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}
// Slower C++ solution:
/*

Problem:People on a Line

Algorithm:"linear" DFS

Status:

*/



#include <iostream>

#include <cstdio>

#include <cmath>

#include <algorithm>

#include <string>

#include <vector>

#include <map>

#include <queue>

#include <deque>

#include <set>

#include <sstream>

#include <utility>

#include <list>

#include <ctime>

#include <memory.h>

#include <stack>

#include <stdio.h>

#include <string.h>



using namespace std;



const int INF=1e9+7;



vector<pair<int,int> > nei[100005];

int pos[100005];

int n,m;

bool used[100005];

bool exist[100005];



void dfs(int x,int cur){

	pos[x]=cur;

	used[x]=true;

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

		if(used[nei[x][i].first]){

			if(pos[nei[x][i].first]-cur!=nei[x][i].second){

				cout<<"No"<<endl;

				exit(0);

			}

		}

		else{

			dfs(nei[x][i].first,cur+nei[x][i].second);

		}

	}

}



int main()

{

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

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

		int l,r,d;

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

		l--;r--;

		exist[l]=true;exist[r]=true;

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

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

	}

	

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

		if(!exist[i])continue;

		if(!used[i]){

			dfs(i,0);

		}

	}

	cout<<"Yes"<<endl;

	

	return 0;

}


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

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

}
// Slower C++ solution:
#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 ( int (i) = a; (i) < (b) ; (i) ++ )

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

#define repD(i, a, b) for ( int (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 print cout << endl

#define tri pair<pair<int, int>, int>

using namespace std;



int n,m;

int fa[100005], cnt[100005];

void find(int x){

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

    find(fa[x]);

    cnt[x] += cnt[fa[x]]; fa[x] = fa[fa[x]];

}



int main(){

    while ( cin >> n >> m ) {

        bool has = false;

        MSET(fa); MSET(cnt);

        for ( int i = 0 ; i <= n ; ++ i ) fa[i] = i;

        for ( int i = 0, x, y, z ; i < m ; ++ i ) {

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

            find(x); find(y);

            if ( fa[x] == fa[y] ) {

                if ( cnt[y] != cnt[x] + z ) has = true;

            }

            else {

                cnt[fa[y]] = cnt[x] + z - cnt[y];

                fa[fa[y]] = fa[x];

            }

        }

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

    }

}


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

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

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

#define X first

#define Y second

#define pb push_back

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

#define int long long



using namespace std;



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



vector <vector <int>> v;



int a[100010];

int used[100010];



void dfs(int vv) {

    used[vv] = 1;

    for (auto u : g[vv]) {

        if (used[u.X] == 1)

            continue;

        a[u.X] = a[vv] + u.Y;

        dfs(u.X);

    }

}



signed main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int n, m;

    cin >> n >> m;

    int l, r, d;

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

        cin >> l >> r >> d;

        l--;

        r--;

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

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

        v.pb({l, r, d});

    }

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

        if (used[i] == 0) {

            dfs(i);

        }

    }

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

        if (a[v[i][1]] - a[v[i][0]] != v[i][2]) {

            cout << "No" << endl;

            exit(0);

        }

    }

    cout << "Yes";

    return 0;

}


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

#include <iostream>

#include<cstdio>

#include <stack>

#include <vector>

using namespace std;

#define INF 0x7ffffff

typedef long long ll;

const int maxn=1e5+10;

int pre[maxn];

int dis[maxn];

int findset(int x)

{

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

    int root=findset(pre[x]);

    dis[x]=dis[pre[x]]+dis[x];

    return pre[x]=root;

}

bool unionset(int x,int y,int w)

{

    int fx=findset(x),fy=findset(y);

    if(fx==fy)

        return w==dis[y]-dis[x];

    pre[fy]=fx;

    dis[fy]=dis[x]+w-dis[y];

    return true;

}

int n,m;

int main()

{

// Input

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

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

    pre[i]=i;

   bool lose=true;

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

   {

       int x,y,w;

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

        if(lose)

       lose=unionset(x,y,w);

   }

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

   else     printf("No\n");

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

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

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

bool dfs(int u){

    vis[u] = 1;

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

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

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

            if(!dfs(v))

                return false;

        }

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

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

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

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

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

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


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

#include <unordered_map>

#include <unordered_set>

using namespace std;

#define int long long

#define eb emplace_back

#define ef emplace_front

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

#define mp make_pair

#define fi first

#define se second

#define ii pair<int, int>

#define endl '\n'

#define vi vector<int>

#define vii vector<pair<int, int>>

#define reverse(s) reverse(s.begin(), s.end())

#define ld long double

#define vs vector<string>

#define inf INT_MAX

#define vc vector<char>

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

#define vb vector<bool>

#define lb(a, x) lower_bound(a.begin(), a.end(), x)

#define ub(a, x) upper_bound(a.begin(), a.end(), x) 

#define vvi vector<vi>

#define mine(a) min_element(a.begin(),a.end())

#define maxe(a) max_element(a.begin(), a.end())

#define iota(a,i) iota(a.begin(),a.end(),i)

#define fileopen(filename) if (fopen(filename".INP", "r")) {freopen(filename".INP", "r", stdin);freopen(filename".OUT", "w", stdout);}

int n, m;

map<ii, int> a;

vi par, val;

vvi adj;

vb d;

int findset(int x) {

	int u = x;

	while (par[x] != -1) {

		x = par[x];

		par[u] = x;

	}

	return x;

}

bool sameset(int x, int y) {

	return findset(x) == findset(y);

}

void unionset(int x, int y) {

	x = findset(x);

	y = findset(y);

	if (x == y) return;

	par[x] = y;

}

void dfs(int x) {

	for (int i : adj[x]) {

		if (d[i] == 0) {

			d[i] = 1;

			val[i] = val[x] + a[mp(x, i)];

			dfs(i);

		}

		else if (val[i] != val[x] + a[mp(x, i)]) {

			cout << "No";

			exit(0);

		}

	}

}

signed main() {

	fastio;

	cin >> n >> m;

	par.resize(n + 1, -1);

	adj.resize(n + 1);

	d.resize(n + 1, 0);

	val.resize(n + 1, 0);

	while (m--) {

		int x, y, z;

		cin >> x >> y >> z;

		a[mp(x, y)] = z;

		a[mp(y, x)] = -z;

		unionset(x, y);

		adj[x].emplace_back(y);

		adj[y].emplace_back(x);

	}

	rep(i, 1, n + 1) {

		if (d[i] == 0) {

			int x = findset(i);

			d[x] = 1;

			val[x] = 0;

			dfs(x);

		}

	}

	cout << "Yes";

	return 0;

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

#include <cstdio>

#include <cstring>

#include <vector>

using namespace std;

const int N = 1e5+10;

int n,m;

int dis[N];

bool vis[N];

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

bool dfs(int u){

    vis[u] = 1;

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

    for(it = E[u].begin(); it != E[u].end(); it++){

        int v = (*it).first;

        int d = (*it).second;

        if(!vis[v]){

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

            if(!dfs(v))

                return false;

        }

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

            return false;

    }

    return true;

}

int main(){

    cin >> n >> m;

    int u,v,w;

    int i;

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

        cin >> u >> v >> w;

        E[u].push_back(make_pair(v,w));

        E[v].push_back(make_pair(u,-w));

    }

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

        if(!vis[i]){

            if(!dfs(i)){

                cout << "No" << endl;

                return 0;

            }

        }

    }

    cout << "Yes" << endl;

    return 0;

}


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

using namespace std;

int main(){

  int N, M;

  cin >> N >> M;

  vector<vector<pair<int, int>>> E(N);

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

    int L, R, D;

    cin >> L >> R >> D;

    L--;

    R--;

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

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

  }

  vector<int> d(N);

  vector<int> comp(N, -1);

  bool ok = true;

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

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

      comp[i] = i;

      d[i] = 0;

      queue<int> Q;

      Q.push(i);

      while (!Q.empty()){

        int v = Q.front();

        Q.pop();

        for (auto P : E[v]){

          int dist = P.first;

          int w = P.second;

          if (comp[w] != -1){

            if (d[w] != d[v] + dist){

              ok = false;

            }

          } else {

            comp[w] = i;

            d[w] = d[v] + dist;

            Q.push(w);

          }

        }

      }

    }

  }

  if (ok){

    cout << "Yes" << endl;

  } else {

    cout << "No" << endl;

  }

}
// Slower C++ solution:
/*

Problem:People on a Line

Algorithm:"linear" DFS

Status:

*/



#include <iostream>

#include <cstdio>

#include <cmath>

#include <algorithm>

#include <string>

#include <vector>

#include <map>

#include <queue>

#include <deque>

#include <set>

#include <sstream>

#include <utility>

#include <list>

#include <ctime>

#include <memory.h>

#include <stack>

#include <stdio.h>

#include <string.h>



using namespace std;



const int INF=1e9+7;



vector<pair<int,int> > nei[100005];

int pos[100005];

int n,m;

bool used[100005];

bool exist[100005];



void dfs(int x,int cur){

	pos[x]=cur;

	used[x]=true;

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

		if(used[nei[x][i].first]){

			if(pos[nei[x][i].first]-cur!=nei[x][i].second){

				cout<<"No"<<endl;

				exit(0);

			}

		}

		else{

			dfs(nei[x][i].first,cur+nei[x][i].second);

		}

	}

}



int main()

{

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

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

		int l,r,d;

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

		l--;r--;

		exist[l]=true;exist[r]=true;

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

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

	}

	

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

		if(!exist[i])continue;

		if(!used[i]){

			dfs(i,0);

		}

	}

	cout<<"Yes"<<endl;

	

	return 0;

}


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

using namespace std;

int main(){

  int N, M;

  cin >> N >> M;

  vector<vector<pair<int, int>>> E(N);

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

    int L, R, D;

    cin >> L >> R >> D;

    L--;

    R--;

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

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

  }

  vector<int> d(N);

  vector<int> comp(N, -1);

  bool ok = true;

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

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

      comp[i] = i;

      d[i] = 0;

      queue<int> Q;

      Q.push(i);

      while (!Q.empty()){

        int v = Q.front();

        Q.pop();

        for (auto P : E[v]){

          int dist = P.first;

          int w = P.second;

          if (comp[w] != -1){

            if (d[w] != d[v] + dist){

              ok = false;

            }

          } else {

            comp[w] = i;

            d[w] = d[v] + dist;

            Q.push(w);

          }

        }

      }

    }

  }

  if (ok){

    cout << "Yes" << endl;

  } else {

    cout << "No" << endl;

  }

}
// Slower C++ solution:
#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 ( int (i) = a; (i) < (b) ; (i) ++ )

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

#define repD(i, a, b) for ( int (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 print cout << endl

#define tri pair<pair<int, int>, int>

using namespace std;



int n,m;

int fa[100005], cnt[100005];

void find(int x){

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

    find(fa[x]);

    cnt[x] += cnt[fa[x]]; fa[x] = fa[fa[x]];

}



int main(){

    while ( cin >> n >> m ) {

        bool has = false;

        MSET(fa); MSET(cnt);

        for ( int i = 0 ; i <= n ; ++ i ) fa[i] = i;

        for ( int i = 0, x, y, z ; i < m ; ++ i ) {

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

            find(x); find(y);

            if ( fa[x] == fa[y] ) {

                if ( cnt[y] != cnt[x] + z ) has = true;

            }

            else {

                cnt[fa[y]] = cnt[x] + z - cnt[y];

                fa[fa[y]] = fa[x];

            }

        }

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

    }

}


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

using namespace std;

int main(){

  int N, M;

  cin >> N >> M;

  vector<vector<pair<int, int>>> E(N);

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

    int L, R, D;

    cin >> L >> R >> D;

    L--;

    R--;

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

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

  }

  vector<int> d(N);

  vector<int> comp(N, -1);

  bool ok = true;

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

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

      comp[i] = i;

      d[i] = 0;

      queue<int> Q;

      Q.push(i);

      while (!Q.empty()){

        int v = Q.front();

        Q.pop();

        for (auto P : E[v]){

          int dist = P.first;

          int w = P.second;

          if (comp[w] != -1){

            if (d[w] != d[v] + dist){

              ok = false;

            }

          } else {

            comp[w] = i;

            d[w] = d[v] + dist;

            Q.push(w);

          }

        }

      }

    }

  }

  if (ok){

    cout << "Yes" << endl;

  } else {

    cout << "No" << endl;

  }

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

#define X first

#define Y second

#define pb push_back

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

#define int long long



using namespace std;



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



vector <vector <int>> v;



int a[100010];

int used[100010];



void dfs(int vv) {

    used[vv] = 1;

    for (auto u : g[vv]) {

        if (used[u.X] == 1)

            continue;

        a[u.X] = a[vv] + u.Y;

        dfs(u.X);

    }

}



signed main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int n, m;

    cin >> n >> m;

    int l, r, d;

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

        cin >> l >> r >> d;

        l--;

        r--;

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

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

        v.pb({l, r, d});

    }

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

        if (used[i] == 0) {

            dfs(i);

        }

    }

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

        if (a[v[i][1]] - a[v[i][0]] != v[i][2]) {

            cout << "No" << endl;

            exit(0);

        }

    }

    cout << "Yes";

    return 0;

}


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

using namespace std;

int main(){

  int N, M;

  cin >> N >> M;

  vector<vector<pair<int, int>>> E(N);

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

    int L, R, D;

    cin >> L >> R >> D;

    L--;

    R--;

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

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

  }

  vector<int> d(N);

  vector<int> comp(N, -1);

  bool ok = true;

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

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

      comp[i] = i;

      d[i] = 0;

      queue<int> Q;

      Q.push(i);

      while (!Q.empty()){

        int v = Q.front();

        Q.pop();

        for (auto P : E[v]){

          int dist = P.first;

          int w = P.second;

          if (comp[w] != -1){

            if (d[w] != d[v] + dist){

              ok = false;

            }

          } else {

            comp[w] = i;

            d[w] = d[v] + dist;

            Q.push(w);

          }

        }

      }

    }

  }

  if (ok){

    cout << "Yes" << endl;

  } else {

    cout << "No" << endl;

  }

}