Language: C++


#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>


#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>


// priority is lesser.
class union_find
{

	public:
	using value_type = std::int32_t;
	using size_type = decltype( std::declval< std::vector < value_type > >().size() );

	private:
	std::vector<int> parent_;
	std::vector<int> size_;

	public:

	// uf_[0,n_-1]
	union_find( size_type n_ )
		:parent_{}
		, size_{}
	{
		parent_.resize( n_ );
		std::iota( std::begin( parent_ ), std::end( parent_ ), 0 );

		size_.resize( n_, 1 );
	}

	auto root( value_type x ){

		const auto p_ = parent_[x];

		if( p_ == x )
			return x;
		else
		{
			size_[x] = size_[p_] = size_[x] + size_[p_];
			auto r = root( p_ );
			return parent_[x] = r;

		}

	}

	auto root( value_type x )const{
		if( x == parent_[x] )
			return x;
		else
			root( parent_[x] );
	}

	auto croot( value_type x )const{
		return root( x );
	}


	auto size()const{
		return parent_.size();
	}

	auto size( value_type x ){
		return size_[root( x )];
	}

	auto size( value_type x )const{
		return size_[croot( x )];
	}

	auto csize( value_type x )const{
		return size( x );
	}

	auto unite( value_type a_, value_type b_ ){
		if( a_ == b_ )
			return;

		// mut : to swap;
		auto ra_ = root( a_ );
		auto rb_ = root( b_ );

		if( ra_ == rb_ )
			return;


		const auto sa_ = size( a_ );
		const auto sb_ = size( b_ );

		//should be ra_ < rb_
		if( sa_ > sb_ )
			std::swap( ra_, rb_ );

		if( sa_ == sb_ )
			++size_[ra_];

		parent_[rb_] = ra_;

	}

	auto same( value_type a_, value_type b_ ){
		return root( a_ ) == root( b_ );
	}
};

int main(){
	std::uint32_t n, q;
	std::cin >> n >> q;

	union_find uf_{ n };

	for( int i{}; i < q; ++i )
	{
		int c, x, y;
		std::cin >> c >> x >> y;
		if( c == 0 )
			uf_.unite( x, y );
		else
			std::cout
			<< ( uf_.same( x, y ) ? "1" : "0" )
			<< "\n";
	}

	return 0;
}

int main(){

	std::uint32_t N, M;
	std::cin >> N >> M;

	union_find uf_{ N };

	int ans_{};
	for( int i{}; i < M; ++i )
	{
		int A, B;
		std::cin >> A >> B;
		--A; --B;
		
		uf_.unite( A, B );
		
		const auto t = uf_.size( A );

		ans_ = std::max( t, ans_ );
	}
	
	std::cout << ans_ << "\n";

	return 0;
}

Compiler output:
a.cc:19:33: error: 'int32_t' in namespace 'std' does not name a type
   19 |         using value_type = std::int32_t;
      |                                 ^~~~~~~
a.cc:20:65: error: 'value_type' was not declared in this scope
   20 |         using size_type = decltype( std::declval< std::vector < value_type > >().size() );
      |                                                                 ^~~~~~~~~~
a.cc:20:76: error: template argument 1 is invalid
   20 |         using size_type = decltype( std::declval< std::vector < value_type > >().size() );
      |                                                                            ^
a.cc:20:76: error: template argument 2 is invalid
a.cc:20:79: error: no matching function for call to 'declval<<expression error> >()'
   20 |         using size_type = decltype( std::declval< std::vector < value_type > >().size() );
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
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:2485:10: note: candidate: 'template<class _Tp> decltype (__declval<_Tp>(0)) std::declval()'
 2485 |     auto declval() noexcept -> decltype(__declval<_Tp>(0))
      |          ^~~~~~~
/usr/include/c++/14/type_traits:2485:10: note:   template argument deduction/substitution failed:
a.cc:20:79: error: template argument 1 is invalid
   20 |         using size_type = decltype( std::declval< std::vector < value_type > >().size() );
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
a.cc:20:79: error: no matching function for call to 'declval<<expression error> >()'
/usr/include/c++/14/type_traits:2485:10: note: candidate: 'template<class _Tp> decltype (__declval<_Tp>(0)) std::declval()'
 2485 |     auto declval() noexcept -> decltype(__declval<_Tp>(0))
      |          ^~~~~~~
/usr/include/c++/14/type_traits:2485:10: note:   template argument deduction/substitution failed:
a.cc:20:79: error: template argument 1 is invalid
   20 |         using size_type = decltype( std::declval< std::vector < value_type > >().size() );
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
a.cc:29:30: error: expected ')' before 'n_'
   29 |         union_find( size_type n_ )
      |                   ~          ^~~
      |                              )
a.cc:31:17: error: expected unqualified-id before ',' token
   31 |                 , size_{}
      |                 ^
a.cc:32:9: error: expected unqualified-id before '{' token
   32 |         {
      |         ^
a.cc:39:20: error: 'value_type' has not been declared
   39 |         auto root( value_type x ){
      |                    ^~~~~~~~~~
a.cc:55:20: error: 'value_type' has not been declared
   55 |         auto root( value_type x )const{
      |                    ^~~~~~~~~~
a.cc:62:21: error: 'value_type' has not been declared
   62 |         auto croot( value_type x )const{
      |                     ^~~~~~~~~~
a.cc:71:20: error: 'value_type' has not been declared
   71 |         auto size( value_type x ){
      |                    ^~~~~~~~~~
a.cc:75:20: error: 'value_type' has not been declared
   75 |         auto size( value_type x )const{
      |                    ^~~~~~~~~~
a.cc:79:21: error: 'value_type' has not been declared
   79 |         auto csize( value_type x )const{
      |                     ^~~~~~~~~~
a.cc:83:21: error: 'value_type' has not been declared
   83 |         auto unite( value_type a_, value_type b_ ){
      |                     ^~~~~~~~~~
a.cc:83:36: error: 'value_type' has not been declared
   83 |         auto unite( value_type a_, value_type b_ ){
      |                                    ^~~~~~~~~~
a.cc:109:20: error: 'value_type' has not been declared
  109 |         auto same( value_type a_, value_type b_ ){
      |                    ^~~~~~~~~~
a.cc:109:35: error: 'value_type' has not been declared
  109 |         auto same( value_type a_, value_type b_ ){
      |                                   ^~~~~~~~~~
a.cc: In function 'int main()':
a.cc:115:14: error: 'uint32_t' is not a member of 'std'; did you mean 'wint_t'?
  115 |         std::uint32_t n, q;
      |              ^~~~~~~~
      |              wint_t
a.cc:116:21: error: 'n' was not declared in this scope
  116 |         std::cin >> n >> q;
      |                     ^
a.cc:116:26: error: 'q' was not declared in this scope
  116 |         std::cin >> n >> q;
      |                          ^
a.cc:118:27: error: no matching function for call to 'union_find::union_find(<brace-enclosed initializer list>)'
  118 |         union_find uf_{ n };
      |                           ^
a.cc:15:7: note: candidate: 'union_find::union_find()'
   15 | class union_find
      |       ^~~~~~~~~~
a.cc:15:7: note:   candidate expects 0 arguments, 1 provided
a.cc:15:7: note: candidate: 'union_find::union_find(const union_find&)'
a.cc:15:7: note:   conversion of argument 1 would be ill-formed:
a.cc:15:7: note: candidate: 'union_find::union_find(union_find&&)'
a.cc:15:7: note:   conversion of argument 1 would be ill-formed:
a.cc: At global scope:
a.cc:135:5: error: redefinition of 'int main()'
  135 | int main(){
      |     ^~~~
a.cc:114:5: note: 'int main()' previously defined here
  114 | int main(){
      |     ^~~~
a.cc: In function 'int main()':
a.cc:137:14: error: 'uint32_t' is not a member of 'std'; did you mean 'wint_t'?
  137 |         std::uint32_t N, M;
      |              ^~~~~~~~
      |              wint_t
a.cc:138:21: error: 'N' was not declared in this scope
  138 |         std::cin >> N >> M;
      |                     ^
a.cc:138:26: error: 'M' was not declared in this scope
  138 |         std::cin >> N >> M;
      |                          ^
a.cc:140:27: error: no matching function for call to 'union_find::union_find(<brace-enclosed initializer list>)'
  140 |         union_find uf_{ N };
      |                           ^
a.cc:15:7: note: candidate: 'union_find::union_find()'
   15 | class union_find
      |       ^~~~~~~~~~
a.cc:15:7: note:   candidate expects 0 arguments, 1 provided
a.cc:15:7: note: candidate: 'union_find::union_find(const union_find&)'
a.cc:15:7: note:   conversion of argument 1 would be ill-formed:
a.cc:15:7: note: candidate: 'union_find::union_find(union_find&&)'
a.cc:15:7: note:   conversion of argument 1 would be ill-formed: