stablegradients/pie-gem5-pairs huggingface dataset:
// 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.2311821775022174x):


#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;  

}
// 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.4218232250741165x):


#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;  

}
// 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.020001062585439x):
#include <cstdio>

#include <iostream>

#include <vector>

#include <queue>

#include <cstring>

#include <algorithm>

#include <set>

#include <string>

using namespace std;

int l,r,d,n,k,t,m,t1,f[100000+10],x[100000+10];bool le;

int getfather(int u)

{

    int t;

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

    t=getfather(f[u]);

    x[u]=x[u]+x[f[u]];

    f[u]=t;

    return f[u];

}

void jia(int x2,int y2,int d)

{int i,j;

    i=getfather(x2);

    j=getfather(y2);

    if(i==j)

    {

        if(x[y2]-x[x2]!=d)

        {

            le=0;

            return;

        }

    }

        f[j]=i;

        x[j]=d+x[x2]-x[y2];

}

int main()

{

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

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

        f[i]=i;

    le=1;

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

    {

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

        jia(l,r,d);

        if(!le)

        {

            printf("No\n");

            return 0;

        }

    }

    printf("Yes\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.214590816057184x):
#include <cstdio>

#include <iostream>

#include <vector>

#include <queue>

#include <cstring>

#include <algorithm>

#include <set>

#include <string>

using namespace std;

int l,r,d,n,k,t,m,t1,f[100000+10],x[100000+10];bool le;

int getfather(int u)

{

    int t;

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

    t=getfather(f[u]);

    x[u]=x[u]+x[f[u]];

    f[u]=t;

    return f[u];

}

void jia(int x2,int y2,int d)

{int i,j;

    i=getfather(x2);

    j=getfather(y2);

    if(i==j)

    {

        if(x[y2]-x[x2]!=d)

        {

            le=0;

            return;

        }

    }

        f[j]=i;

        x[j]=d+x[x2]-x[y2];

}

int main()

{

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

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

        f[i]=i;

    le=1;

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

    {

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

        jia(l,r,d);

        if(!le)

        {

            printf("No\n");

            return 0;

        }

    }

    printf("Yes\n");

    return 0;

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

#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

const int MAXN=1e5+5;

int p[MAXN],vis[MAXN],error;

struct node{

	int x,y;

}push;

int n,m;

vector <node> vec[MAXN];

void solve(int x){

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

		node g=vec[x][i];

		if (vis[g.x])

		  if (g.y+p[x]!=p[g.x]){

		  	error=1;		

		  	return;

		  }

		  else continue;

		 vis[g.x]=1;

		 p[g.x]=p[x]+g.y;

		 solve(g.x);

		 if (error) return;

	}

}

int main(){

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

		memset(vis,0,sizeof(vis));

		memset(p,0,sizeof(p));

		error=0;

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

			int a,b,l;

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

			push.x=b;

			push.y=l;

			vec[a].push_back(push);

			push.x=a;

			push.y=-l;

			vec[b].push_back(push);

		}

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

		  if (!vis[i]){

		  	solve(i);

		  	if (error) break;

		  }

		if (!error) printf("Yes\n");

		else printf("No\n");

	

	return 0;

}

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

#include <iostream>

#include <vector>

#include <queue>

#include <cstring>

#include <algorithm>

#include <set>

#include <string>

using namespace std;

int l,r,d,n,k,t,m,t1,f[100000+10],x[100000+10];bool le;

int getfather(int u)

{

    int t;

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

    t=getfather(f[u]);

    x[u]=x[u]+x[f[u]];

    f[u]=t;

    return f[u];

}

void jia(int x2,int y2,int d)

{int i,j;

    i=getfather(x2);

    j=getfather(y2);

    if(i==j)

    {

        if(x[y2]-x[x2]!=d)

        {

            le=0;

            return;

        }

    }

        f[j]=i;

        x[j]=d+x[x2]-x[y2];

}

int main()

{

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

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

        f[i]=i;

    le=1;

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

    {

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

        jia(l,r,d);

        if(!le)

        {

            printf("No\n");

            return 0;

        }

    }

    printf("Yes\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.002544687137634x):
#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

int fa[100500];

int road[100500];

int get_fa(int x) {

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

    int a1=fa[x];

    int a2=get_fa(fa[x]);

    fa[x]=a2;

    road[x]=road[x]+road[a1];

    return a2;

}

int find_dis(int x) {

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

    return find_dis(fa[x])+road[x];

}



int main() {

    int n,cas,L,R,D;

    cin>>n>>cas;

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

        fa[i]=i;

        road[i]=0;

    }

    while(cas--) {

        cin>>L>>R>>D;

        int fl=get_fa(L);

        int fr=get_fa(R);

        if(fl==fr) {

            if(road[L]+D!=road[R]) {

                cout<<"No"<<endl;

                return 0;

            }

        } else {

            fa[fr]=fl;

            road[fr]=find_dis(L)-find_dis(R)+D;

        }

    }

    cout<<"Yes"<<endl;

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

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

int fa[100500];

int road[100500];

int get_fa(int x) {

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

    int a1=fa[x];

    int a2=get_fa(fa[x]);

    fa[x]=a2;

    road[x]=road[x]+road[a1];

    return a2;

}

int find_dis(int x) {

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

    return find_dis(fa[x])+road[x];

}



int main() {

    int n,cas,L,R,D;

    cin>>n>>cas;

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

        fa[i]=i;

        road[i]=0;

    }

    while(cas--) {

        cin>>L>>R>>D;

        int fl=get_fa(L);

        int fr=get_fa(R);

        if(fl==fr) {

            if(road[L]+D!=road[R]) {

                cout<<"No"<<endl;

                return 0;

            }

        } else {

            fa[fr]=fl;

            road[fr]=find_dis(L)-find_dis(R)+D;

        }

    }

    cout<<"Yes"<<endl;

    return 0;

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

#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

const int MAXN=1e5+5;

int p[MAXN],vis[MAXN],error;

struct node{

	int x,y;

}push;

int n,m;

vector <node> vec[MAXN];

void solve(int x){

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

		node g=vec[x][i];

		if (vis[g.x])

		  if (g.y+p[x]!=p[g.x]){

		  	error=1;		

		  	return;

		  }

		  else continue;

		 vis[g.x]=1;

		 p[g.x]=p[x]+g.y;

		 solve(g.x);

		 if (error) return;

	}

}

int main(){

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

		memset(vis,0,sizeof(vis));

		memset(p,0,sizeof(p));

		error=0;

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

			int a,b,l;

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

			push.x=b;

			push.y=l;

			vec[a].push_back(push);

			push.x=a;

			push.y=-l;

			vec[b].push_back(push);

		}

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

		  if (!vis[i]){

		  	solve(i);

		  	if (error) break;

		  }

		if (!error) printf("Yes\n");

		else printf("No\n");

	

	return 0;

}

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

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

int fa[100500];

int road[100500];

int get_fa(int x) {

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

    int a1=fa[x];

    int a2=get_fa(fa[x]);

    fa[x]=a2;

    road[x]=road[x]+road[a1];

    return a2;

}

int find_dis(int x) {

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

    return find_dis(fa[x])+road[x];

}



int main() {

    int n,cas,L,R,D;

    cin>>n>>cas;

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

        fa[i]=i;

        road[i]=0;

    }

    while(cas--) {

        cin>>L>>R>>D;

        int fl=get_fa(L);

        int fr=get_fa(R);

        if(fl==fr) {

            if(road[L]+D!=road[R]) {

                cout<<"No"<<endl;

                return 0;

            }

        } else {

            fa[fr]=fl;

            road[fr]=find_dis(L)-find_dis(R)+D;

        }

    }

    cout<<"Yes"<<endl;

    return 0;

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

#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

const int MAXN=1e5+5;

int p[MAXN],vis[MAXN],error;

struct node{

	int x,y;

}push;

int n,m;

vector <node> vec[MAXN];

void solve(int x){

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

		node g=vec[x][i];

		if (vis[g.x])

		  if (g.y+p[x]!=p[g.x]){

		  	error=1;		

		  	return;

		  }

		  else continue;

		 vis[g.x]=1;

		 p[g.x]=p[x]+g.y;

		 solve(g.x);

		 if (error) return;

	}

}

int main(){

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

		memset(vis,0,sizeof(vis));

		memset(p,0,sizeof(p));

		error=0;

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

			int a,b,l;

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

			push.x=b;

			push.y=l;

			vec[a].push_back(push);

			push.x=a;

			push.y=-l;

			vec[b].push_back(push);

		}

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

		  if (!vis[i]){

		  	solve(i);

		  	if (error) break;

		  }

		if (!error) printf("Yes\n");

		else printf("No\n");

	

	return 0;

}

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

/********                                                      ********/

/********                     Writer:孙~~                      ********/

/********                       Lang:C++                       ********/

/********                     Status:Unknow                    ********/

/********                                                      ********/

/**********************************************************************/

#include <iostream>

#include <string>

#include <cstdio>

#include <vector>

#include <queue>

#include <list>

#include <cstdlib>

#include <algorithm>

#include <set>

#include <map>

#include <deque>

#include <cstring>

#include <cmath>

//#include<bits/stdc++.h>

using namespace std;



const int INF=0x3f3f3f3f;

const int SIZE=100005;



int n,m;

vector<pair<int,int> > mp[SIZE];

bool vis[SIZE];

long long val[SIZE];



bool bfs(int i){

	queue<int> que;

	que.push(i);

	val[i]=0;

	while(!que.empty()){

		int v=que.front();

		que.pop();

		for(int i=0;i<mp[v].size();i++){

			pair<int,int> u=mp[v][i];

			int to=u.first;

			if(val[to]==INF){

				val[to]=val[v]+u.second;

				que.push(to);

			}

			else if(val[to]!=val[v]+u.second){

				return false;

			}

		}

	}

	return true;

}



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

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

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

	}

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

	for(int i=0;i<n;i++) if(val[i]==INF) if(!bfs(i)){

		puts("No");

		return 0;

	}

	puts("Yes");

	return 0;

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

using namespace std;



int n,m;

vector <pair<int,int> > a[200002];

bool vis[100001];

int d[100001];



bool dfs(int x,int y) {

	if (vis[x]) return d[x]!=y;

	vis[x]=true;

	d[x]=y;

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

		if (dfs(a[x][i].first,d[x]+a[x][i].second)) return true;

	}

	return false;

}



int main(){

	memset(vis,false,sizeof(vis));

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

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

		int x,y,z;

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

		x--;y--; 

		a[x].push_back(make_pair(y,z));

		a[y].push_back(make_pair(x,-z));

	}

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

		if (!vis[i]&&dfs(i,0)) {

			cout<<"No"<<endl;

			return 0;

		}

	}

	cout<<"Yes"<<endl;

	return 0;

}

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

/********                                                      ********/

/********                     Writer:孙~~                      ********/

/********                       Lang:C++                       ********/

/********                     Status:Unknow                    ********/

/********                                                      ********/

/**********************************************************************/

#include <iostream>

#include <string>

#include <cstdio>

#include <vector>

#include <queue>

#include <list>

#include <cstdlib>

#include <algorithm>

#include <set>

#include <map>

#include <deque>

#include <cstring>

#include <cmath>

//#include<bits/stdc++.h>

using namespace std;



const int INF=0x3f3f3f3f;

const int SIZE=100005;



int n,m;

vector<pair<int,int> > mp[SIZE];

bool vis[SIZE];

long long val[SIZE];



bool bfs(int i){

	queue<int> que;

	que.push(i);

	val[i]=0;

	while(!que.empty()){

		int v=que.front();

		que.pop();

		for(int i=0;i<mp[v].size();i++){

			pair<int,int> u=mp[v][i];

			int to=u.first;

			if(val[to]==INF){

				val[to]=val[v]+u.second;

				que.push(to);

			}

			else if(val[to]!=val[v]+u.second){

				return false;

			}

		}

	}

	return true;

}



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

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

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

	}

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

	for(int i=0;i<n;i++) if(val[i]==INF) if(!bfs(i)){

		puts("No");

		return 0;

	}

	puts("Yes");

	return 0;

}