#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(nullptr)->sync_with_stdio(0);
int n;
cin >> n;
vector<set<int>> g(n + 1);
vector<set<pair<int, int>>> rg(n + 1);
vector<int> r(n + 1), sz(n + 1);
function<int(int)> f = [&](int x) {
return x == r[x] ? x : r[x] = f(r[x]);
};
long long ans = 0;
function<void(int, int)> u = [&](int a, int b) {
if(f(a) == f(b)) {
return;
}
assert(a == f(a) && b == f(b));
if(sz[a] < sz[b]) { // b do a
swap(a, b);
}
auto remove = [&](int aa, int bb) {
set<pair<int, int>>::iterator it;
while((it = rg[bb].lower_bound({aa, 0})) != rg[bb].end() && it->first == aa) {
ans -= sz[bb];
rg[bb].erase(it);
}
};
g[a].erase(b);
g[b].erase(a);
remove(a, b);
remove(b, a);
vector<int> todo;
for(int x : g[b]) {
set<pair<int, int>>::iterator it;
while((it = rg[x].lower_bound({b, 0})) != rg[x].end() && it->first == b) {
int t = it->second;
rg[x].erase(it);
rg[x].insert({a, t});
}
g[a].insert(x);
if(g[x].count(a)) {
todo.push_back(x);
}
}
ans += (long long)sz[b] * rg[a].size();
for(pair<int, int> x : rg[b]) {
g[x.first].erase(b);
g[x.first].insert(a);
if(!rg[a].count(x)) {
ans += sz[a];
rg[a].insert(x);
}
else {
ans -= sz[b]; // overcounted in line 51
}
if(g[a].count(x.first)) {
todo.push_back(x.first);
}
}
ans -= (long long)sz[a] * (sz[a] - 1);
ans -= (long long)sz[b] * (sz[b] - 1);
r[b] = a;
sz[a] += sz[b];
ans += (long long)sz[a] * (sz[a] - 1);
for(int x : todo) {
u(a, x);
}
};
auto edge = [&](int a, int b) { // a -> b
int v = a;
a = f(a);
b = f(b);
if(a == b) {
return;
}
if(!rg[b].count({a, v})) {
ans += sz[b];
rg[b].insert({a, v});
g[a].insert(b);
}
if(g[a].count(b) && g[b].count(a)) {
u(a, b);
}
};
for(int i = 1; i <= n; i++) {
r[i] = i;
sz[i] = 1;
}
int m;
cin >> m;
for(int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
edge(a, b);
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
80 ms |
5892 KB |
Output is correct |
36 |
Correct |
98 ms |
8140 KB |
Output is correct |
37 |
Correct |
94 ms |
8276 KB |
Output is correct |
38 |
Correct |
91 ms |
7960 KB |
Output is correct |
39 |
Correct |
3 ms |
584 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
3 ms |
584 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
4 ms |
628 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
588 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
596 KB |
Output is correct |
48 |
Correct |
3 ms |
716 KB |
Output is correct |
49 |
Correct |
9 ms |
1364 KB |
Output is correct |
50 |
Correct |
94 ms |
8260 KB |
Output is correct |
51 |
Correct |
9 ms |
852 KB |
Output is correct |
52 |
Correct |
114 ms |
7120 KB |
Output is correct |
53 |
Correct |
9 ms |
1364 KB |
Output is correct |
54 |
Correct |
88 ms |
7680 KB |
Output is correct |
55 |
Correct |
4 ms |
852 KB |
Output is correct |
56 |
Correct |
4 ms |
852 KB |
Output is correct |
57 |
Correct |
5 ms |
1364 KB |
Output is correct |
58 |
Correct |
4 ms |
1360 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
69 ms |
5504 KB |
Output is correct |
61 |
Correct |
4 ms |
724 KB |
Output is correct |
62 |
Correct |
98 ms |
7880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
80 ms |
5892 KB |
Output is correct |
36 |
Correct |
98 ms |
8140 KB |
Output is correct |
37 |
Correct |
94 ms |
8276 KB |
Output is correct |
38 |
Correct |
91 ms |
7960 KB |
Output is correct |
39 |
Correct |
3 ms |
584 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
3 ms |
584 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
4 ms |
628 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
2 ms |
588 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
596 KB |
Output is correct |
48 |
Correct |
3 ms |
716 KB |
Output is correct |
49 |
Correct |
9 ms |
1364 KB |
Output is correct |
50 |
Correct |
94 ms |
8260 KB |
Output is correct |
51 |
Correct |
9 ms |
852 KB |
Output is correct |
52 |
Correct |
114 ms |
7120 KB |
Output is correct |
53 |
Correct |
9 ms |
1364 KB |
Output is correct |
54 |
Correct |
88 ms |
7680 KB |
Output is correct |
55 |
Correct |
4 ms |
852 KB |
Output is correct |
56 |
Correct |
4 ms |
852 KB |
Output is correct |
57 |
Correct |
5 ms |
1364 KB |
Output is correct |
58 |
Correct |
4 ms |
1360 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
69 ms |
5504 KB |
Output is correct |
61 |
Correct |
4 ms |
724 KB |
Output is correct |
62 |
Correct |
98 ms |
7880 KB |
Output is correct |
63 |
Correct |
379 ms |
44080 KB |
Output is correct |
64 |
Correct |
371 ms |
44124 KB |
Output is correct |
65 |
Correct |
415 ms |
44116 KB |
Output is correct |
66 |
Correct |
103 ms |
14796 KB |
Output is correct |
67 |
Correct |
204 ms |
19576 KB |
Output is correct |
68 |
Correct |
112 ms |
14892 KB |
Output is correct |
69 |
Correct |
288 ms |
19476 KB |
Output is correct |
70 |
Correct |
97 ms |
14892 KB |
Output is correct |
71 |
Correct |
96 ms |
14852 KB |
Output is correct |
72 |
Correct |
234 ms |
20308 KB |
Output is correct |
73 |
Correct |
250 ms |
20260 KB |
Output is correct |
74 |
Correct |
756 ms |
40388 KB |
Output is correct |
75 |
Correct |
428 ms |
24792 KB |
Output is correct |
76 |
Correct |
554 ms |
32524 KB |
Output is correct |
77 |
Correct |
492 ms |
32732 KB |
Output is correct |
78 |
Correct |
146 ms |
19532 KB |
Output is correct |
79 |
Correct |
285 ms |
22884 KB |
Output is correct |
80 |
Correct |
146 ms |
19568 KB |
Output is correct |
81 |
Correct |
265 ms |
22932 KB |
Output is correct |
82 |
Correct |
446 ms |
33492 KB |
Output is correct |
83 |
Correct |
464 ms |
33464 KB |
Output is correct |
84 |
Correct |
449 ms |
52300 KB |
Output is correct |
85 |
Correct |
420 ms |
52232 KB |
Output is correct |
86 |
Correct |
111 ms |
14124 KB |
Output is correct |
87 |
Correct |
134 ms |
16356 KB |
Output is correct |
88 |
Correct |
256 ms |
20324 KB |
Output is correct |
89 |
Correct |
452 ms |
30564 KB |
Output is correct |