Language: C++

class UFTree {
private:
	static const int	UFTREE_NODE_NUM = 100000;
	int mNode[UFTREE_NODE_NUM];
	int mRank[UFTREE_NODE_NUM];
	int mNodeNum[UFTREE_NODE_NUM];

public:
	void init(int n)
	{
		for (int i = 0; i < n; i++) {
			mNode[i] = i;
			mRank[i] = 0;
			mNodeNum[i] = 1;
		}
	}

	int find(int x)
	{
		if (mNode[x] == x) {
			return x;
		}
		else {
			mNode[x] = find(mNode[x]);
			return mNode[x];
		}
	}

	void unite(int x, int y)
	{
		int idxX = find(x);
		int idxY = find(y);

		if (idxX == idxY) {
			return;
		}

		if (mRank[idxX] > mRank[idxY]) {
			mNode[idxY] = idxX;
			mNodeNum[idxX] += mNodeNum[idxY];
		}
		else {
			mNode[idxX] = idxY;
			mNodeNum[idxY] += mNodeNum[idxX];
			if (mRank[idxX] == mRank[idxY]) {
				mRank[idxY]++;
			}
		}
	}

	bool isSameSet(int x, int y)
	{
		return find(x) == find(y);
	}

	int getNodeNum(int x)
	{
		auto node = find(x);
		return mNodeNum[node];
	}
};

UFTree tree;

int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> A(M);
	vector<int> B(M);

	for (auto i = 0; i < M; ++i)
	{
		cin >> A[i] >> B[i];
	}

	vector<long long> result(M);

	auto num = ((long long)N) * (N - 1) / 2;
	result[M - 1] = num;
	tree.init(N);

	for (int i = M - 1; i >= 1; --i)
	{
		if (tree.isSameSet(A[i], B[i]))
		{
			result[i - 1] = result[i];
			continue;
		}

		long long numA = tree.getNodeNum(A[i]);
		long long numB = tree.getNodeNum(B[i]);

		num -= numA * (N - numA);
		num -= numB * (N - numA - numB);

		tree.unite(A[i], B[i]);
		num += (numA + numB) * (N - numA - numB);
		result[i - 1] = num;
	}

	for (auto i = 0; i < M; ++i)
	{
		cout << result[i] << endl;
	}

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:68:9: error: 'cin' was not declared in this scope
   68 |         cin >> N >> M;
      |         ^~~
a.cc:69:9: error: 'vector' was not declared in this scope
   69 |         vector<int> A(M);
      |         ^~~~~~
a.cc:69:16: error: expected primary-expression before 'int'
   69 |         vector<int> A(M);
      |                ^~~
a.cc:70:16: error: expected primary-expression before 'int'
   70 |         vector<int> B(M);
      |                ^~~
a.cc:74:24: error: 'A' was not declared in this scope
   74 |                 cin >> A[i] >> B[i];
      |                        ^
a.cc:74:32: error: 'B' was not declared in this scope
   74 |                 cin >> A[i] >> B[i];
      |                                ^
a.cc:77:16: error: expected primary-expression before 'long'
   77 |         vector<long long> result(M);
      |                ^~~~
a.cc:80:9: error: 'result' was not declared in this scope
   80 |         result[M - 1] = num;
      |         ^~~~~~
a.cc:85:36: error: 'A' was not declared in this scope
   85 |                 if (tree.isSameSet(A[i], B[i]))
      |                                    ^
a.cc:85:42: error: 'B' was not declared in this scope
   85 |                 if (tree.isSameSet(A[i], B[i]))
      |                                          ^
a.cc:91:50: error: 'A' was not declared in this scope
   91 |                 long long numA = tree.getNodeNum(A[i]);
      |                                                  ^
a.cc:92:50: error: 'B' was not declared in this scope
   92 |                 long long numB = tree.getNodeNum(B[i]);
      |                                                  ^
a.cc:104:17: error: 'cout' was not declared in this scope
  104 |                 cout << result[i] << endl;
      |                 ^~~~
a.cc:104:38: error: 'endl' was not declared in this scope
  104 |                 cout << result[i] << endl;
      |                                      ^~~~