Language: C++

class UnionFind {
public:
  vector<int> Parent; //親の番号を格納する。親だった場合は-1

  // 作るときはParrentの値を-(その集合のサイズ)にする
  // こうするとすべてばらばらになる
  // 引数に初期化するノードの個数を指定する
  UnionFind(int N) {
    Parent = vector<int>(N, -1);
  }

  // Aがどのグループに属性しているか調べる
  int root (int A) {
    if (Parent[A] < 0) return A; // 自分がリーダー?
    return Parent[A] = root(Parent[A]);
  }

  // 自分のいるグループの頂点数を調べる
  // rootの値はもともとマイナスにしているのでサイズとして返すときは
  // さらに符号を付けることで正の値にしていることに注意
  int size (int A) {
    return -Parent[root(A)];
  }

  int connect(int A, int B) {
    // AとBを直接くっつけるのではなく、root(A) にroot(B)をくっつける
    A = root(A);
    B = root(B);

    if (A = B) { return false; }

    // 大きいほうに小さい方をくっつけたいので
    // 大小逆だったらひっくり返す
    if (size(A) < size(B)) swap(A,B);

    // Aのサイズを更新
    Parent[A] += Parent[B];
    // Ｂの親をAに更新
    Parent[B] = A;
    return true;
  }   

};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (int i = 0; i < M ; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<LL> ans(M);
  ans[M - 1] = (LL) N * (N-1) / 2;

  UnionFind uni(N);

  for (int i = M - 1; i >= 1 ; i--) {
    ans[i-1] = ans[i];
    if (uni.root(A[i]) != uni.root(B[i])) {
      ans[i-1] -= (LL) uni.size(A[i]) * uni.size(B[i]);
      uni.connect(A[i],B[i]);
    }
  }
  for (int i = 0; i < M; i++) {
    cout << ans[i] << endl;
  }
}

Compiler output:
a.cc:3:3: error: 'vector' does not name a type
    3 |   vector<int> Parent; //親の番号を格納する。親だった場合は-1
      |   ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:9:5: error: 'Parent' was not declared in this scope
    9 |     Parent = vector<int>(N, -1);
      |     ^~~~~~
a.cc:9:14: error: 'vector' was not declared in this scope
    9 |     Parent = vector<int>(N, -1);
      |              ^~~~~~
a.cc:9:21: error: expected primary-expression before 'int'
    9 |     Parent = vector<int>(N, -1);
      |                     ^~~
a.cc: In member function 'int UnionFind::root(int)':
a.cc:14:9: error: 'Parent' was not declared in this scope
   14 |     if (Parent[A] < 0) return A; // 自分がリーダー?
      |         ^~~~~~
a.cc:15:12: error: 'Parent' was not declared in this scope
   15 |     return Parent[A] = root(Parent[A]);
      |            ^~~~~~
a.cc: In member function 'int UnionFind::size(int)':
a.cc:22:13: error: 'Parent' was not declared in this scope
   22 |     return -Parent[root(A)];
      |             ^~~~~~
a.cc: In member function 'int UnionFind::connect(int, int)':
a.cc:34:28: error: 'swap' was not declared in this scope
   34 |     if (size(A) < size(B)) swap(A,B);
      |                            ^~~~
a.cc:37:5: error: 'Parent' was not declared in this scope
   37 |     Parent[A] += Parent[B];
      |     ^~~~~~
a.cc: In function 'int main()':
a.cc:47:3: error: 'cin' was not declared in this scope
   47 |   cin >> N >> M;
      |   ^~~
a.cc:48:3: error: 'vector' was not declared in this scope
   48 |   vector<int> A(M), B(M);
      |   ^~~~~~
a.cc:48:10: error: expected primary-expression before 'int'
   48 |   vector<int> A(M), B(M);
      |          ^~~
a.cc:50:12: error: 'A' was not declared in this scope
   50 |     cin >> A[i] >> B[i];
      |            ^
a.cc:50:20: error: 'B' was not declared in this scope
   50 |     cin >> A[i] >> B[i];
      |                    ^
a.cc:55:10: error: 'LL' was not declared in this scope
   55 |   vector<LL> ans(M);
      |          ^~
a.cc:55:14: error: 'ans' was not declared in this scope
   55 |   vector<LL> ans(M);
      |              ^~~
a.cc:62:18: error: 'A' was not declared in this scope
   62 |     if (uni.root(A[i]) != uni.root(B[i])) {
      |                  ^
a.cc:62:36: error: 'B' was not declared in this scope
   62 |     if (uni.root(A[i]) != uni.root(B[i])) {
      |                                    ^
a.cc:68:5: error: 'cout' was not declared in this scope
   68 |     cout << ans[i] << endl;
      |     ^~~~
a.cc:68:23: error: 'endl' was not declared in this scope
   68 |     cout << ans[i] << endl;
      |                       ^~~~