Language: C++

#define _USE_MATH_DEFINES
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
typedef long long int LL;
typedef pair<int,int> P;

int N,M;
int A[100010],B[100010];
int par[100010],rank[100010],size[100010];
LL ans[100010];

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

int root(int x) {
	return par[x] == x ? x : par[x] = root(par[x]);
}

void unite(int x,int y) {
	int xs,ys;
	x = root(x);
	y = root(y);
	xs = size[x];
	ys = size[y];
	if(x==y) return;

	if(rank[x]<rank[y]) {
		par[x] = y;
		size[y] = xs+ys;
	} else {
		par[y] = x;
		size[x] = xs+ys;
		if(rank[x]==rank[y]) rank[x]++;
	}
}

int main() {
	cin >> N >> M;
	for(int i = 1;i <= M;i++)
		cin >> A[i] >> B[i];
	init();
	ans[M] = N*(N-1)/2;
	for(int i = M;i >= 1;i--) {
		int a = root(A[i]),b = root(B[i]);
		if(a==b) continue;
		ans[i-1] = ans[i]-(size[a]*size[b]);
		unite(a,b);
	}
	for(int i = 1;i <= M;i++)
		cout << ans[i] << endl;
	return 0;
}

Compiler output:
a.cc: In function 'void init()':
a.cc:24:17: error: reference to 'rank' is ambiguous
   24 |                 rank[i] = 0;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 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:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc:25:17: error: reference to 'size' is ambiguous
   25 |                 size[i] = 1;
      |                 ^~~~
In file included from /usr/include/c++/14/unordered_map:42,
                 from /usr/include/c++/14/functional:63,
                 from a.cc:3:
/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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:37:14: error: reference to 'size' is ambiguous
   37 |         xs = 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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc:38:14: error: reference to 'size' is ambiguous
   38 |         ys = 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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc:41:12: error: reference to 'rank' is ambiguous
   41 |         if(rank[x]<rank[y]) {
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc:41:20: error: reference to 'rank' is ambiguous
   41 |         if(rank[x]<rank[y]) {
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc:43:17: error: reference to 'size' is ambiguous
   43 |                 size[y] = xs+ys;
      |                 ^~~~
/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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc:46:17: error: reference to 'size' is ambiguous
   46 |                 size[x] = xs+ys;
      |                 ^~~~
/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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc:47:20: error: reference to 'rank' is ambiguous
   47 |                 if(rank[x]==rank[y]) rank[x]++;
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc:47:29: error: reference to 'rank' is ambiguous
   47 |                 if(rank[x]==rank[y]) rank[x]++;
      |                             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc:47:38: error: reference to 'rank' is ambiguous
   47 |                 if(rank[x]==rank[y]) rank[x]++;
      |                                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:18:17: note:                 'int rank [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                 ^~~~
a.cc: In function 'int main()':
a.cc:60:36: error: reference to 'size' is ambiguous
   60 |                 ans[i-1] = ans[i]-(size[a]*size[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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~
a.cc:60:44: error: reference to 'size' is ambiguous
   60 |                 ans[i-1] = ans[i]-(size[a]*size[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:18:30: note:                 'int size [100010]'
   18 | int par[100010],rank[100010],size[100010];
      |                              ^~~~