Language: C++



// void merge(int fromP,int toP)
// {
// 	fromP = find_set(fromP);
// 	toP = find_set(toP);

// 	//UNION by RANK
// 	if(nodes[fromP].rank > nodes[toP].rank)	//fromP has higher rank
// 		nodes[toP].parent = fromP;
// 	else if(nodes[fromP].rank < nodes[toP].rank)	//toP has higher rank
// 		nodes[fromP].parent = toP;
// 	else
// 	{
// 		//Both have same rank and so anyone can be made as parent
// 		nodes[fromP].parent = toP;
// 		nodes[toP].rank +=1;		//Increase rank of parent
// 	}
// }


#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

struct node{
    int parent = -1;
    int rank = 0;
};

vector<node> nodes;

void make_set(int x){
    nodes[x].parent = x;
    nodes[x].rank = 1;
}

int find_set(int v)
{
	if(nodes[v].parent==v)
		return v;
	return nodes[v].parent=find_set(nodes[v].parent);
}

void merge_set(int a, int b){
    a = find_set(a);
    b = find_set(b);
    if(a!=b){
        if(nodes[a].rank<nodes[b].rank){
            nodes[a].parent = b;
        }
        else if(nodes[a].rank<nodes[b].rank){
            nodes[b].parent = a;
            
        }
        else{
            nodes[b].parent = a;
            nodes[a].rank+=1;
        }
    }
}

// void merge_set(int a, int b){
//     a = find_set(a);
//     b = find_set(b);
//     if(a!=b){
//         if(nodes[a].rank<nodes[b].rank)swap(a,b);
//         nodes[b].parent = a;
//         if(nodes[a].rank==nodes[b].rank)nodes[a].rank+=1;
//     }
// }

int main() {
    
    int N,M,a,b;
    cin>>N>>M;
    nodes.resize(N+1);
    
    for(int i=1;i<=N;++i){
        make_set(i);
    }
    
    for(int i=0;i<M;++i){
        cin>>a>>b;
        a = find(a);
        b = find(b);
        merge_set(a,b);
    }
    
    unordered_map<int,int> uomap;
    
    for(int i=1;i<=N;++i){
        ++uomap[nodes[i].parent];
    }
    
    cout<<uomap.size()-1;
    
  	return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:87:17: error: no matching function for call to 'find(int&)'
   87 |         a = find(a);
      |             ~~~~^~~
In file included from /usr/include/c++/14/bits/locale_facets.h:48,
                 from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:22:
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   candidate expects 3 arguments, 1 provided
a.cc:88:17: error: no matching function for call to 'find(int&)'
   88 |         b = find(b);
      |             ~~~~^~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   candidate expects 3 arguments, 1 provided