stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 4.818150300982875x):
#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 <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: 3.954839806314063x):
#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 <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;

}

// Faster C++ solution for the same problem (measured speedup: 3.555429738920041x):
#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:
/*|In The Name Of Allah|*/



#include <bits/stdc++.h>



using namespace std;



typedef pair < int , pair < int , int > > pll;



const int N = 1e5+5;



int n;

int m;

int dist[N];

int indeg[N];

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



int main(){

    memset(dist , -1 , sizeof dist);

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

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

        int l , r , d;

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

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

        indeg[r]++;

    }

    int bad = 0;

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

        if(!indeg[i]){

            queue < int > q;

            q.push(i);

            dist[i] = 0;

            while(!q.empty()){

                int u = q.front();

                q.pop();

                for(auto nw : adj[u]){

                    int v = nw.first;

                    int d = nw.second;

                    if(dist[v] == -1){

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

                        if(dist[v] > 1e9)

                            bad = 1;

                        q.push(v);

                    } else {

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

                            bad = 1;

                        }

                    }

                }

            }

        }

    }

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

        if(dist[i] == -1)

            bad = 1;

    }

    puts(bad ? "No" : "Yes");

}


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

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}
// 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.0922999968825895x):
#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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}
// 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: 4.868103225952723x):
#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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}
// 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: 3.995842225037338x):
#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;       //input members

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}
// Slower C++ solution:
#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;

}

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

int main()    //main body

{

   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");          //output

   else     printf("No\n");

}