Language: C++

#include "stdafx.h"
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <functional>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <tuple>
#include <numeric>
#include <assert.h>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n)				for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n)			for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define INF						2e9
#define MOD						(1000 * 1000 * 1000 + 7)
#define Ceil(x, n)				(((((x))+((n)-1))/n))		/* Nの倍数に切り上げ割り算 */
#define CeilN(x, n)				(((((x))+((n)-1))/n)*n)		/* Nの倍数に切り上げ */
#define FloorN(x, n)			((x)-(x)%(n))				/* Nの倍数に切り下げ */
#define IsOdd(x)				(((x)&0x01UL) == 0x01UL)			
#define IsEven(x)				(!IsOdd((x)))						
#define	BitSetV(Val,Bit)		((Val) |= (Bit))			
#define	BitTstV(Val,Bit)		((Val) & (Bit))				
#define ArrayLength(x)			(sizeof( x ) / sizeof( x[ 0 ]))
#define MAX_DWORD				(0xFFFFFFFF)
#define	MAX_SDWORD				((SDWORD)0x7FFFFFFF)
#define	MIN_SDWORD				((SDWORD)0x80000000)
#define	MAX_QWORD				((QWORD)0xFFFFFFFFFFFFFFFF)
#define	MIN_QWORD				((QWORD)0x0000000000000000)
#define	MAX_SQWORD				((SQWORD)0x7FFFFFFFFFFFFFFF)
#define	MIN_SQWORD				((SQWORD)0x8000000000000000)
#define M_PI					3.14159265358979323846

typedef long					SDWORD;
typedef long long				SQWORD;
typedef unsigned long			DWORD;
typedef unsigned long long int	QWORD;
typedef long long ll;
typedef pair<ll, ll>			P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
#define N_MAX	(200 * 1000)
#define K_MAX	(10)
#define M_MAX	(10)
#define Q_MAX	(10)
#define H_MAX	(1000)
#define W_MAX	(1000)

struct UnionFind {
	vector<int> par, rank, sz;

	UnionFind(int n) {
		par.resize(n), rank.resize(n, 0), sz.resize(n, 1);
		for (int i = 0; i < n; i++) par[i] = i;
	}

	int root(int x) {
		return par[x] == x ? x : par[x] = root(par[x]);
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	int size(int x) {
		return sz[root(x)];
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return;
		if (rank[x] < rank[y]) {
			par[x] = y;
			sz[y] += sz[x];
		} else {
			par[y] = x;
			sz[x] += sz[y];
			if (rank[x] == rank[y]) rank[x]++;
		}
	}
};


int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> a(M), b(M);
	REP(i, M) {
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
	}

	UnionFind uTree(N);
	vector<int> ans(M, 0);
	ans[M - 1] = N * (N - 1);
	for (int i = M - 1; i >= 1; i--) {
		if (uTree.root(a[i]) == uTree.root(b[i])) {
			ans[i - 1] = ans[i];
		} else {
			ans[i - 1] = ans[i] - uTree.size(a[i]) * uTree.size(b[i]);
		}
		uTree.unite(a[i], b[i]);
	}

	REP(i, M) {
		cout << ans[i] << endl;
	}
	return 0;
}


Compiler output:
a.cc:1:10: fatal error: stdafx.h: No such file or directory
    1 | #include "stdafx.h"
      |          ^~~~~~~~~~
compilation terminated.