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

#include<string.h>

#include<iostream>

#include<malloc.h>

#include<math.h>

#include<algorithm>



using namespace std;



const int maxn = 1e5+100;

int n,m,tag,f[maxn],dis[maxn];



int getfa(int x)

{

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

    int fx = getfa(f[x]);

    dis[x] += dis[f[x]];

    f[x] = fx;

    return f[x];

}



int main()

{

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

    tag = 1;

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

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

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

    {

        int x,y,d;

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

        int fx = getfa(x);

        int fy = getfa(y);

        if (fx == fy)

        {

            if (dis[y] != dis[x]+d) tag = 0;

        }

        else

        {

            f[fy] = fx;

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

        }

    }

    if (tag) printf("Yes");

    else

        printf("No");

    return 0;

}


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

#include<cstdio>

#include<cmath>

#include<bits/stdc++.h>

using namespace std;

#define MAXN 400000+50

#define MAXM 400000+50

struct Edge{

    int from,to,dis,next;

}edge[MAXM];

#define inf 0x7fffffff

int head[MAXM];

int num=0;

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

{

    edge[++num].to=v,edge[num].dis=dis;

    edge[num].next=head[u];

    head[u]=num;

//    edge[++num].from=v,edge[num].to=u,edge[num].dis=dis;

}

bool vis[MAXN];

bool flag;

int pos[MAXN];

int rec[MAXN];

void dfs(int x)

{

    if(!flag) return;

    vis[x]=1;

    for(int i=head[x];i!=0;i=edge[i].next)

    {

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

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

        else if(t!=pos[edge[i].to]) {

            flag=0;return;

        }

    }

}

int main()

{

    ios::sync_with_stdio(false);

    int n,m;

    cin>>n>>m;

    int cnt=0;

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

    {

        int x,y,z;

        cin>>x>>y>>z;

        add(x,y,z);add(y,x,-z);

        pos[x]=pos[y]=inf;

        rec[++cnt]=x,rec[++cnt]=y;

    }

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

    cnt=unique(rec+1,rec+n+1)-rec-1;

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

    {

        if(!vis[rec[i]])

        {

            flag=1;

            pos[rec[i]]=0;

            dfs(rec[i]);

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

        }

    }

    puts("Yes");

}
// Slower C++ solution:
//状态:

//题号:

#include <iostream>

#include <stdio.h>

#include <fstream>

#include <algorithm>

#include <ctime>

#include <cstdlib>

#include <cstring>

#include <vector>

#include <set>

#include <string>

#include <queue>

#include <map>

#include <iomanip>

#include <cassert>

#include <complex>

#include <functional>

#include <list>

using namespace std;

typedef pair<int,int> pii;

const int INF=1e9+7;

int dp[100005];

int n,m;

vector<pii> nei[100005];

int main(){

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

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

		int a,b,c;

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

		a--;

		b--;

		nei[a].push_back(make_pair(b,c));

		nei[b].push_back(make_pair(a,-c));

	}

	fill(dp,dp+100005,INF);

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

		if(dp[i]==INF){

			//f/or(int i=0;i<n;i++){

				//c/out<<dp[i]<<" ";

			//}

			//cout<<endl;

			queue<int> q;

			q.push(i);

			dp[i]=0;

			for(;q.size();){

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

				for(int j=0;j<nei[t].size();j++){

					pii p=nei[t][j];

					int pp=p.first,c=p.second;

					if(dp[pp]==INF){

						dp[pp]=dp[t]+c;

						q.push(pp);

					}

					else if(dp[pp]!=dp[t]+c){

						puts("No");

						return 0;

					}

				}

			}

		}

	}

	puts("Yes");

	return 0;

}

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

#include<cstdio>

#include<cmath>

#include<bits/stdc++.h>

using namespace std;

#define MAXN 400000+50

#define MAXM 400000+50

struct Edge{

    int from,to,dis,next;

}edge[MAXM];

#define inf 0x7fffffff

int head[MAXM];

int num=0;

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

{

    edge[++num].to=v,edge[num].dis=dis;

    edge[num].next=head[u];

    head[u]=num;

//    edge[++num].from=v,edge[num].to=u,edge[num].dis=dis;

}

bool vis[MAXN];

bool flag;

int pos[MAXN];

int rec[MAXN];

void dfs(int x)

{

    if(!flag) return;

    vis[x]=1;

    for(int i=head[x];i!=0;i=edge[i].next)

    {

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

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

        else if(t!=pos[edge[i].to]) {

            flag=0;return;

        }

    }

}

int main()

{

    ios::sync_with_stdio(false);

    int n,m;

    cin>>n>>m;

    int cnt=0;

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

    {

        int x,y,z;

        cin>>x>>y>>z;

        add(x,y,z);add(y,x,-z);

        pos[x]=pos[y]=inf;

        rec[++cnt]=x,rec[++cnt]=y;

    }

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

    cnt=unique(rec+1,rec+n+1)-rec-1;

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

    {

        if(!vis[rec[i]])

        {

            flag=1;

            pos[rec[i]]=0;

            dfs(rec[i]);

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

        }

    }

    puts("Yes");

}
// 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]] = z - cnt[y];

                fa[fa[y]] = x;

            }

        }

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

    }

}


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

#include<cstdio>

#include<cmath>

#include<bits/stdc++.h>

using namespace std;

#define MAXN 400000+50

#define MAXM 400000+50

struct Edge{

    int from,to,dis,next;

}edge[MAXM];

#define inf 0x7fffffff

int head[MAXM];

int num=0;

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

{

    edge[++num].to=v,edge[num].dis=dis;

    edge[num].next=head[u];

    head[u]=num;

//    edge[++num].from=v,edge[num].to=u,edge[num].dis=dis;

}

bool vis[MAXN];

bool flag;

int pos[MAXN];

int rec[MAXN];

void dfs(int x)

{

    if(!flag) return;

    vis[x]=1;

    for(int i=head[x];i!=0;i=edge[i].next)

    {

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

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

        else if(t!=pos[edge[i].to]) {

            flag=0;return;

        }

    }

}

int main()

{

    ios::sync_with_stdio(false);

    int n,m;

    cin>>n>>m;

    int cnt=0;

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

    {

        int x,y,z;

        cin>>x>>y>>z;

        add(x,y,z);add(y,x,-z);

        pos[x]=pos[y]=inf;

        rec[++cnt]=x,rec[++cnt]=y;

    }

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

    cnt=unique(rec+1,rec+n+1)-rec-1;

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

    {

        if(!vis[rec[i]])

        {

            flag=1;

            pos[rec[i]]=0;

            dfs(rec[i]);

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

        }

    }

    puts("Yes");

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

#include<string.h>

#include<iostream>

#include<malloc.h>

#include<math.h>

#include<algorithm>



using namespace std;



const int maxn = 1e5+100;

int n,m,tag,f[maxn],dis[maxn];



int getfa(int x)

{

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

    int fx = getfa(f[x]);

    dis[x] += dis[f[x]];

    f[x] = fx;

    return f[x];

}



int main()

{

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

    tag = 1;

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

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

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

    {

        int x,y,d;

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

        int fx = getfa(x);

        int fy = getfa(y);

        if (fx == fy)

        {

            if (dis[y] != dis[x]+d) tag = 0;

        }

        else

        {

            f[fy] = fx;

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

        }

    }

    if (tag) printf("Yes");

    else

        printf("No");

    return 0;

}


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

#include <cstring>

#include <iostream>

using namespace std;

#define maxn 200000 + 10

int father[maxn];

int val[maxn];

int Find(int x)

{

    if (x == father[x])

        return x;

    int temp = father[x];

    father[x] = Find(father[x]);

    val[x] += val[temp];

    return father[x];

}

int n, m;

int main()

{

    while (~scanf("%d %d", &n, &m))

    {

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

        {

            father[i] = i;

            val[i] = 0;

        }

        int ans = 0;

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

        {

            int u, v, w;

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

//            u -= 1;

            int ru = Find(u), rv = Find(v);

            if (ru == rv)

            {

                if (val[u] + w != val[v])

                {

                    cout << "No" << endl;

                    return 0;

                }

            }

            else

            {

                father[rv] = ru;

                val[rv] = val[u] - val[v] + w;

            }

        }

        cout << "Yes" << endl;

    }

    return 0;

}
// Slower C++ solution:
//状态:

//题号:

#include <iostream>

#include <stdio.h>

#include <fstream>

#include <algorithm>

#include <ctime>

#include <cstdlib>

#include <cstring>

#include <vector>

#include <set>

#include <string>

#include <queue>

#include <map>

#include <iomanip>

#include <cassert>

#include <complex>

#include <functional>

#include <list>

using namespace std;

typedef pair<int,int> pii;

const int INF=1e9+7;

int dp[100005];

int n,m;

vector<pii> nei[100005];

int main(){

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

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

		int a,b,c;

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

		a--;

		b--;

		nei[a].push_back(make_pair(b,c));

		nei[b].push_back(make_pair(a,-c));

	}

	fill(dp,dp+100005,INF);

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

		if(dp[i]==INF){

			//f/or(int i=0;i<n;i++){

				//c/out<<dp[i]<<" ";

			//}

			//cout<<endl;

			queue<int> q;

			q.push(i);

			dp[i]=0;

			for(;q.size();){

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

				for(int j=0;j<nei[t].size();j++){

					pii p=nei[t][j];

					int pp=p.first,c=p.second;

					if(dp[pp]==INF){

						dp[pp]=dp[t]+c;

						q.push(pp);

					}

					else if(dp[pp]!=dp[t]+c){

						puts("No");

						return 0;

					}

				}

			}

		}

	}

	puts("Yes");

	return 0;

}

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

#include <cstring>

#include <iostream>

using namespace std;

#define maxn 200000 + 10

int father[maxn];

int val[maxn];

int Find(int x)

{

    if (x == father[x])

        return x;

    int temp = father[x];

    father[x] = Find(father[x]);

    val[x] += val[temp];

    return father[x];

}

int n, m;

int main()

{

    while (~scanf("%d %d", &n, &m))

    {

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

        {

            father[i] = i;

            val[i] = 0;

        }

        int ans = 0;

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

        {

            int u, v, w;

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

//            u -= 1;

            int ru = Find(u), rv = Find(v);

            if (ru == rv)

            {

                if (val[u] + w != val[v])

                {

                    cout << "No" << endl;

                    return 0;

                }

            }

            else

            {

                father[rv] = ru;

                val[rv] = val[u] - val[v] + w;

            }

        }

        cout << "Yes" << endl;

    }

    return 0;

}
// 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]] = z - cnt[y];

                fa[fa[y]] = x;

            }

        }

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

    }

}


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

#include <cstring>

#include <iostream>

using namespace std;

#define maxn 200000 + 10

int father[maxn];

int val[maxn];

int Find(int x)

{

    if (x == father[x])

        return x;

    int temp = father[x];

    father[x] = Find(father[x]);

    val[x] += val[temp];

    return father[x];

}

int n, m;

int main()

{

    while (~scanf("%d %d", &n, &m))

    {

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

        {

            father[i] = i;

            val[i] = 0;

        }

        int ans = 0;

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

        {

            int u, v, w;

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

//            u -= 1;

            int ru = Find(u), rv = Find(v);

            if (ru == rv)

            {

                if (val[u] + w != val[v])

                {

                    cout << "No" << endl;

                    return 0;

                }

            }

            else

            {

                father[rv] = ru;

                val[rv] = val[u] - val[v] + w;

            }

        }

        cout << "Yes" << endl;

    }

    return 0;

}
// Slower C++ solution:
//状态:

//题号:

#include <iostream>

#include <stdio.h>

#include <fstream>

#include <algorithm>

#include <ctime>

#include <cstdlib>

#include <cstring>

#include <vector>

#include <set>

#include <string>

#include <queue>

#include <map>

#include <iomanip>

#include <cassert>

#include <complex>

#include <functional>

#include <list>

using namespace std;

typedef pair<int,int> pii;

const int INF=1e9+7;

int dp[100005];

int n,m;

vector<pii> nei[100005];

int main(){

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

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

		int a,b,c;

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

		a--;

		b--;

		nei[a].push_back(make_pair(b,c));

		nei[b].push_back(make_pair(a,-c));

	}

	fill(dp,dp+100005,INF);

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

		if(dp[i]==INF){

			//f/or(int i=0;i<n;i++){

				//c/out<<dp[i]<<" ";

			//}

			//cout<<endl;

			queue<int> q;

			q.push(i);

			dp[i]=0;

			for(;q.size();){

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

				for(int j=0;j<nei[t].size();j++){

					pii p=nei[t][j];

					int pp=p.first,c=p.second;

					if(dp[pp]==INF){

						dp[pp]=dp[t]+c;

						q.push(pp);

					}

					else if(dp[pp]!=dp[t]+c){

						puts("No");

						return 0;

					}

				}

			}

		}

	}

	puts("Yes");

	return 0;

}

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

int dfs(int from)

{

    vis[from] = v;

    for(auto pa : g[from])

    {

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

        {

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

            if(dfs(pa.f) == -1)

            {

                return -1;

            }

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

        {

            return -1;

        }

    }

    return 1;

}



void dfs1(int from)

{

    vis[from] = v;

    for(auto pa: g[from])

    {

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

        {

            dfs1(pa.f);

        }

    }

}



int main() {

    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;

                }

            }

            if(dfs(i) == -1)

            {

               cout << "No";

                return 0;

            }

        }

        st.pop();

    }

    cout << "Yes";

    return 0;

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

#include<string.h>

#include<iostream>

#include<malloc.h>

#include<math.h>

#include<algorithm>



using namespace std;



const int maxn = 1e5+100;

int n,m,tag,f[maxn],dis[maxn];



int getfa(int x)

{

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

    int fx = getfa(f[x]);

    dis[x] += dis[f[x]];

    f[x] = fx;

    return f[x];

}



int main()

{

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

    tag = 1;

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

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

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

    {

        int x,y,d;

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

        int fx = getfa(x);

        int fy = getfa(y);

        if (fx == fy)

        {

            if (dis[y] != dis[x]+d) tag = 0;

        }

        else

        {

            f[fy] = fx;

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

        }

    }

    if (tag) printf("Yes");

    else

        printf("No");

    return 0;

}


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



using namespace std;



#define int long long





vector<int> x;

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



vector<int> used;

void dfs(int v) {

    used[v] = 1;

    for (auto l: g[v]) {

        int to = l.first;

        int w = l.second;

        if (x[to] != -1) {

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

                cout << "No";

                exit(0);

            }

        }

        else {

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

            if (!used[to]) dfs(to);

        }

    }

}



void solve() {

    int n;

    cin >> n;

    x.assign(n, -1);

    g.resize(n);

    used.resize(n);

    int m;

    cin >> m;

    set<int> st;

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

        int l, r, d;

        cin >> l >> r >> d;

        l--;

        r--;

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

        st.insert(r);

    }

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

        if (st.count(i)) continue;

        if (!used[i]) dfs(i);

    }

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

        if (!used[i]) {

            cout << "No";

            return;

        }

    }

    cout << "Yes";





}



signed main() {

    //freopen

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    solve();

}

/*

5 5

4 5 1

5 1 1

1 2 1

2 3 1

4 3 4



 */
// Slower C++ solution:
//状态:

//题号:

#include <iostream>

#include <stdio.h>

#include <fstream>

#include <algorithm>

#include <ctime>

#include <cstdlib>

#include <cstring>

#include <vector>

#include <set>

#include <string>

#include <queue>

#include <map>

#include <iomanip>

#include <cassert>

#include <complex>

#include <functional>

#include <list>

using namespace std;

typedef pair<int,int> pii;

const int INF=1e9+7;

int dp[100005];

int n,m;

vector<pii> nei[100005];

int main(){

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

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

		int a,b,c;

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

		a--;

		b--;

		nei[a].push_back(make_pair(b,c));

		nei[b].push_back(make_pair(a,-c));

	}

	fill(dp,dp+100005,INF);

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

		if(dp[i]==INF){

			//f/or(int i=0;i<n;i++){

				//c/out<<dp[i]<<" ";

			//}

			//cout<<endl;

			queue<int> q;

			q.push(i);

			dp[i]=0;

			for(;q.size();){

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

				for(int j=0;j<nei[t].size();j++){

					pii p=nei[t][j];

					int pp=p.first,c=p.second;

					if(dp[pp]==INF){

						dp[pp]=dp[t]+c;

						q.push(pp);

					}

					else if(dp[pp]!=dp[t]+c){

						puts("No");

						return 0;

					}

				}

			}

		}

	}

	puts("Yes");

	return 0;

}

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



using namespace std;



#define int long long





vector<int> x;

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



vector<int> used;

void dfs(int v) {

    used[v] = 1;

    for (auto l: g[v]) {

        int to = l.first;

        int w = l.second;

        if (x[to] != -1) {

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

                cout << "No";

                exit(0);

            }

        }

        else {

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

            if (!used[to]) dfs(to);

        }

    }

}



void solve() {

    int n;

    cin >> n;

    x.assign(n, -1);

    g.resize(n);

    used.resize(n);

    int m;

    cin >> m;

    set<int> st;

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

        int l, r, d;

        cin >> l >> r >> d;

        l--;

        r--;

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

        st.insert(r);

    }

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

        if (st.count(i)) continue;

        if (!used[i]) dfs(i);

    }

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

        if (!used[i]) {

            cout << "No";

            return;

        }

    }

    cout << "Yes";





}



signed main() {

    //freopen

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    solve();

}

/*

5 5

4 5 1

5 1 1

1 2 1

2 3 1

4 3 4



 */