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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

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

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


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



const int maxn=100000;

const int maxm=200000;

long p[maxn+1],c[maxn+1]={0};

int n,m;

int x,y;

int l,r,d;

int ans=1;



int find(int x)

{

    int tem;



    if(p[x]!=x){

        tem=p[x];

        p[x]=find(p[x]);

        c[x]=c[tem]+c[x];    //renew c[tem]+c[x]

    }



    return p[x];

}



void gather()

{

    p[y]=x;

    c[y]=c[l]+d-c[r];

}



int main()

{

    int i;



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



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

        p[i]=i;



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

    {

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

        x=find(l);y=find(r);

        if(x==y){

            if(c[r]-c[l]!=d)

                ans=0;

            else

                continue;

        }else{

            gather();

        }

    }



    if(ans)

        printf("Yes\n");

    else

        printf("No\n");



    return 0;

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

#include <algorithm>

#include <string.h>

#include <stdio.h>

using namespace std;

#define ll long long

const int pmax = 100005;

const int emax = 400005;

struct node { int to; int w; int next; } edge[emax];

int head[pmax], id = 0, vis[pmax];

int dis[pmax];

inline void add(int x, int y, int w) {

    edge[id].to = y;

    edge[id].w = w;

    edge[id].next = head[x];

    head[x] = id++;

}



bool dfs(int u) {

    vis[u] = 1;

    for (int i = head[u]; ~i; i = edge[i].next) {

        int v = edge[i].to;

        if (!vis[v]) {

            dis[v] = dis[u]+edge[i].w;

            if (!dfs(v)) return false;

        } else if (dis[u]+edge[i].w != dis[v]) return false;

    }

    return true;

}

int main(int argc, char const *argv[])

{

    int n, m;

    int a, b, c;

    memset(head, -1, sizeof head);

    memset(vis, 0, sizeof vis);

    memset(dis, 0, sizeof dis);

    cin >> n >> m;

    while (m--) {

        cin >> a >> b >> c;

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

    }

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

        if (!vis[i]) {

            if (!dfs(i)) return 0*printf("No\n");

        }

    printf("Yes\n");

    return 0;

}


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



const int maxn=100000;

const int maxm=200000;

long p[maxn+1],c[maxn+1]={0};

int n,m;

int x,y;

int l,r,d;

int ans=1;



int find(int x)

{

    int tem;



    if(p[x]!=x){

        tem=p[x];

        p[x]=find(p[x]);

        c[x]=c[tem]+c[x];    //renew c[tem]+c[x]

    }



    return p[x];

}



void gather()

{

    p[y]=x;

    c[y]=c[l]+d-c[r];

}



int main()

{

    int i;



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



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

        p[i]=i;



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

    {

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

        x=find(l);y=find(r);

        if(x==y){

            if(c[r]-c[l]!=d)

                ans=0;

            else

                continue;

        }else{

            gather();

        }

    }



    if(ans)

        printf("Yes\n");

    else

        printf("No\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: 23.015470862594565x):
#include <cstdio>



const int maxn=100000;

const int maxm=200000;

long p[maxn+1],c[maxn+1]={0};

int n,m;

int x,y;

int l,r,d;

int ans=1;



int find(int x)

{

    int tem;



    if(p[x]!=x){

        tem=p[x];

        p[x]=find(p[x]);

        c[x]=c[tem]+c[x];    //renew c[tem]+c[x]

    }



    return p[x];

}



void gather()

{

    p[y]=x;

    c[y]=c[l]+d-c[r];

}



int main()

{

    int i;



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



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

        p[i]=i;



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

    {

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

        x=find(l);y=find(r);

        if(x==y){

            if(c[r]-c[l]!=d)

                ans=0;

            else

                continue;

        }else{

            gather();

        }

    }



    if(ans)

        printf("Yes\n");

    else

        printf("No\n");



    return 0;

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

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

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


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

#include<cstdio>

using namespace std;

int fa[100005],dis[100005];



int find(int x)

{

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

	int fx=find(fa[x]);

	dis[x]+=dis[fa[x]];

	fa[x]=fx;

	return fx;

}

int main()

{

	int n,m;

	bool f=true;

	int l,r,d;

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

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

	{

		fa[i]=i;dis[i]=0;

	}

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

	{

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

		if (!f) continue;

		int fl=find(l),fr=find(r);

		if (fl==fr)

		{

			if (dis[l]-dis[r]!=d) f=false;

	    }

	    else 

	    {

	    	fa[fl]=fr;

	    	dis[fl]=dis[r]-dis[l]+d;

	   }

   }

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

   else printf("No\n");

   return 0;

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

#include <algorithm>

#include <string.h>

#include <stdio.h>

using namespace std;

#define ll long long

const int pmax = 100005;

const int emax = 400005;

struct node { int to; int w; int next; } edge[emax];

int head[pmax], id = 0, vis[pmax];

int dis[pmax];

inline void add(int x, int y, int w) {

    edge[id].to = y;

    edge[id].w = w;

    edge[id].next = head[x];

    head[x] = id++;

}



bool dfs(int u) {

    vis[u] = 1;

    for (int i = head[u]; ~i; i = edge[i].next) {

        int v = edge[i].to;

        if (!vis[v]) {

            dis[v] = dis[u]+edge[i].w;

            if (!dfs(v)) return false;

        } else if (dis[u]+edge[i].w != dis[v]) return false;

    }

    return true;

}

int main(int argc, char const *argv[])

{

    int n, m;

    int a, b, c;

    memset(head, -1, sizeof head);

    memset(vis, 0, sizeof vis);

    memset(dis, 0, sizeof dis);

    cin >> n >> m;

    while (m--) {

        cin >> a >> b >> c;

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

    }

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

        if (!vis[i]) {

            if (!dfs(i)) return 0*printf("No\n");

        }

    printf("Yes\n");

    return 0;

}


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

#include<cstdio>

using namespace std;

int fa[100005],dis[100005];



int find(int x)

{

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

	int fx=find(fa[x]);

	dis[x]+=dis[fa[x]];

	fa[x]=fx;

	return fx;

}

int main()

{

	int n,m;

	bool f=true;

	int l,r,d;

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

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

	{

		fa[i]=i;dis[i]=0;

	}

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

	{

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

		if (!f) continue;

		int fl=find(l),fr=find(r);

		if (fl==fr)

		{

			if (dis[l]-dis[r]!=d) f=false;

	    }

	    else 

	    {

	    	fa[fl]=fr;

	    	dis[fl]=dis[r]-dis[l]+d;

	   }

   }

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

   else printf("No\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: 4.388216638293579x):
#include<iostream>

#include<cstdio>

using namespace std;

int fa[100005],dis[100005];



int find(int x)

{

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

	int fx=find(fa[x]);

	dis[x]+=dis[fa[x]];

	fa[x]=fx;

	return fx;

}

int main()

{

	int n,m;

	bool f=true;

	int l,r,d;

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

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

	{

		fa[i]=i;dis[i]=0;

	}

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

	{

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

		if (!f) continue;

		int fl=find(l),fr=find(r);

		if (fl==fr)

		{

			if (dis[l]-dis[r]!=d) f=false;

	    }

	    else 

	    {

	    	fa[fl]=fr;

	    	dis[fl]=dis[r]-dis[l]+d;

	   }

   }

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

   else printf("No\n");

   return 0;

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

#include <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

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

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


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

#define MAXN 100500

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();int x, y, z;

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

	{

		

		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 <algorithm>

#include <string.h>

#include <stdio.h>

using namespace std;

#define ll long long

const int pmax = 100005;

const int emax = 400005;

struct node { int to; int w; int next; } edge[emax];

int head[pmax], id = 0, vis[pmax];

int dis[pmax];

inline void add(int x, int y, int w) {

    edge[id].to = y;

    edge[id].w = w;

    edge[id].next = head[x];

    head[x] = id++;

}



bool dfs(int u) {

    vis[u] = 1;

    for (int i = head[u]; ~i; i = edge[i].next) {

        int v = edge[i].to;

        if (!vis[v]) {

            dis[v] = dis[u]+edge[i].w;

            if (!dfs(v)) return false;

        } else if (dis[u]+edge[i].w != dis[v]) return false;

    }

    return true;

}

int main(int argc, char const *argv[])

{

    int n, m;

    int a, b, c;

    memset(head, -1, sizeof head);

    memset(vis, 0, sizeof vis);

    memset(dis, 0, sizeof dis);

    cin >> n >> m;

    while (m--) {

        cin >> a >> b >> c;

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

    }

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

        if (!vis[i]) {

            if (!dfs(i)) return 0*printf("No\n");

        }

    printf("Yes\n");

    return 0;

}


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

#define MAXN 100500

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();int x, y, z;

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

	{

		

		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<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: 23.03720497187679x):
#include<stdio.h>

#define MAXN 100500

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();int x, y, z;

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

	{

		

		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<cstdio>

using namespace std;

int fa[100005],dis[100005];



int find(int x)

{

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

	int fx=find(fa[x]);

	dis[x]+=dis[fa[x]];

	fa[x]=fx;

	return fx;

}

int main()

{

	int n,m;

	bool f=true;

	int l,r,d;

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

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

	{

		fa[i]=i;dis[i]=0;

	}

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

	{

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

		if (!f) continue;

		int fl=find(l),fr=find(r);

		if (fl==fr)

		{

			if (dis[l]-dis[r]!=d) f=false;

	    }

	    else 

	    {

	    	fa[fl]=fr;

	    	dis[fl]=dis[r]-dis[l]+d;

	   }

   }

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

   else printf("No\n");

   return 0;

}

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

#define MAXN 100500

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();int x, y, z;

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

	{

		

		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 <algorithm>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iomanip>

#define inf -2147483647

using namespace std;

struct info

{

    int l,r,d;

}s[400005];

int n,m;

int flag = 1;

int dis[100005],visited[100005];

int first[400005],nexti[400005];

void dfs(int t)

{

    if(!flag)return;

    int k = first[t];

    while(k != -1)

    {

        if(!flag)return;

        if(!visited[s[k].r])

        {

            dis[s[k].r] = dis[s[k].l] + s[k].d;

            visited[s[k].r] = 1;

            dfs(s[k].r);

        }

        else if(s[k].d != dis[s[k].r] - dis[s[k].l])

        {

            flag = 0;

            return;

        }

        k = nexti[k];

    }

}

int main()

{

    cin>>n>>m;

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

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

    {

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

        nexti[i] = first[s[i].l];

        first[s[i].l] = i;

        s[i + m].r = s[i].l;

        s[i + m].l = s[i].r;

        s[i + m].d = -s[i].d;

        nexti[i + m] = first[s[i + m].l];

        first[s[i + m].l] = i + m;

    }

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

    {

        if(!visited[i])

        {

            visited[i] = 1;

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

            dfs(i);

        }

    }

    if(flag)cout<<"Yes";

    else cout<<"No";

}


// Faster C++ solution for the same problem (measured speedup: 3.042626429235766x):


#include <string>  

#include <string.h>  

#include <iostream>  

#include <vector>  

#include <math.h>  

#include <stdio.h>  

using namespace std;  

const int maxn = 400200;  

int head[maxn],nxt[maxn],to[maxn],w[maxn];  

int flag[maxn];  

int sz;  

int n,m;  

int ans = 1;  

void init()  

{  

    for(int i=1;i<=n;i++) head[i]=-1,flag[i]=0;  

    sz=0;  

   

}  

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

{  

    nxt[++sz]=head[u];  

    to[sz]=v;  

    w[sz]=ww;  

    head[u]=sz;  

   

}  

int dis[maxn];  

void dfs(int u,int pre)  

{  

    if(!ans) return;  

    for(int i=head[u];~i;i=nxt[i]){  

        int v = to[i];  

        if(v==pre) continue;  

        if(flag[v]){  

            if(dis[v]-dis[u]!=w[i]){  

                ans=0;  

                return;  

            }  

        }else{  

            flag[v]=1;  

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

            dfs(v,u);  

        }  

    }  

}  

int main()  

{  

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

    init();  

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

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

        add(l,r,d);  

        add(r,l,-d);  

    }  

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

        if(flag[i]==0){  

            flag[i]=1;  

            dis[i]=0;  

            dfs(i,-1);  

        }  

    }  

    if(ans){  

        printf("Yes\n");  

    }else  

        printf("No\n");  

   

   

    return 0;  

}