Language: C++

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

#define forn(e,n,s) for(int i = e; i < n;i += s)
#define forr(e,n,s) for(int i = e; i >= n; i -= s)
#define PI 3.14159265358979323846264338327950L
#define ll long long

using namespace std;

const int MOD = 1e9+7;

void go(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout<<fixed;
  cout<<setprecision(20);

  #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
  #endif
}
vector<int> arr,size;
int comps;
int getp(int x){
    return arr[x] = (x!=arr[x] ? getp(arr[x]) : x);
}
void merg(int x,int y){
    int p_a = getp(x);
    int p_b = getp(y);
    if(p_a == p_b) return;
    if(size[p_a] > size[p_b]) swap(p_a,p_b);
    arr[p_b] = p_a;
    size[p_a] += size[p_b];
    comps--;
}



void solve(){
    int n,m; cin>>n>>m;
    arr.resize(n+1);
    size.resize(n+1,1);
    comps = n;
    for(int i = 0; i <= n; i++) arr[i] = i; 
    while(m--){
        int a,b; cin>>a>>b;
        merg(a,b);
    }
    cout<<comps-1;
} 

int main(){
    go(); 

    // code here
    int t = 1; 
    while(t--) solve();
   
}









Compiler output:
a.cc: In function 'void merg(int, int)':
a.cc:34:8: error: reference to 'size' is ambiguous
   34 |     if(size[p_a] > size[p_b]) swap(p_a,p_b);
      |        ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:25:17: note:                 'std::vector<int> size'
   25 | vector<int> arr,size;
      |                 ^~~~
a.cc:34:20: error: reference to 'size' is ambiguous
   34 |     if(size[p_a] > size[p_b]) swap(p_a,p_b);
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:25:17: note:                 'std::vector<int> size'
   25 | vector<int> arr,size;
      |                 ^~~~
a.cc:36:5: error: reference to 'size' is ambiguous
   36 |     size[p_a] += size[p_b];
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:25:17: note:                 'std::vector<int> size'
   25 | vector<int> arr,size;
      |                 ^~~~
a.cc:36:18: error: reference to 'size' is ambiguous
   36 |     size[p_a] += size[p_b];
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:25:17: note:                 'std::vector<int> size'
   25 | vector<int> arr,size;
      |                 ^~~~
a.cc: In function 'void solve()':
a.cc:45:5: error: reference to 'size' is ambiguous
   45 |     size.resize(n+1,1);
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:25:17: note:                 'std::vector<int> size'
   25 | vector<int> arr,size;
      |                 ^~~~