#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
int N, M;
ll ans = 0;
vector<int> rep;
vector<int> sz;
queue<pi> next_merge;
// Connections to and from component with rep i
vector<set<int>> to, from;
// Individuals following i
vector<set<int>> follows;
int tot_size(int a)
{
return to[a].size() + from[a].size() + follows[a].size();
}
// insert edge from a to b
void insert_edge(int a, int b)
{
from[a].insert(b);
to[b].insert(a);
if (to[a].find(b) != to[a].end())
next_merge.push(mp(a, b));
}
int find(int a)
{
if (rep[a] != a) {
rep[a] = find(rep[a]);
}
return rep[a];
}
void unite(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return;
// Create a strong link between component a and b
if (tot_size(a) < tot_size(b))
swap(a, b);
// Everyone following A will also follow B
// and vice versa
ans += follows[a].size() * (ll)sz[b];
ans += follows[b].size() * (ll)sz[a];
for (auto v : follows[b]) {
if (follows[a].find(v) != follows[a].end()) {
// We doublecounted
ans -= sz[a] + sz[b];
}
follows[a].insert(v);
}
sz[a] += sz[b];
rep[b] = a;
to[a].erase(b);
from[a].erase(b);
to[b].erase(a);
from[b].erase(a);
for (auto c : to[b]) {
// There is weak edge c -> b
from[c].erase(b);
insert_edge(c, a);
}
for (auto d : from[b]) {
// There is weak edge b -> d
to[d].erase(b);
insert_edge(a, d);
}
}
int main(void)
{
scanf("%d %d", &N, &M);
rep.resize(N);
to.resize(N);
from.resize(N);
follows.resize(N);
sz.assign(N, 1);
for (int i = 0; i < N; i++) {
rep[i] = i;
follows[i].insert(i);
}
while (M--) {
int u, v;
scanf("%d %d", &u, &v);
u--; v--;
int c1 = find(u);
int c2 = find(v);
if (c1 == c2 || follows[c2].count(u)) {
printf("%lld\n", ans);
continue;
}
// u can now follow everyone in c2
ans += sz[c2];
follows[c2].insert(u);
insert_edge(c1, c2);
while (next_merge.empty() == false) {
unite(next_merge.front().first, next_merge.front().second);
next_merge.pop();
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d %d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 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 |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 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 |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 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 |
444 KB |
Output is correct |
35 |
Correct |
88 ms |
6252 KB |
Output is correct |
36 |
Correct |
101 ms |
9140 KB |
Output is correct |
37 |
Correct |
100 ms |
9216 KB |
Output is correct |
38 |
Correct |
96 ms |
8844 KB |
Output is correct |
39 |
Correct |
3 ms |
932 KB |
Output is correct |
40 |
Correct |
6 ms |
1296 KB |
Output is correct |
41 |
Correct |
4 ms |
1208 KB |
Output is correct |
42 |
Correct |
3 ms |
956 KB |
Output is correct |
43 |
Correct |
4 ms |
956 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
852 KB |
Output is correct |
47 |
Correct |
5 ms |
1112 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
9 ms |
1800 KB |
Output is correct |
50 |
Correct |
105 ms |
9336 KB |
Output is correct |
51 |
Correct |
9 ms |
1208 KB |
Output is correct |
52 |
Correct |
86 ms |
7780 KB |
Output is correct |
53 |
Correct |
13 ms |
1748 KB |
Output is correct |
54 |
Correct |
90 ms |
8408 KB |
Output is correct |
55 |
Correct |
5 ms |
1312 KB |
Output is correct |
56 |
Correct |
6 ms |
1332 KB |
Output is correct |
57 |
Correct |
7 ms |
1340 KB |
Output is correct |
58 |
Correct |
5 ms |
1336 KB |
Output is correct |
59 |
Correct |
4 ms |
1364 KB |
Output is correct |
60 |
Correct |
76 ms |
6240 KB |
Output is correct |
61 |
Correct |
4 ms |
1080 KB |
Output is correct |
62 |
Correct |
99 ms |
8752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 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 |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 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 |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 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 |
444 KB |
Output is correct |
35 |
Correct |
88 ms |
6252 KB |
Output is correct |
36 |
Correct |
101 ms |
9140 KB |
Output is correct |
37 |
Correct |
100 ms |
9216 KB |
Output is correct |
38 |
Correct |
96 ms |
8844 KB |
Output is correct |
39 |
Correct |
3 ms |
932 KB |
Output is correct |
40 |
Correct |
6 ms |
1296 KB |
Output is correct |
41 |
Correct |
4 ms |
1208 KB |
Output is correct |
42 |
Correct |
3 ms |
956 KB |
Output is correct |
43 |
Correct |
4 ms |
956 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
3 ms |
852 KB |
Output is correct |
46 |
Correct |
3 ms |
852 KB |
Output is correct |
47 |
Correct |
5 ms |
1112 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
9 ms |
1800 KB |
Output is correct |
50 |
Correct |
105 ms |
9336 KB |
Output is correct |
51 |
Correct |
9 ms |
1208 KB |
Output is correct |
52 |
Correct |
86 ms |
7780 KB |
Output is correct |
53 |
Correct |
13 ms |
1748 KB |
Output is correct |
54 |
Correct |
90 ms |
8408 KB |
Output is correct |
55 |
Correct |
5 ms |
1312 KB |
Output is correct |
56 |
Correct |
6 ms |
1332 KB |
Output is correct |
57 |
Correct |
7 ms |
1340 KB |
Output is correct |
58 |
Correct |
5 ms |
1336 KB |
Output is correct |
59 |
Correct |
4 ms |
1364 KB |
Output is correct |
60 |
Correct |
76 ms |
6240 KB |
Output is correct |
61 |
Correct |
4 ms |
1080 KB |
Output is correct |
62 |
Correct |
99 ms |
8752 KB |
Output is correct |
63 |
Correct |
532 ms |
67524 KB |
Output is correct |
64 |
Correct |
535 ms |
67592 KB |
Output is correct |
65 |
Correct |
523 ms |
67588 KB |
Output is correct |
66 |
Correct |
235 ms |
33672 KB |
Output is correct |
67 |
Correct |
431 ms |
60588 KB |
Output is correct |
68 |
Correct |
196 ms |
33628 KB |
Output is correct |
69 |
Correct |
382 ms |
34772 KB |
Output is correct |
70 |
Correct |
226 ms |
33588 KB |
Output is correct |
71 |
Correct |
248 ms |
33636 KB |
Output is correct |
72 |
Correct |
376 ms |
44648 KB |
Output is correct |
73 |
Correct |
388 ms |
46304 KB |
Output is correct |
74 |
Correct |
1041 ms |
68196 KB |
Output is correct |
75 |
Correct |
511 ms |
41576 KB |
Output is correct |
76 |
Correct |
702 ms |
53556 KB |
Output is correct |
77 |
Correct |
676 ms |
53800 KB |
Output is correct |
78 |
Correct |
229 ms |
37360 KB |
Output is correct |
79 |
Correct |
375 ms |
41680 KB |
Output is correct |
80 |
Correct |
214 ms |
36184 KB |
Output is correct |
81 |
Correct |
341 ms |
39740 KB |
Output is correct |
82 |
Correct |
735 ms |
54180 KB |
Output is correct |
83 |
Correct |
748 ms |
54296 KB |
Output is correct |
84 |
Correct |
651 ms |
52980 KB |
Output is correct |
85 |
Correct |
554 ms |
52980 KB |
Output is correct |
86 |
Correct |
288 ms |
66400 KB |
Output is correct |
87 |
Correct |
306 ms |
68712 KB |
Output is correct |
88 |
Correct |
385 ms |
45656 KB |
Output is correct |
89 |
Correct |
678 ms |
51496 KB |
Output is correct |