Language: C++

#include <bits/stdc++.h>
using namespace std;

int64_t ans = 0;

namespace UnionTree {
template <typename T>
int root(vector<T> &u, int x) {
    if (x < 0 || u.size() <= x) return -1;
    if (u[x].parent == -1)
        return x;
    else
        return u[x].parent = root(u, u[x].parent);
}
template <typename T>
bool connect(vector<T> &u, int a, int b) {
    a = root(u, a), b = root(u, b);
    if (a == b || a < 0 || b < 0) return false;
    {
        ans = max(0, ans - u[a].size * u[b].size);
        u[a].size += u[b].size;
    }
    u[b].parent = a;
    return true;
}
};  // namespace UnionTree

struct Node {
    int parent;
    int size;
};

int main() {
#ifdef DEBUG
    std::ifstream in("/home/share/inputf.in");
    std::cin.rdbuf(in.rdbuf());
#endif
    using namespace UnionTree;
    int N, M;
    cin >> N >> M;
    vector<Node> tree(N, {-1, 1});
    vector<int> A(M), B(M), ans_buf(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i];
        A[i]--, B[i]--;
    }
    ans = N * (N - 1) / 2;
    for (int i = M - 1; i >= 0; i--) {
        ans_buf[i] = ans;
        connect(tree, A[i], B[i]);
    }
    for (int i = 0; i < M; i++) {
        cout << ans_buf[i] << endl;
    }
    return 0;
}

Compiler output:
a.cc: In instantiation of 'bool UnionTree::connect(std::vector<_Tp>&, int, int) [with T = Node]':
a.cc:50:16:   required from here
   50 |         connect(tree, A[i], B[i]);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~
a.cc:20:18: error: no matching function for call to 'max(int, int64_t)'
   20 |         ans = max(0, ans - u[a].size * u[b].size);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included 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/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:20:18: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'int64_t' {aka 'long int'})
   20 |         ans = max(0, ans - u[a].size * u[b].size);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:20:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   20 |         ans = max(0, ans - u[a].size * u[b].size);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~