Language: C++

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdio>
using namespace std;

typedef long long ll;

const int maxn = 1e5+5;
int N,M;
int pre[maxn];
int size[maxn];
ll ans[maxn];
int l[maxn];
int r[maxn];

int find(int x)
{
    return x == pre[x] ? x : pre[x] = find(pre[x]); //这里一开始写错了，写成了x = find。。
}

void join(int a, int b)
{
    a = find(a), b = find(b);
    if(a != b)
    {
        size[a] += size[b];
        size[b] = 0;
       pre[b] = a;
    }
}
int main(){
	cin>>N>>M;

	for(int i=0;i<maxn;i++)
		fa[i] = i,size[i]=1;
	 memset(ans, 0, sizeof(ans));
		
	for(int i=1;i<=M;i++)
		scanf("%d%d", &l[i], &r[i]);
	ll cnt = N;
	ans[M+1] = cnt*(cnt-1)/2;
	for(int i = M;i>=2;i--){
		int x = find(l[i]);
		int y = find(r[i]);
		if(x!=y){
			ans[i] = ans[i+1]-size[x]*size[y];
			join(l[i],r[i]);
		}
		else 
			ans[i] = ans[i+1];
	}
	
	for(int i=2;i<=M+1;i++)
		  printf("%lld\n", ans[i]);
	
	return 0;
}

Compiler output:
a.cc: In function 'void join(int, int)':
a.cc:31:9: error: reference to 'size' is ambiguous
   31 |         size[a] += size[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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~
a.cc:31:20: error: reference to 'size' is ambiguous
   31 |         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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~
a.cc:32:9: error: reference to 'size' is ambiguous
   32 |         size[b] = 0;
      |         ^~~~
/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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:40:17: error: 'fa' was not declared in this scope; did you mean 'fma'?
   40 |                 fa[i] = i,size[i]=1;
      |                 ^~
      |                 fma
a.cc:40:27: error: reference to 'size' is ambiguous
   40 |                 fa[i] = i,size[i]=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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~
a.cc:51:43: error: reference to 'size' is ambiguous
   51 |                         ans[i] = ans[i+1]-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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~
a.cc:51:51: error: reference to 'size' is ambiguous
   51 |                         ans[i] = ans[i+1]-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:16:5: note:                 'int size [100005]'
   16 | int size[maxn];
      |     ^~~~