Language: C++


#include<iostream>
#include<vector>
using namespace std;
 
vector<int> p,rank,cost;
 
void makeSet(int x) {
    p.push_back(x);
    rank.push_back(0);
    cost.push_back(0);
}
 
int findSet(int x) {
    int prep = p[x];
    if(x != p[x]) {
        p[x] = findSet(p[x]);
    }
    cost[x] += cost[prep];
    return p[x];
}
 
void relate(int x, int y, int z) {
    int rootx = findSet(x), rooty = findSet(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;
    }
}
 
int main() {
    int n,q,com,x,y,z;
    cin >> n >> q;
    for(int i=0; i<n; i++) makeSet(i);
    for(int i=0; i<q; i++) {
        cin >> com >> x >> y;
        if(com == 0) {
            cin >> z;
            relate(x,y,z);
        }
        else {
            if(findSet(x) == findSet(y)) cout << cost[y]-cost[x] << endl;
            else cout << '?' << endl;
        }
    }
    return 0;
}


Compiler output:
a.cc: In function 'void makeSet(int)':
a.cc:10:5: error: reference to 'rank' is ambiguous
   10 |     rank.push_back(0);
      |     ^~~~
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:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~
a.cc: In function 'void relate(int, int, int)':
a.cc:27:8: error: reference to 'rank' is ambiguous
   27 |     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:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:27:22: error: reference to 'rank' is ambiguous
   27 |     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:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:34:12: error: reference to 'rank' is ambiguous
   34 |         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:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:34:27: error: reference to 'rank' is ambiguous
   34 |         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:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~
a.cc:34:40: error: reference to 'rank' is ambiguous
   34 |         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:6:15: note:                 'std::vector<int> rank'
    6 | vector<int> p,rank,cost;
      |               ^~~~