1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
struct UFDS {
int par[100010];
void init()
{
memset(par, -1, sizeof(par));
}
int root(int x)
{
return par[x] < 0 ? x : par[x] = root(par[x]);
}
ll merge(int x, int y)
{
x = root(x);
y = root(y);
if (x == y)
return 0;
if (par[x] > par[y])
swap(x, y);
ll ret = 1LL * par[x] * par[y];
par[x] += par[y];
par[y] = x;
return ret;
}
} uf;
int main()
{
uf.init();
int n, m;
scanf("%d %d", &n, &m);
ll ans = 1LL * n * (n - 1) / 2;
vector<ll> ret;
vector<ii> inp;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
inp.push_back({u, v});
}
for (int i = m - 1; i >= 0; i--) {
ret.push_back(ans);
ans -= uf.merge(inp[i].first, inp[i].second);
}
reverse(ret.begin(), ret.end());
for (auto i : ret)
printf("%lld\n", i);
return 0;
}
|