#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int sz[N];
int p[N];
set <int> out[N];
set <int> in[N];
set <int> who[N];
set <int> ver[N];
int get(int a) {
return (a == p[a] ? a : p[a] = get(p[a]));
}
ll ans = 0;
void join(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
ans -= (ll)sz[a] * (sz[a] - 1);
ans -= (ll)sz[b] * (sz[b] - 1);
if (in[a].size() + out[a].size() + who[a].size() + ver[a].size() > in[b].size() + out[b].size() + who[b].size() + ver[b].size()) swap(a, b);
in[a].erase(b);
out[a].erase(b);
in[b].erase(a);
out[b].erase(a);
ans += (ll)who[a].size() * sz[b];
ans += (ll)who[b].size() * sz[a];
for (int x : who[a]) {
if (who[b].count(x)) {
ans -= sz[a] + sz[b];
} else if (ver[b].count(x)) {
ans -= sz[a] + sz[b];
} else {
who[b].insert(x);
}
}
for (int x : ver[a]) {
if (who[b].count(x)) {
ans -= sz[a] + sz[b];
who[b].erase(x);
}
ver[b].insert(x);
}
vector <pair <int, int>> to_join;
for (int x : in[a]) {
if (out[b].count(x)) {
to_join.push_back({x, b});
}
in[b].insert(x);
out[x].erase(a);
out[x].insert(b);
}
for (int x : out[a]) {
if (in[b].count(x)) {
to_join.push_back({x, b});
}
out[b].insert(x);
in[x].erase(a);
in[x].insert(b);
}
who[a] = {};
ver[a] = {};
in[a] = {};
out[a] = {};
p[a] = b;
sz[b] += sz[a];
ans += (ll)sz[b] * (sz[b] - 1);
for (auto p : to_join) join(p.first, p.second);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
p[i] = i;
sz[i] = 1;
ver[i].insert(i);
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
int a = get(u);
int b = get(v);
if (a != b) {
out[a].insert(b);
in[b].insert(a);
if (!who[b].count(u)) {
who[b].insert(u);
ans += sz[b];
}
}
if (out[b].count(a)) {
join(a, b);
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19072 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
19072 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
15 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19232 KB |
Output is correct |
10 |
Correct |
15 ms |
19072 KB |
Output is correct |
11 |
Correct |
15 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
15 ms |
19072 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
15 ms |
19072 KB |
Output is correct |
18 |
Correct |
14 ms |
19072 KB |
Output is correct |
19 |
Correct |
15 ms |
19072 KB |
Output is correct |
20 |
Correct |
15 ms |
19200 KB |
Output is correct |
21 |
Correct |
16 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19072 KB |
Output is correct |
23 |
Correct |
15 ms |
19200 KB |
Output is correct |
24 |
Correct |
15 ms |
19200 KB |
Output is correct |
25 |
Correct |
15 ms |
19200 KB |
Output is correct |
26 |
Correct |
15 ms |
19200 KB |
Output is correct |
27 |
Correct |
15 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
15 ms |
19200 KB |
Output is correct |
30 |
Correct |
15 ms |
19072 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
19 ms |
19072 KB |
Output is correct |
33 |
Correct |
17 ms |
19200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19072 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
19072 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
15 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19232 KB |
Output is correct |
10 |
Correct |
15 ms |
19072 KB |
Output is correct |
11 |
Correct |
15 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
15 ms |
19072 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
15 ms |
19072 KB |
Output is correct |
18 |
Correct |
14 ms |
19072 KB |
Output is correct |
19 |
Correct |
15 ms |
19072 KB |
Output is correct |
20 |
Correct |
15 ms |
19200 KB |
Output is correct |
21 |
Correct |
16 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19072 KB |
Output is correct |
23 |
Correct |
15 ms |
19200 KB |
Output is correct |
24 |
Correct |
15 ms |
19200 KB |
Output is correct |
25 |
Correct |
15 ms |
19200 KB |
Output is correct |
26 |
Correct |
15 ms |
19200 KB |
Output is correct |
27 |
Correct |
15 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
15 ms |
19200 KB |
Output is correct |
30 |
Correct |
15 ms |
19072 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
19 ms |
19072 KB |
Output is correct |
33 |
Correct |
17 ms |
19200 KB |
Output is correct |
34 |
Correct |
20 ms |
19332 KB |
Output is correct |
35 |
Correct |
105 ms |
25080 KB |
Output is correct |
36 |
Correct |
133 ms |
27768 KB |
Output is correct |
37 |
Correct |
137 ms |
27768 KB |
Output is correct |
38 |
Correct |
130 ms |
27512 KB |
Output is correct |
39 |
Correct |
17 ms |
19328 KB |
Output is correct |
40 |
Correct |
21 ms |
19456 KB |
Output is correct |
41 |
Correct |
20 ms |
19456 KB |
Output is correct |
42 |
Correct |
17 ms |
19328 KB |
Output is correct |
43 |
Correct |
22 ms |
19456 KB |
Output is correct |
44 |
Correct |
20 ms |
19456 KB |
Output is correct |
45 |
Correct |
22 ms |
19328 KB |
Output is correct |
46 |
Correct |
20 ms |
19328 KB |
Output is correct |
47 |
Correct |
20 ms |
19456 KB |
Output is correct |
48 |
Correct |
20 ms |
19456 KB |
Output is correct |
49 |
Correct |
27 ms |
20172 KB |
Output is correct |
50 |
Correct |
146 ms |
27896 KB |
Output is correct |
51 |
Correct |
27 ms |
19712 KB |
Output is correct |
52 |
Correct |
126 ms |
26484 KB |
Output is correct |
53 |
Correct |
36 ms |
20216 KB |
Output is correct |
54 |
Correct |
124 ms |
27064 KB |
Output is correct |
55 |
Correct |
23 ms |
19840 KB |
Output is correct |
56 |
Correct |
22 ms |
19840 KB |
Output is correct |
57 |
Correct |
31 ms |
20088 KB |
Output is correct |
58 |
Correct |
22 ms |
20096 KB |
Output is correct |
59 |
Correct |
24 ms |
19328 KB |
Output is correct |
60 |
Correct |
97 ms |
24316 KB |
Output is correct |
61 |
Correct |
23 ms |
19456 KB |
Output is correct |
62 |
Correct |
126 ms |
27256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19072 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
19072 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
15 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
16 ms |
19200 KB |
Output is correct |
9 |
Correct |
15 ms |
19232 KB |
Output is correct |
10 |
Correct |
15 ms |
19072 KB |
Output is correct |
11 |
Correct |
15 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
15 ms |
19072 KB |
Output is correct |
15 |
Correct |
16 ms |
19328 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
15 ms |
19072 KB |
Output is correct |
18 |
Correct |
14 ms |
19072 KB |
Output is correct |
19 |
Correct |
15 ms |
19072 KB |
Output is correct |
20 |
Correct |
15 ms |
19200 KB |
Output is correct |
21 |
Correct |
16 ms |
19200 KB |
Output is correct |
22 |
Correct |
15 ms |
19072 KB |
Output is correct |
23 |
Correct |
15 ms |
19200 KB |
Output is correct |
24 |
Correct |
15 ms |
19200 KB |
Output is correct |
25 |
Correct |
15 ms |
19200 KB |
Output is correct |
26 |
Correct |
15 ms |
19200 KB |
Output is correct |
27 |
Correct |
15 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
15 ms |
19200 KB |
Output is correct |
30 |
Correct |
15 ms |
19072 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
19 ms |
19072 KB |
Output is correct |
33 |
Correct |
17 ms |
19200 KB |
Output is correct |
34 |
Correct |
20 ms |
19332 KB |
Output is correct |
35 |
Correct |
105 ms |
25080 KB |
Output is correct |
36 |
Correct |
133 ms |
27768 KB |
Output is correct |
37 |
Correct |
137 ms |
27768 KB |
Output is correct |
38 |
Correct |
130 ms |
27512 KB |
Output is correct |
39 |
Correct |
17 ms |
19328 KB |
Output is correct |
40 |
Correct |
21 ms |
19456 KB |
Output is correct |
41 |
Correct |
20 ms |
19456 KB |
Output is correct |
42 |
Correct |
17 ms |
19328 KB |
Output is correct |
43 |
Correct |
22 ms |
19456 KB |
Output is correct |
44 |
Correct |
20 ms |
19456 KB |
Output is correct |
45 |
Correct |
22 ms |
19328 KB |
Output is correct |
46 |
Correct |
20 ms |
19328 KB |
Output is correct |
47 |
Correct |
20 ms |
19456 KB |
Output is correct |
48 |
Correct |
20 ms |
19456 KB |
Output is correct |
49 |
Correct |
27 ms |
20172 KB |
Output is correct |
50 |
Correct |
146 ms |
27896 KB |
Output is correct |
51 |
Correct |
27 ms |
19712 KB |
Output is correct |
52 |
Correct |
126 ms |
26484 KB |
Output is correct |
53 |
Correct |
36 ms |
20216 KB |
Output is correct |
54 |
Correct |
124 ms |
27064 KB |
Output is correct |
55 |
Correct |
23 ms |
19840 KB |
Output is correct |
56 |
Correct |
22 ms |
19840 KB |
Output is correct |
57 |
Correct |
31 ms |
20088 KB |
Output is correct |
58 |
Correct |
22 ms |
20096 KB |
Output is correct |
59 |
Correct |
24 ms |
19328 KB |
Output is correct |
60 |
Correct |
97 ms |
24316 KB |
Output is correct |
61 |
Correct |
23 ms |
19456 KB |
Output is correct |
62 |
Correct |
126 ms |
27256 KB |
Output is correct |
63 |
Correct |
580 ms |
72440 KB |
Output is correct |
64 |
Correct |
578 ms |
72312 KB |
Output is correct |
65 |
Correct |
587 ms |
72440 KB |
Output is correct |
66 |
Correct |
253 ms |
29048 KB |
Output is correct |
67 |
Correct |
518 ms |
35320 KB |
Output is correct |
68 |
Correct |
258 ms |
29060 KB |
Output is correct |
69 |
Correct |
582 ms |
36088 KB |
Output is correct |
70 |
Correct |
266 ms |
29048 KB |
Output is correct |
71 |
Correct |
266 ms |
29048 KB |
Output is correct |
72 |
Correct |
553 ms |
35448 KB |
Output is correct |
73 |
Correct |
562 ms |
35576 KB |
Output is correct |
74 |
Correct |
1275 ms |
47212 KB |
Output is correct |
75 |
Correct |
830 ms |
41848 KB |
Output is correct |
76 |
Correct |
995 ms |
46584 KB |
Output is correct |
77 |
Correct |
1028 ms |
46940 KB |
Output is correct |
78 |
Correct |
274 ms |
34808 KB |
Output is correct |
79 |
Correct |
494 ms |
38264 KB |
Output is correct |
80 |
Correct |
313 ms |
34808 KB |
Output is correct |
81 |
Correct |
526 ms |
38136 KB |
Output is correct |
82 |
Correct |
1000 ms |
56484 KB |
Output is correct |
83 |
Correct |
1004 ms |
56440 KB |
Output is correct |
84 |
Correct |
939 ms |
65888 KB |
Output is correct |
85 |
Correct |
937 ms |
65912 KB |
Output is correct |
86 |
Correct |
337 ms |
29816 KB |
Output is correct |
87 |
Correct |
377 ms |
32344 KB |
Output is correct |
88 |
Correct |
580 ms |
35704 KB |
Output is correct |
89 |
Correct |
979 ms |
46348 KB |
Output is correct |