#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 1e5 + 5;
long long ans;
set<int> r[N];
int p[N], sz[N];
set<pair<int, int>> in[N], out[N];
int get(int x) {
return p[x] = (p[x] == x ? x : get(p[x]));
}
void merge(int x, int y) {
x = get(x), y = get(y);
if (x == y) {
return;
}
auto f = in[x].lower_bound(make_pair(y, 0));
while (f != in[x].end() && f->first == y) {
ans -= sz[x];
r[x].erase(f->second);
out[y].erase(make_pair(x, f->second));
f = in[x].erase(f);
}
f = in[y].lower_bound(make_pair(x, 0));
while (f != in[y].end() && f->first == x) {
ans -= sz[y];
r[y].erase(f->second);
out[x].erase(make_pair(y, f->second));
f = in[y].erase(f);
}
if (in[x].size() + out[x].size() < in[y].size() + out[y].size()) {
swap(x, y);
}
ans -= (long long) in[x].size() * sz[x];
ans -= (long long) in[y].size() * sz[y];
ans -= (long long) sz[x] * (sz[x] - 1);
ans -= (long long) sz[y] * (sz[y] - 1);
vector<int> to_merge;
for (auto [i, j] : in[y]) {
out[i].erase({y, j});
out[i].emplace(x, j);
if (!in[x].count({i, j})) {
in[x].emplace(i, j);
r[x].insert(j);
}
f = out[x].lower_bound(make_pair(i, 0));
if (f != out[x].end() && f->first == i) {
to_merge.push_back(i);
}
}
p[y] = x;
sz[x] += sz[y];
ans += (long long) in[x].size() * sz[x];
ans += (long long) sz[x] * (sz[x] - 1);
for (auto [i, j] : out[y]) {
in[i].erase(make_pair(y, j));
in[i].emplace(x, j);
out[x].emplace(i, j);
f = in[x].lower_bound(make_pair(i, 0));
if (f != in[x].end() && f->first == i) {
to_merge.push_back(i);
}
}
for (int i : to_merge) {
merge(x, i);
}
}
void add(int x, int y) {
int px = get(x), py = get(y);
if (px == py || r[py].count(x)) {
return;
}
ans += sz[py];
r[py].insert(x);
in[py].emplace(px, x);
out[px].emplace(py, x);
auto f = in[px].lower_bound(make_pair(py, 0));
if (f != in[px].end() && f->first == py) {
merge(px, py);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
iota(p + 1, p + 1 + n, 1);
fill(sz + 1, sz + 1 + n, 1);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
add(x, y);
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
15196 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
4 ms |
15196 KB |
Output is correct |
10 |
Correct |
3 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15336 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
4 ms |
15192 KB |
Output is correct |
20 |
Correct |
3 ms |
15348 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
4 ms |
15328 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
4 ms |
15196 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
4 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
15196 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
4 ms |
15196 KB |
Output is correct |
10 |
Correct |
3 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15336 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
4 ms |
15192 KB |
Output is correct |
20 |
Correct |
3 ms |
15348 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
4 ms |
15328 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
4 ms |
15196 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
4 ms |
15196 KB |
Output is correct |
34 |
Correct |
5 ms |
15452 KB |
Output is correct |
35 |
Correct |
66 ms |
21328 KB |
Output is correct |
36 |
Correct |
86 ms |
24084 KB |
Output is correct |
37 |
Correct |
91 ms |
24148 KB |
Output is correct |
38 |
Correct |
77 ms |
23696 KB |
Output is correct |
39 |
Correct |
4 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15452 KB |
Output is correct |
41 |
Correct |
5 ms |
15352 KB |
Output is correct |
42 |
Correct |
5 ms |
15288 KB |
Output is correct |
43 |
Correct |
6 ms |
15448 KB |
Output is correct |
44 |
Correct |
6 ms |
15520 KB |
Output is correct |
45 |
Correct |
5 ms |
15196 KB |
Output is correct |
46 |
Correct |
6 ms |
15344 KB |
Output is correct |
47 |
Correct |
5 ms |
15452 KB |
Output is correct |
48 |
Correct |
6 ms |
15448 KB |
Output is correct |
49 |
Correct |
11 ms |
16728 KB |
Output is correct |
50 |
Correct |
87 ms |
24224 KB |
Output is correct |
51 |
Correct |
8 ms |
15708 KB |
Output is correct |
52 |
Correct |
69 ms |
22356 KB |
Output is correct |
53 |
Correct |
10 ms |
16216 KB |
Output is correct |
54 |
Correct |
75 ms |
23380 KB |
Output is correct |
55 |
Correct |
7 ms |
15964 KB |
Output is correct |
56 |
Correct |
7 ms |
15856 KB |
Output is correct |
57 |
Correct |
7 ms |
16476 KB |
Output is correct |
58 |
Correct |
7 ms |
16136 KB |
Output is correct |
59 |
Correct |
4 ms |
15448 KB |
Output is correct |
60 |
Correct |
57 ms |
20308 KB |
Output is correct |
61 |
Correct |
6 ms |
15452 KB |
Output is correct |
62 |
Correct |
75 ms |
23484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15196 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
3 |
Correct |
3 ms |
15196 KB |
Output is correct |
4 |
Correct |
3 ms |
15196 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
15196 KB |
Output is correct |
7 |
Correct |
4 ms |
15196 KB |
Output is correct |
8 |
Correct |
3 ms |
15192 KB |
Output is correct |
9 |
Correct |
4 ms |
15196 KB |
Output is correct |
10 |
Correct |
3 ms |
15192 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
4 ms |
15196 KB |
Output is correct |
14 |
Correct |
3 ms |
15336 KB |
Output is correct |
15 |
Correct |
3 ms |
15196 KB |
Output is correct |
16 |
Correct |
3 ms |
15196 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
4 ms |
15192 KB |
Output is correct |
20 |
Correct |
3 ms |
15348 KB |
Output is correct |
21 |
Correct |
5 ms |
15196 KB |
Output is correct |
22 |
Correct |
3 ms |
15196 KB |
Output is correct |
23 |
Correct |
3 ms |
15196 KB |
Output is correct |
24 |
Correct |
3 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15196 KB |
Output is correct |
26 |
Correct |
3 ms |
15196 KB |
Output is correct |
27 |
Correct |
4 ms |
15328 KB |
Output is correct |
28 |
Correct |
3 ms |
15196 KB |
Output is correct |
29 |
Correct |
3 ms |
15196 KB |
Output is correct |
30 |
Correct |
3 ms |
15196 KB |
Output is correct |
31 |
Correct |
4 ms |
15196 KB |
Output is correct |
32 |
Correct |
3 ms |
15196 KB |
Output is correct |
33 |
Correct |
4 ms |
15196 KB |
Output is correct |
34 |
Correct |
5 ms |
15452 KB |
Output is correct |
35 |
Correct |
66 ms |
21328 KB |
Output is correct |
36 |
Correct |
86 ms |
24084 KB |
Output is correct |
37 |
Correct |
91 ms |
24148 KB |
Output is correct |
38 |
Correct |
77 ms |
23696 KB |
Output is correct |
39 |
Correct |
4 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15452 KB |
Output is correct |
41 |
Correct |
5 ms |
15352 KB |
Output is correct |
42 |
Correct |
5 ms |
15288 KB |
Output is correct |
43 |
Correct |
6 ms |
15448 KB |
Output is correct |
44 |
Correct |
6 ms |
15520 KB |
Output is correct |
45 |
Correct |
5 ms |
15196 KB |
Output is correct |
46 |
Correct |
6 ms |
15344 KB |
Output is correct |
47 |
Correct |
5 ms |
15452 KB |
Output is correct |
48 |
Correct |
6 ms |
15448 KB |
Output is correct |
49 |
Correct |
11 ms |
16728 KB |
Output is correct |
50 |
Correct |
87 ms |
24224 KB |
Output is correct |
51 |
Correct |
8 ms |
15708 KB |
Output is correct |
52 |
Correct |
69 ms |
22356 KB |
Output is correct |
53 |
Correct |
10 ms |
16216 KB |
Output is correct |
54 |
Correct |
75 ms |
23380 KB |
Output is correct |
55 |
Correct |
7 ms |
15964 KB |
Output is correct |
56 |
Correct |
7 ms |
15856 KB |
Output is correct |
57 |
Correct |
7 ms |
16476 KB |
Output is correct |
58 |
Correct |
7 ms |
16136 KB |
Output is correct |
59 |
Correct |
4 ms |
15448 KB |
Output is correct |
60 |
Correct |
57 ms |
20308 KB |
Output is correct |
61 |
Correct |
6 ms |
15452 KB |
Output is correct |
62 |
Correct |
75 ms |
23484 KB |
Output is correct |
63 |
Correct |
414 ms |
62840 KB |
Output is correct |
64 |
Correct |
376 ms |
63056 KB |
Output is correct |
65 |
Correct |
366 ms |
62804 KB |
Output is correct |
66 |
Correct |
89 ms |
19600 KB |
Output is correct |
67 |
Correct |
137 ms |
26452 KB |
Output is correct |
68 |
Correct |
76 ms |
19560 KB |
Output is correct |
69 |
Correct |
210 ms |
26708 KB |
Output is correct |
70 |
Correct |
96 ms |
19736 KB |
Output is correct |
71 |
Correct |
91 ms |
19560 KB |
Output is correct |
72 |
Correct |
166 ms |
26872 KB |
Output is correct |
73 |
Correct |
175 ms |
26960 KB |
Output is correct |
74 |
Correct |
586 ms |
54832 KB |
Output is correct |
75 |
Correct |
369 ms |
33672 KB |
Output is correct |
76 |
Correct |
404 ms |
43600 KB |
Output is correct |
77 |
Correct |
421 ms |
43924 KB |
Output is correct |
78 |
Correct |
112 ms |
27836 KB |
Output is correct |
79 |
Correct |
205 ms |
31736 KB |
Output is correct |
80 |
Correct |
145 ms |
26704 KB |
Output is correct |
81 |
Correct |
222 ms |
30028 KB |
Output is correct |
82 |
Correct |
442 ms |
47884 KB |
Output is correct |
83 |
Correct |
452 ms |
47952 KB |
Output is correct |
84 |
Correct |
426 ms |
64808 KB |
Output is correct |
85 |
Correct |
430 ms |
64920 KB |
Output is correct |
86 |
Correct |
71 ms |
18768 KB |
Output is correct |
87 |
Correct |
107 ms |
21076 KB |
Output is correct |
88 |
Correct |
200 ms |
26964 KB |
Output is correct |
89 |
Correct |
419 ms |
41332 KB |
Output is correct |