Language: C++

#include<iostream>
#include<vector>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
 
vector<int> p,rank,cost;
 
 
int find(int x) {
    int prep = p[x];
    if(x != p[x]) {
        p[x] = find(p[x]);
    }
    cost[x] += cost[prep];
    return p[x];
}
 
void connect(int x, int y, int z) {
    int rootx = find(x), rooty = find(y);
    if(rootx == rooty) return;
    z = z + cost[x] - cost[y];
    if(rank[rooty] < rank[rootx]) {
        p[rooty] = rootx;
        cost[rooty] = z;
    }
    else {
        p[rootx] = rooty;
        cost[rootx] = -z;
        if(rank[rootx] == rank[rooty]) rank[rooty]+=1;
    }
}

void Set(int x) {
    p.push_back(x);
    rank.push_back(0);
    cost.push_back(0);
}
 
int main() {
    int n,q,com,x,y,z;
    scanf("%d %d", &n, &q);
    for(int i=0; i<n; i++) Set(i);
    for(int i=0; i<q; i++) {
        scanf("%d %d %d", &com, &x, &y);
        if(com == 1) {
            if(find(x) == find(y)) 
            {
                cout << cost[y]-cost[x] << endl;
            }
            else {
                cout << '?' << endl;
            }
        }
        else {
            
        scanf("%d", &z);
            connect(x,y,z);
    }
    return 0;
}


Compiler output:
a.cc: In function 'void connect(int, int, int)':
a.cc:25:8: error: reference to 'rank' is ambiguous
   25 |     if(rank[rooty] < rank[rootx]) {
      |        ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:25:22: error: reference to 'rank' is ambiguous
   25 |     if(rank[rooty] < rank[rootx]) {
      |                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:32:12: error: reference to 'rank' is ambiguous
   32 |         if(rank[rootx] == rank[rooty]) rank[rooty]+=1;
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:32:27: error: reference to 'rank' is ambiguous
   32 |         if(rank[rootx] == rank[rooty]) rank[rooty]+=1;
      |                           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:32:40: error: reference to 'rank' is ambiguous
   32 |         if(rank[rootx] == rank[rooty]) rank[rooty]+=1;
      |                                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc: In function 'void Set(int)':
a.cc:38:5: error: reference to 'rank' is ambiguous
   38 |     rank.push_back(0);
      |     ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:15: note:                 'std::vector<int> rank'
    9 | vector<int> p,rank,cost;
      |               ^~~~
a.cc: In function 'int main()':
a.cc:63:2: error: expected '}' at end of input
   63 | }
      |  ^
a.cc:42:12: note: to match this '{'
   42 | int main() {
      |            ^