/**
* author: NimaAryan
* created: 2024-08-15 13:30:23
**/
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
constexpr int N = 1E5;
i64 ans = 0;
vector<set<int>> S(N); // all vertices in A
vector<set<int>> T(N); // {x->a | a \in A}
vector<set<int>> G(N); // {B | a \in A, a->B}
vector<int> par(N, -1);
queue<pair<int, int>> to_merge;
int find(int x) {
return par[x] == -1 ? x : par[x] = find(par[x]);
}
int size(int A) {
return S[A].size() + T[A].size() + G[A].size();
}
void unite(int A, int B) {
A = find(A), B = find(B);
if (A == B) {
return;
}
if (size(A) < size(B)) {
swap(A, B);
}
ans -= 1LL * S[A].size() * (S[A].size() - 1);
ans -= 1LL * S[A].size() * T[A].size();
ans -= 1LL * S[B].size() * (S[B].size() - 1);
ans -= 1LL * S[B].size() * T[B].size();
for (int X : G[B]) {
if (G[X].count(A)) {
to_merge.emplace(A, X);
}
}
T[A].insert(T[B].begin(), T[B].end());
for (int b : S[B]) {
T[A].erase(b);
}
for (int x : T[B]) {
int X = find(x);
if (X == A) {
T[A].erase(x);
} else if (G[A].count(X)) {
to_merge.emplace(A, X);
}
if (X != A) {
G[X].erase(B);
G[X].insert(A);
}
}
S[A].insert(S[B].begin(), S[B].end());
G[A].insert(G[B].begin(), G[B].end());
G[A].erase(B);
G[A].erase(A);
par[B] = A;
ans += 1LL * S[A].size() * (S[A].size() - 1);
ans += 1LL * S[A].size() * T[A].size();
S[B].clear();
T[B].clear();
G[B].clear();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int x = 0; x < n; ++x) {
S[x] = {x};
}
while (m--) {
int a, b;
cin >> a >> b;
--a, --b;
int A = find(a), B = find(b);
if (A != B) {
if (G[B].count(A)) {
unite(A, B);
} else if (!T[B].count(a)) {
ans += S[B].size();
T[B].insert(a);
G[A].insert(B);
}
while (!to_merge.empty()) {
auto [X, Y] = to_merge.front();
to_merge.pop();
unite(X, Y);
}
}
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14936 KB |
Output is correct |
4 |
Correct |
4 ms |
14788 KB |
Output is correct |
5 |
Correct |
4 ms |
14844 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14788 KB |
Output is correct |
21 |
Correct |
5 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14884 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
6 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14936 KB |
Output is correct |
4 |
Correct |
4 ms |
14788 KB |
Output is correct |
5 |
Correct |
4 ms |
14844 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14788 KB |
Output is correct |
21 |
Correct |
5 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14884 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
6 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14832 KB |
Output is correct |
34 |
Correct |
6 ms |
14940 KB |
Output is correct |
35 |
Correct |
48 ms |
17780 KB |
Output is correct |
36 |
Correct |
64 ms |
19536 KB |
Output is correct |
37 |
Correct |
73 ms |
19624 KB |
Output is correct |
38 |
Correct |
61 ms |
19280 KB |
Output is correct |
39 |
Correct |
5 ms |
14936 KB |
Output is correct |
40 |
Correct |
7 ms |
14936 KB |
Output is correct |
41 |
Correct |
6 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14940 KB |
Output is correct |
43 |
Correct |
6 ms |
14940 KB |
Output is correct |
44 |
Correct |
6 ms |
14940 KB |
Output is correct |
45 |
Correct |
5 ms |
14940 KB |
Output is correct |
46 |
Correct |
5 ms |
14940 KB |
Output is correct |
47 |
Correct |
7 ms |
14936 KB |
Output is correct |
48 |
Correct |
6 ms |
14940 KB |
Output is correct |
49 |
Correct |
10 ms |
15452 KB |
Output is correct |
50 |
Correct |
65 ms |
19544 KB |
Output is correct |
51 |
Correct |
7 ms |
15196 KB |
Output is correct |
52 |
Correct |
57 ms |
18476 KB |
Output is correct |
53 |
Correct |
10 ms |
15448 KB |
Output is correct |
54 |
Correct |
62 ms |
19108 KB |
Output is correct |
55 |
Correct |
8 ms |
15196 KB |
Output is correct |
56 |
Correct |
7 ms |
15196 KB |
Output is correct |
57 |
Correct |
7 ms |
15444 KB |
Output is correct |
58 |
Correct |
7 ms |
15196 KB |
Output is correct |
59 |
Correct |
6 ms |
15080 KB |
Output is correct |
60 |
Correct |
47 ms |
17388 KB |
Output is correct |
61 |
Correct |
7 ms |
14940 KB |
Output is correct |
62 |
Correct |
61 ms |
19348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14936 KB |
Output is correct |
4 |
Correct |
4 ms |
14788 KB |
Output is correct |
5 |
Correct |
4 ms |
14844 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
4 ms |
14940 KB |
Output is correct |
11 |
Correct |
4 ms |
14868 KB |
Output is correct |
12 |
Correct |
4 ms |
14936 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
4 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
4 ms |
14940 KB |
Output is correct |
19 |
Correct |
4 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14788 KB |
Output is correct |
21 |
Correct |
5 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14940 KB |
Output is correct |
23 |
Correct |
4 ms |
14884 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14936 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
14940 KB |
Output is correct |
28 |
Correct |
6 ms |
14940 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
14940 KB |
Output is correct |
33 |
Correct |
4 ms |
14832 KB |
Output is correct |
34 |
Correct |
6 ms |
14940 KB |
Output is correct |
35 |
Correct |
48 ms |
17780 KB |
Output is correct |
36 |
Correct |
64 ms |
19536 KB |
Output is correct |
37 |
Correct |
73 ms |
19624 KB |
Output is correct |
38 |
Correct |
61 ms |
19280 KB |
Output is correct |
39 |
Correct |
5 ms |
14936 KB |
Output is correct |
40 |
Correct |
7 ms |
14936 KB |
Output is correct |
41 |
Correct |
6 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14940 KB |
Output is correct |
43 |
Correct |
6 ms |
14940 KB |
Output is correct |
44 |
Correct |
6 ms |
14940 KB |
Output is correct |
45 |
Correct |
5 ms |
14940 KB |
Output is correct |
46 |
Correct |
5 ms |
14940 KB |
Output is correct |
47 |
Correct |
7 ms |
14936 KB |
Output is correct |
48 |
Correct |
6 ms |
14940 KB |
Output is correct |
49 |
Correct |
10 ms |
15452 KB |
Output is correct |
50 |
Correct |
65 ms |
19544 KB |
Output is correct |
51 |
Correct |
7 ms |
15196 KB |
Output is correct |
52 |
Correct |
57 ms |
18476 KB |
Output is correct |
53 |
Correct |
10 ms |
15448 KB |
Output is correct |
54 |
Correct |
62 ms |
19108 KB |
Output is correct |
55 |
Correct |
8 ms |
15196 KB |
Output is correct |
56 |
Correct |
7 ms |
15196 KB |
Output is correct |
57 |
Correct |
7 ms |
15444 KB |
Output is correct |
58 |
Correct |
7 ms |
15196 KB |
Output is correct |
59 |
Correct |
6 ms |
15080 KB |
Output is correct |
60 |
Correct |
47 ms |
17388 KB |
Output is correct |
61 |
Correct |
7 ms |
14940 KB |
Output is correct |
62 |
Correct |
61 ms |
19348 KB |
Output is correct |
63 |
Correct |
199 ms |
49748 KB |
Output is correct |
64 |
Correct |
198 ms |
49668 KB |
Output is correct |
65 |
Correct |
196 ms |
49740 KB |
Output is correct |
66 |
Correct |
92 ms |
21820 KB |
Output is correct |
67 |
Correct |
182 ms |
25564 KB |
Output is correct |
68 |
Correct |
90 ms |
21504 KB |
Output is correct |
69 |
Correct |
196 ms |
26320 KB |
Output is correct |
70 |
Correct |
90 ms |
21716 KB |
Output is correct |
71 |
Correct |
113 ms |
21588 KB |
Output is correct |
72 |
Correct |
173 ms |
25684 KB |
Output is correct |
73 |
Correct |
183 ms |
25680 KB |
Output is correct |
74 |
Correct |
429 ms |
32848 KB |
Output is correct |
75 |
Correct |
268 ms |
29640 KB |
Output is correct |
76 |
Correct |
307 ms |
32460 KB |
Output is correct |
77 |
Correct |
308 ms |
32436 KB |
Output is correct |
78 |
Correct |
109 ms |
25496 KB |
Output is correct |
79 |
Correct |
191 ms |
27216 KB |
Output is correct |
80 |
Correct |
111 ms |
25680 KB |
Output is correct |
81 |
Correct |
219 ms |
27216 KB |
Output is correct |
82 |
Correct |
363 ms |
40016 KB |
Output is correct |
83 |
Correct |
370 ms |
39760 KB |
Output is correct |
84 |
Correct |
315 ms |
39608 KB |
Output is correct |
85 |
Correct |
301 ms |
39508 KB |
Output is correct |
86 |
Correct |
157 ms |
22480 KB |
Output is correct |
87 |
Correct |
173 ms |
23668 KB |
Output is correct |
88 |
Correct |
187 ms |
25684 KB |
Output is correct |
89 |
Correct |
304 ms |
32336 KB |
Output is correct |