Language: C++

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;

int par[100010];
int rank[100010];
int weight[100010];

void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;rank[i]=0;weight[i]=0;
  }
  return;
}

int root(int n){
  if(par[n]==n){
    return n;
  }
  int r=root(par[n]);
  weight[n]+=weight[par[n]];
  return par[n]=r;
}

void unite(int x,int y,int w){
  int rx=root(x),ry=root(y);
  if(rx==ry){
    return;
  }
  if(rank[x]<rank[y]){
    par[rx]=ry;
    weight[rx]=w-weight[x]+weight[y];
  }  
  else{
    par[ry]=rx;
    weight[ry]=-w+weight[x]-weight[y];
    if(rank[rx]==rank[ry]) rank[rx]++;
  }
}

bool same(int x,int y){
  return root(x)==root(y);
}

int diff(int x,int y){
  return weight[x]-weight[y];
}

int main() {
  int n,m;
  cin>>n>>m;
  init(n);
  for(int i=0;i<m;i++){
    int l,r,d;
    cin>>l>>r>>d;
    if(same(l-1,r-1)){
      if(d==diff(r,l)) continue;
      else {
        cout<<"No"<<endl;
        return 0;
      }
    }
    else unite(l,r,d);
  }
  cout<<"Yes"<<endl;
  return 0;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:12:14: error: reference to 'rank' is ambiguous
   12 |     par[i]=i;rank[i]=0;weight[i]=0;
      |              ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~
a.cc: In function 'void unite(int, int, int)':
a.cc:31:6: error: reference to 'rank' is ambiguous
   31 |   if(rank[x]<rank[y]){
      |      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~
a.cc:31:14: error: reference to 'rank' is ambiguous
   31 |   if(rank[x]<rank[y]){
      |              ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~
a.cc:38:8: error: reference to 'rank' is ambiguous
   38 |     if(rank[rx]==rank[ry]) rank[rx]++;
      |        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~
a.cc:38:18: error: reference to 'rank' is ambiguous
   38 |     if(rank[rx]==rank[ry]) rank[rx]++;
      |                  ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~
a.cc:38:28: error: reference to 'rank' is ambiguous
   38 |     if(rank[rx]==rank[ry]) rank[rx]++;
      |                            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:5: note:                 'int rank [100010]'
    7 | int rank[100010];
      |     ^~~~