#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int p[N], sz[N];
set<int> arr[N], brr[N];
void Init(int n) {
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
}
int Find(int x) {
if (x == p[x]) return p[x];
return Find(p[x]);
}
ll C(ll x) {
return x * (x - 1);
}
map<pair<int, int>, int> imaput, strelica;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
Init(n);
ll ans = 0;
while (m--) {
int a, b;
cin >> a >> b;
imaput[{a, b}] = 1;
if (Find(a) == Find(b)) {
cout << ans << en;
continue;
}
int x = Find(a);
int y = Find(b);
if (imaput[{b, a}] != 1) {
if (strelica.find({y, a}) == strelica.end()) {
ans += sz[y];
arr[y].insert(a);
brr[x].insert({a, y});
strelica[{y, a}] = 1;
}
cout << ans << en;
continue;
}
ans += C(sz[x] + sz[y]) - C(sz[x]) - C(sz[y]);
if (sz[x] < sz[y]) {swap(x, y); swap(a, b);}
int cnt = arr[x].size();
for (int u : arr[y]) {
int o = Find(u);
if (o == x) {
ans -= sz[y];
}
else {
ans += sz[x];
arr[x].insert(u);
brr[o].insert(x);
}
brr[o].erase(y);
}
for (int u : brr[y]) {
int o = Find(u);
if (o == x) {
ans -= sz[x];
cnt -= 1;
arr[o].erase(y);
}
else {
brr[x].insert(u);
}
}
arr[y].clear(); brr[y].clear();
ans += (ll)(cnt * sz[y]);
sz[x] += sz[y];
p[y] = x;
cout << ans << en;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |