Language: C++

#include <bits/stdc++.h>
using namespace std;
#define fol(i,n) for(int i=0;i<n;++i)
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;
typedef vector<P> vip;

const int length = 100001;
int par[length]={};
int rank[length]={};
int size[length]={};
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];
  }else{
    par[y] = x;
    size[x] += size[y];
    if(rank[x]==rank[y])rank[x]++;
  }
  return;
}
int get_size(int n){
  return size[find(n)];
}
void db(int n){
  fol(i,n+1){
    cout << size[i] << " ";
  }cout << endl;
  fol(i,n+1){
    cout << par[i] << " ";
  }cout << endl;
}
int main(){
  int n,m;cin>>n>>m;
  P p[m];
  int a,b;
  fol(i,m){
    cin>>a>>b;
    p[m-1-i] = make_pair(a,b);
  }
  init(n);
  ll ans[m+1] = {};
  ans[m] = (ll)(n * (n-1)) /2;
  for(int i=0;i<m;i++){
    a = p[i].first,
    b = p[i].second;
    ans[m-i-1] = ans[m-i];
    if(find(a)==find(b))continue;
    ans[m-i-1] -= (get_size(a)*get_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(int)':
a.cc:15:16: error: reference to 'rank' is ambiguous
   15 |     par[i] = i,rank[i] = 0,size[i]=1;
      |                ^~~~
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 /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc:15:28: error: reference to 'size' is ambiguous
   15 |     par[i] = i,rank[i] = 0,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:
/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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:26:6: error: reference to 'rank' is ambiguous
   26 |   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:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc:26:16: error: reference to 'rank' is ambiguous
   26 |   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:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc:28:5: error: reference to 'size' is ambiguous
   28 |     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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc:28:16: error: reference to 'size' is ambiguous
   28 |     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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc:31:5: error: reference to 'size' is ambiguous
   31 |     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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc:31:16: error: reference to 'size' is ambiguous
   31 |     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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc:32:8: error: reference to 'rank' is ambiguous
   32 |     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:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc:32:17: error: reference to 'rank' is ambiguous
   32 |     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:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc:32:25: error: reference to 'rank' is ambiguous
   32 |     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:11:5: note:                 'int rank [100001]'
   11 | int rank[length]={};
      |     ^~~~
a.cc: In function 'int get_size(int)':
a.cc:37:10: error: reference to 'size' is ambiguous
   37 |   return size[find(n)];
      |          ^~~~
/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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~
a.cc: In function 'void db(int)':
a.cc:41:13: error: reference to 'size' is ambiguous
   41 |     cout << size[i] << " ";
      |             ^~~~
/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:12:5: note:                 'int size [100001]'
   12 | int size[length]={};
      |     ^~~~