Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod = 1e9 + 7;

int par[100002];  // 親
int Rank[100002]; // 木の深さ
int size[100002]; // 木の要素数
ll si[100001];

int u[100001];
vector<int> X[100001];

vector<int> Y[100001];

void init(int n){
	for(int i=0; i<n; i++){
		par[i] = i;
		Rank[i] = 0;
		size[i] = 1;
	}
}

int find(int x){
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(Rank[x] < Rank[y]){
		par[x] = y;
		size[y] += size[x];
		set<int> st;
		for(int i=0; i<Y[x].size(); i++){
			st.insert(Y[x][i]);
		}
		for(int i=0; i<Y[y].size(); i++){
			st.insert(Y[y][i]);
		}
		si[x] = st.size();
		si[y] = si[x];
	}else{
		par[y] = x;
		size[x] += size[y];
		set<int> st;
		for(int i=0; i<Y[x].size(); i++){
			st.insert(Y[x][i]);
		}
		for(int i=0; i<Y[y].size(); i++){
			st.insert(Y[y][i]);
		}
		si[x] = st.size();
		si[y] = si[x];
		if(Rank[x] == Rank[y]) Rank[x]++;
	}
}

bool same(int x, int y){
	return find(x) == find(y);
}

int main(){
	int N; cin >> N;
	for(int i=0; i<N; i++){
		int a, b; cin >> a >> b;
		X[a].push_back(b);
		Y[b].push_back(a);
	}
	init(100001);
  int r=0;
	for(int i=0; i<100001; i++){
      for(int j=1; j<X[i].size(); j++){
			unite(X[i][0], X[i][j]);
        r++;
		}
	}
	ll ans = 0;
  	ll so[100001];
  ll cn[100001];
  fill(so, so+100001, 0);
  fill(cn, cn+100001, 0);
	for(int i=0; i<100001; i++){
		so[par[i]] = max(so[par[i]], si[i]);
      cn[par[i]]++;
      
	}
  for(int i=0; i<100001; i++){
    ans += so[i] * cn[i];
  }if(ans < N) cout << 0 < endl;
	else {cout << ans-N << endl;}
	return 0;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:20:17: error: reference to 'size' is ambiguous
   20 |                 size[i] = 1;
      |                 ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 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:8:5: note:                 'int size [100002]'
    8 | int size[100002]; // 木の要素数
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:38:17: error: reference to 'size' is ambiguous
   38 |                 size[y] += size[x];
      |                 ^~~~
/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:8:5: note:                 'int size [100002]'
    8 | int size[100002]; // 木の要素数
      |     ^~~~
a.cc:38:28: error: reference to 'size' is ambiguous
   38 |                 size[y] += size[x];
      |                            ^~~~
/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:8:5: note:                 'int size [100002]'
    8 | int size[100002]; // 木の要素数
      |     ^~~~
a.cc:50:17: error: reference to 'size' is ambiguous
   50 |                 size[x] += size[y];
      |                 ^~~~
/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:8:5: note:                 'int size [100002]'
    8 | int size[100002]; // 木の要素数
      |     ^~~~
a.cc:50:28: error: reference to 'size' is ambiguous
   50 |                 size[x] += size[y];
      |                            ^~~~
/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:8:5: note:                 'int size [100002]'
    8 | int size[100002]; // 木の要素数
      |     ^~~~
a.cc: In function 'int main()':
a.cc:95:26: error: no match for 'operator<' (operand types are 'std::basic_ostream<char>' and '<unresolved overloaded function type>')
   95 |   }if(ans < N) cout << 0 < endl;
      |                ~~~~~~~~~~^~~~~~
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181:
/usr/include/c++/14/bits/regex.h:1143:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1143 |     operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1143:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1224:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1224 |     operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1224:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1317:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1317 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1317:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1391:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1391 |     operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1391:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   couldn't deduce template parameter '_Bi_iter'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1485:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1485 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1485:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1560:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1560 |     operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1560:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   couldn't deduce template parameter '_Bi_iter'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/regex.h:1660:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1660 |     operator<(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1660:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:1045:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1045 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1045:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::pair<_T1, _T2>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:448:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  448 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::reverse_iterator<_Iterator>'
   95 |   }if(ans < N) cout << 0 < endl;
      |                            ^~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  493 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note:   template argument deduction/substitution failed:
a.cc:95:28: note:   'std::basic_ostream<char>' is not derived from 'const std::reverse_iterator<_Iterator>'
   95 |   }if(ans <