#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
int n, m, par[N];
vector<int> st[N];
set<ar<int, 2>> ed;
set<int> in[N];
vector<ar<int, 2>> use[N];
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
st[i].push_back(i);
par[i] = i;
}
ll ans = 0;
auto f = [&](int x) {
assert(x == par[x]);
return (long long) sz(st[x]) * sz(in[x]);
};
auto g = [&](int x) {
assert(x == par[x]);
return (long long) sz(st[x]) * (sz(st[x]) - 1);
};
auto merge = [&](int a, int b) {
a = par[a], b = par[b];
if (a == b) return;
if (sz(st[a]) < sz(st[b])) swap(a, b);
vector<ar<int, 2>> change;
ans -= g(a), ans -= g(b);
ans -= f(a), ans -= f(b);
for (int x : st[b]) {
par[x] = a;
st[a].push_back(x);
in[a].erase(x);
for (auto e : use[x]) {
change.push_back({e[0], e[1]});
}
use[x].clear();
}
st[b].clear();
for (int x : in[b]) {
if (par[x] != a) {
in[a].insert(x);
}
}
in[b].clear();
for (auto e : change) {
ar<int, 2> ne{par[e[0]], par[e[1]]};
ed.insert(ne);
use[ne[0]].push_back(ne);
use[ne[1]].push_back(ne);
}
ans += f(a);
ans += g(a);
};
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b, --a, --b;
ans -= f(par[b]);
if (par[a] != par[b]) in[par[b]].insert(a);
ans += f(par[b]);
ed.insert({par[a], par[b]});
use[par[a]].push_back({par[a], par[b]});
use[par[b]].push_back({par[a], par[b]});
if (ed.count({par[b], par[a]})) {
merge(a, b);
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
4 ms |
9684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |