#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int cmp[100001];
ll sz[100001], ans = 0;
set<int> child[100001], graph[100001], rgraph[100001];
queue<pair<int, int>> to_merge;
void insert_weak_connection(int A, int B) {
graph[A].insert(B);
rgraph[B].insert(A);
// If there's A new strong connection between A's and B's components, merge
// them
if (graph[B].count(A)) to_merge.push({A, B});
}
int dsu_size(int A) { return child[A].size() + graph[A].size() + rgraph[A].size(); }
int find(int A) { return (A == cmp[A] ? A : cmp[A] = find(cmp[A])); }
void onion(int A, int B) {
if (A == B) return;
// Merge the smaller component into the larger
if (dsu_size(A) < dsu_size(B)) swap(A, B);
// Add new contribution
ans += sz[B] * child[A].size() + sz[A] * child[B].size();
// DSU stuff
cmp[B] = A;
sz[A] += sz[B];
// Merge children of B into A
for (int i : child[B]) {
if (child[A].count(i)) ans -= sz[A];
else child[A].insert(i);
}
// Erase the connections
graph[A].erase(B), rgraph[A].erase(B);
graph[B].erase(A), rgraph[B].erase(A);
// Merge the weak connections to other components
for (int i : graph[B]) {
rgraph[i].erase(B);
insert_weak_connection(A, i);
}
for (int i : rgraph[B]) {
graph[i].erase(B);
insert_weak_connection(i, A);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cmp[i] = i;
sz[i] = 1;
child[i].insert(i);
}
while (m--) {
int A, B;
cin >> A >> B;
B = find(B);
// If A isn't in B's component and doesn't already follow someone in B's
// component...
if (find(A) != B && !child[B].count(A)) {
// We insert A as A follower of B and add sz[find(B)] to the answer
child[B].insert(A);
ans += sz[B];
A = find(A);
// Add connections between components
insert_weak_connection(A, B);
// We may have to merge multiple components for each new event
while (to_merge.size()) {
tie(A, B) = to_merge.front();
to_merge.pop();
onion(find(A), find(B));
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15004 KB |
Output is correct |
4 |
Correct |
3 ms |
14932 KB |
Output is correct |
5 |
Correct |
3 ms |
14932 KB |
Output is correct |
6 |
Correct |
3 ms |
14932 KB |
Output is correct |
7 |
Correct |
3 ms |
14932 KB |
Output is correct |
8 |
Correct |
4 ms |
14932 KB |
Output is correct |
9 |
Correct |
3 ms |
14932 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14996 KB |
Output is correct |
12 |
Correct |
3 ms |
14932 KB |
Output is correct |
13 |
Correct |
3 ms |
14932 KB |
Output is correct |
14 |
Correct |
3 ms |
15096 KB |
Output is correct |
15 |
Correct |
3 ms |
14932 KB |
Output is correct |
16 |
Correct |
3 ms |
14932 KB |
Output is correct |
17 |
Correct |
3 ms |
14932 KB |
Output is correct |
18 |
Correct |
3 ms |
14932 KB |
Output is correct |
19 |
Correct |
3 ms |
15004 KB |
Output is correct |
20 |
Correct |
3 ms |
14932 KB |
Output is correct |
21 |
Correct |
4 ms |
14932 KB |
Output is correct |
22 |
Correct |
3 ms |
14932 KB |
Output is correct |
23 |
Correct |
3 ms |
14932 KB |
Output is correct |
24 |
Correct |
4 ms |
14932 KB |
Output is correct |
25 |
Correct |
3 ms |
14932 KB |
Output is correct |
26 |
Correct |
3 ms |
14932 KB |
Output is correct |
27 |
Correct |
3 ms |
14964 KB |
Output is correct |
28 |
Correct |
3 ms |
14932 KB |
Output is correct |
29 |
Correct |
3 ms |
15000 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14932 KB |
Output is correct |
32 |
Correct |
3 ms |
14932 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15004 KB |
Output is correct |
4 |
Correct |
3 ms |
14932 KB |
Output is correct |
5 |
Correct |
3 ms |
14932 KB |
Output is correct |
6 |
Correct |
3 ms |
14932 KB |
Output is correct |
7 |
Correct |
3 ms |
14932 KB |
Output is correct |
8 |
Correct |
4 ms |
14932 KB |
Output is correct |
9 |
Correct |
3 ms |
14932 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14996 KB |
Output is correct |
12 |
Correct |
3 ms |
14932 KB |
Output is correct |
13 |
Correct |
3 ms |
14932 KB |
Output is correct |
14 |
Correct |
3 ms |
15096 KB |
Output is correct |
15 |
Correct |
3 ms |
14932 KB |
Output is correct |
16 |
Correct |
3 ms |
14932 KB |
Output is correct |
17 |
Correct |
3 ms |
14932 KB |
Output is correct |
18 |
Correct |
3 ms |
14932 KB |
Output is correct |
19 |
Correct |
3 ms |
15004 KB |
Output is correct |
20 |
Correct |
3 ms |
14932 KB |
Output is correct |
21 |
Correct |
4 ms |
14932 KB |
Output is correct |
22 |
Correct |
3 ms |
14932 KB |
Output is correct |
23 |
Correct |
3 ms |
14932 KB |
Output is correct |
24 |
Correct |
4 ms |
14932 KB |
Output is correct |
25 |
Correct |
3 ms |
14932 KB |
Output is correct |
26 |
Correct |
3 ms |
14932 KB |
Output is correct |
27 |
Correct |
3 ms |
14964 KB |
Output is correct |
28 |
Correct |
3 ms |
14932 KB |
Output is correct |
29 |
Correct |
3 ms |
15000 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14932 KB |
Output is correct |
32 |
Correct |
3 ms |
14932 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
34 |
Correct |
5 ms |
15012 KB |
Output is correct |
35 |
Correct |
56 ms |
20820 KB |
Output is correct |
36 |
Correct |
82 ms |
23540 KB |
Output is correct |
37 |
Correct |
78 ms |
23628 KB |
Output is correct |
38 |
Correct |
71 ms |
23124 KB |
Output is correct |
39 |
Correct |
5 ms |
15188 KB |
Output is correct |
40 |
Correct |
6 ms |
15700 KB |
Output is correct |
41 |
Correct |
6 ms |
15700 KB |
Output is correct |
42 |
Correct |
5 ms |
15188 KB |
Output is correct |
43 |
Correct |
6 ms |
15188 KB |
Output is correct |
44 |
Correct |
6 ms |
15188 KB |
Output is correct |
45 |
Correct |
6 ms |
15188 KB |
Output is correct |
46 |
Correct |
5 ms |
15188 KB |
Output is correct |
47 |
Correct |
6 ms |
15516 KB |
Output is correct |
48 |
Correct |
6 ms |
15444 KB |
Output is correct |
49 |
Correct |
12 ms |
16212 KB |
Output is correct |
50 |
Correct |
76 ms |
23860 KB |
Output is correct |
51 |
Correct |
8 ms |
15444 KB |
Output is correct |
52 |
Correct |
66 ms |
22092 KB |
Output is correct |
53 |
Correct |
11 ms |
15956 KB |
Output is correct |
54 |
Correct |
68 ms |
22752 KB |
Output is correct |
55 |
Correct |
7 ms |
15700 KB |
Output is correct |
56 |
Correct |
7 ms |
15752 KB |
Output is correct |
57 |
Correct |
6 ms |
15864 KB |
Output is correct |
58 |
Correct |
6 ms |
15544 KB |
Output is correct |
59 |
Correct |
5 ms |
15864 KB |
Output is correct |
60 |
Correct |
49 ms |
20556 KB |
Output is correct |
61 |
Correct |
6 ms |
15444 KB |
Output is correct |
62 |
Correct |
72 ms |
23028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
3 ms |
15004 KB |
Output is correct |
4 |
Correct |
3 ms |
14932 KB |
Output is correct |
5 |
Correct |
3 ms |
14932 KB |
Output is correct |
6 |
Correct |
3 ms |
14932 KB |
Output is correct |
7 |
Correct |
3 ms |
14932 KB |
Output is correct |
8 |
Correct |
4 ms |
14932 KB |
Output is correct |
9 |
Correct |
3 ms |
14932 KB |
Output is correct |
10 |
Correct |
3 ms |
15096 KB |
Output is correct |
11 |
Correct |
3 ms |
14996 KB |
Output is correct |
12 |
Correct |
3 ms |
14932 KB |
Output is correct |
13 |
Correct |
3 ms |
14932 KB |
Output is correct |
14 |
Correct |
3 ms |
15096 KB |
Output is correct |
15 |
Correct |
3 ms |
14932 KB |
Output is correct |
16 |
Correct |
3 ms |
14932 KB |
Output is correct |
17 |
Correct |
3 ms |
14932 KB |
Output is correct |
18 |
Correct |
3 ms |
14932 KB |
Output is correct |
19 |
Correct |
3 ms |
15004 KB |
Output is correct |
20 |
Correct |
3 ms |
14932 KB |
Output is correct |
21 |
Correct |
4 ms |
14932 KB |
Output is correct |
22 |
Correct |
3 ms |
14932 KB |
Output is correct |
23 |
Correct |
3 ms |
14932 KB |
Output is correct |
24 |
Correct |
4 ms |
14932 KB |
Output is correct |
25 |
Correct |
3 ms |
14932 KB |
Output is correct |
26 |
Correct |
3 ms |
14932 KB |
Output is correct |
27 |
Correct |
3 ms |
14964 KB |
Output is correct |
28 |
Correct |
3 ms |
14932 KB |
Output is correct |
29 |
Correct |
3 ms |
15000 KB |
Output is correct |
30 |
Correct |
3 ms |
14932 KB |
Output is correct |
31 |
Correct |
3 ms |
14932 KB |
Output is correct |
32 |
Correct |
3 ms |
14932 KB |
Output is correct |
33 |
Correct |
4 ms |
14932 KB |
Output is correct |
34 |
Correct |
5 ms |
15012 KB |
Output is correct |
35 |
Correct |
56 ms |
20820 KB |
Output is correct |
36 |
Correct |
82 ms |
23540 KB |
Output is correct |
37 |
Correct |
78 ms |
23628 KB |
Output is correct |
38 |
Correct |
71 ms |
23124 KB |
Output is correct |
39 |
Correct |
5 ms |
15188 KB |
Output is correct |
40 |
Correct |
6 ms |
15700 KB |
Output is correct |
41 |
Correct |
6 ms |
15700 KB |
Output is correct |
42 |
Correct |
5 ms |
15188 KB |
Output is correct |
43 |
Correct |
6 ms |
15188 KB |
Output is correct |
44 |
Correct |
6 ms |
15188 KB |
Output is correct |
45 |
Correct |
6 ms |
15188 KB |
Output is correct |
46 |
Correct |
5 ms |
15188 KB |
Output is correct |
47 |
Correct |
6 ms |
15516 KB |
Output is correct |
48 |
Correct |
6 ms |
15444 KB |
Output is correct |
49 |
Correct |
12 ms |
16212 KB |
Output is correct |
50 |
Correct |
76 ms |
23860 KB |
Output is correct |
51 |
Correct |
8 ms |
15444 KB |
Output is correct |
52 |
Correct |
66 ms |
22092 KB |
Output is correct |
53 |
Correct |
11 ms |
15956 KB |
Output is correct |
54 |
Correct |
68 ms |
22752 KB |
Output is correct |
55 |
Correct |
7 ms |
15700 KB |
Output is correct |
56 |
Correct |
7 ms |
15752 KB |
Output is correct |
57 |
Correct |
6 ms |
15864 KB |
Output is correct |
58 |
Correct |
6 ms |
15544 KB |
Output is correct |
59 |
Correct |
5 ms |
15864 KB |
Output is correct |
60 |
Correct |
49 ms |
20556 KB |
Output is correct |
61 |
Correct |
6 ms |
15444 KB |
Output is correct |
62 |
Correct |
72 ms |
23028 KB |
Output is correct |
63 |
Correct |
319 ms |
68036 KB |
Output is correct |
64 |
Correct |
334 ms |
67916 KB |
Output is correct |
65 |
Correct |
343 ms |
67916 KB |
Output is correct |
66 |
Correct |
145 ms |
34124 KB |
Output is correct |
67 |
Correct |
293 ms |
61124 KB |
Output is correct |
68 |
Correct |
148 ms |
34124 KB |
Output is correct |
69 |
Correct |
263 ms |
35148 KB |
Output is correct |
70 |
Correct |
171 ms |
34124 KB |
Output is correct |
71 |
Correct |
145 ms |
34124 KB |
Output is correct |
72 |
Correct |
270 ms |
45132 KB |
Output is correct |
73 |
Correct |
329 ms |
46852 KB |
Output is correct |
74 |
Correct |
667 ms |
68684 KB |
Output is correct |
75 |
Correct |
377 ms |
42060 KB |
Output is correct |
76 |
Correct |
503 ms |
54048 KB |
Output is correct |
77 |
Correct |
511 ms |
54172 KB |
Output is correct |
78 |
Correct |
190 ms |
37768 KB |
Output is correct |
79 |
Correct |
272 ms |
42060 KB |
Output is correct |
80 |
Correct |
149 ms |
36428 KB |
Output is correct |
81 |
Correct |
255 ms |
40268 KB |
Output is correct |
82 |
Correct |
573 ms |
54604 KB |
Output is correct |
83 |
Correct |
516 ms |
54556 KB |
Output is correct |
84 |
Correct |
413 ms |
53448 KB |
Output is correct |
85 |
Correct |
420 ms |
53460 KB |
Output is correct |
86 |
Correct |
220 ms |
66892 KB |
Output is correct |
87 |
Correct |
229 ms |
69196 KB |
Output is correct |
88 |
Correct |
259 ms |
46000 KB |
Output is correct |
89 |
Correct |
410 ms |
51964 KB |
Output is correct |