#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 300'010;
struct {
set<int> *out_nodes, *out_comp;
map<int, int> *in_comp;
vector<int> *comp_nodes;
int rt;
} node[N];
ll ans = 0;
void add(int i, int j);
void merge(int i, int j);
void merge(int i, int j)
{
assert(node[i].rt == i);
assert(node[j].rt == j);
if (i == j)
return;
int szi = node[i].comp_nodes->size() + node[i].out_nodes->size() + node[i].in_comp->size();
int szj = node[j].comp_nodes->size() + node[j].out_nodes->size() + node[i].in_comp->size();
if (szi < szj)
swap(i, j);
ans -= node[i].comp_nodes->size() * node[i].out_nodes->size();
ans -= node[j].comp_nodes->size() * node[j].out_nodes->size();
delete(node[j].out_comp);
node[j].out_comp = 0;
for (int k : *node[j].comp_nodes) {
node[i].comp_nodes->push_back(k);
node[k].rt = i;
}
delete(node[j].comp_nodes);
node[j].comp_nodes = 0;
ans += node[i].comp_nodes->size() * node[i].out_nodes->size();
for (auto [k, jj] : *node[j].in_comp) {
if (node[k].out_comp == NULL)
continue;
add(k, jj);
}
delete(node[j].in_comp);
node[j].in_comp = 0;
for (int k : *node[j].out_nodes)
add(i, k);
delete(node[j].out_nodes);
node[j].out_nodes = 0;
}
void add(int i, int j)
{
i = node[i].rt;
if (node[i].out_nodes->insert(j).second == true)
ans += node[i].comp_nodes->size();
int jj = j;
j = node[j].rt;
node[i].out_comp->insert(j);
(*node[j].in_comp)[i] = jj;
if (node[j].out_comp->count(i))
merge(i, j);
}
void init(int n)
{
Loop (i,0,n) {
node[i].rt = i;
node[i].comp_nodes = new vector<int>({i});
node[i].out_nodes = new set<int>({i});
node[i].out_comp = new set<int>({i});
node[i].in_comp = new map<int, int>({{i, i}});
}
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int n, m;
cin >> n >> m;
init(n);
while (m--) {
int i, j;
cin >> i >> j;
--i; --j;
add(j, i);
cout << ans << '\n';
}
}
Compilation message
joitter2.cpp: In function 'void init(int)':
joitter2.cpp:74:41: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
74 | node[i].comp_nodes = new vector<int>({i});
| ^
joitter2.cpp:74:41: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
joitter2.cpp:75:37: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
75 | node[i].out_nodes = new set<int>({i});
| ^
joitter2.cpp:75:37: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
joitter2.cpp:76:36: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
76 | node[i].out_comp = new set<int>({i});
| ^
joitter2.cpp:76:36: warning: narrowing conversion of 'i' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
120 ms |
6560 KB |
Output is correct |
36 |
Correct |
139 ms |
9792 KB |
Output is correct |
37 |
Correct |
136 ms |
9844 KB |
Output is correct |
38 |
Correct |
146 ms |
9564 KB |
Output is correct |
39 |
Correct |
4 ms |
1232 KB |
Output is correct |
40 |
Correct |
6 ms |
1236 KB |
Output is correct |
41 |
Correct |
6 ms |
1364 KB |
Output is correct |
42 |
Correct |
4 ms |
1236 KB |
Output is correct |
43 |
Correct |
5 ms |
1364 KB |
Output is correct |
44 |
Correct |
4 ms |
1364 KB |
Output is correct |
45 |
Correct |
5 ms |
1236 KB |
Output is correct |
46 |
Correct |
4 ms |
1236 KB |
Output is correct |
47 |
Correct |
6 ms |
1364 KB |
Output is correct |
48 |
Correct |
6 ms |
1364 KB |
Output is correct |
49 |
Correct |
11 ms |
1972 KB |
Output is correct |
50 |
Correct |
144 ms |
10076 KB |
Output is correct |
51 |
Correct |
8 ms |
1620 KB |
Output is correct |
52 |
Correct |
125 ms |
8212 KB |
Output is correct |
53 |
Correct |
10 ms |
2000 KB |
Output is correct |
54 |
Correct |
128 ms |
8976 KB |
Output is correct |
55 |
Correct |
8 ms |
1748 KB |
Output is correct |
56 |
Correct |
6 ms |
1748 KB |
Output is correct |
57 |
Correct |
6 ms |
1752 KB |
Output is correct |
58 |
Correct |
6 ms |
1748 KB |
Output is correct |
59 |
Correct |
5 ms |
1236 KB |
Output is correct |
60 |
Correct |
104 ms |
6092 KB |
Output is correct |
61 |
Correct |
6 ms |
1428 KB |
Output is correct |
62 |
Correct |
136 ms |
9364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
120 ms |
6560 KB |
Output is correct |
36 |
Correct |
139 ms |
9792 KB |
Output is correct |
37 |
Correct |
136 ms |
9844 KB |
Output is correct |
38 |
Correct |
146 ms |
9564 KB |
Output is correct |
39 |
Correct |
4 ms |
1232 KB |
Output is correct |
40 |
Correct |
6 ms |
1236 KB |
Output is correct |
41 |
Correct |
6 ms |
1364 KB |
Output is correct |
42 |
Correct |
4 ms |
1236 KB |
Output is correct |
43 |
Correct |
5 ms |
1364 KB |
Output is correct |
44 |
Correct |
4 ms |
1364 KB |
Output is correct |
45 |
Correct |
5 ms |
1236 KB |
Output is correct |
46 |
Correct |
4 ms |
1236 KB |
Output is correct |
47 |
Correct |
6 ms |
1364 KB |
Output is correct |
48 |
Correct |
6 ms |
1364 KB |
Output is correct |
49 |
Correct |
11 ms |
1972 KB |
Output is correct |
50 |
Correct |
144 ms |
10076 KB |
Output is correct |
51 |
Correct |
8 ms |
1620 KB |
Output is correct |
52 |
Correct |
125 ms |
8212 KB |
Output is correct |
53 |
Correct |
10 ms |
2000 KB |
Output is correct |
54 |
Correct |
128 ms |
8976 KB |
Output is correct |
55 |
Correct |
8 ms |
1748 KB |
Output is correct |
56 |
Correct |
6 ms |
1748 KB |
Output is correct |
57 |
Correct |
6 ms |
1752 KB |
Output is correct |
58 |
Correct |
6 ms |
1748 KB |
Output is correct |
59 |
Correct |
5 ms |
1236 KB |
Output is correct |
60 |
Correct |
104 ms |
6092 KB |
Output is correct |
61 |
Correct |
6 ms |
1428 KB |
Output is correct |
62 |
Correct |
136 ms |
9364 KB |
Output is correct |
63 |
Correct |
372 ms |
91028 KB |
Output is correct |
64 |
Correct |
383 ms |
90968 KB |
Output is correct |
65 |
Correct |
370 ms |
91084 KB |
Output is correct |
66 |
Correct |
397 ms |
48200 KB |
Output is correct |
67 |
Correct |
727 ms |
52684 KB |
Output is correct |
68 |
Correct |
378 ms |
48288 KB |
Output is correct |
69 |
Correct |
459 ms |
52732 KB |
Output is correct |
70 |
Correct |
434 ms |
48240 KB |
Output is correct |
71 |
Correct |
438 ms |
48212 KB |
Output is correct |
72 |
Correct |
692 ms |
52124 KB |
Output is correct |
73 |
Correct |
700 ms |
52552 KB |
Output is correct |
74 |
Correct |
1185 ms |
61304 KB |
Output is correct |
75 |
Correct |
664 ms |
58148 KB |
Output is correct |
76 |
Correct |
982 ms |
61332 KB |
Output is correct |
77 |
Correct |
978 ms |
61468 KB |
Output is correct |
78 |
Correct |
298 ms |
58516 KB |
Output is correct |
79 |
Correct |
655 ms |
62000 KB |
Output is correct |
80 |
Correct |
281 ms |
53932 KB |
Output is correct |
81 |
Correct |
547 ms |
57324 KB |
Output is correct |
82 |
Correct |
765 ms |
75792 KB |
Output is correct |
83 |
Correct |
769 ms |
75736 KB |
Output is correct |
84 |
Correct |
734 ms |
75996 KB |
Output is correct |
85 |
Correct |
742 ms |
75984 KB |
Output is correct |
86 |
Correct |
561 ms |
47756 KB |
Output is correct |
87 |
Correct |
630 ms |
50196 KB |
Output is correct |
88 |
Correct |
726 ms |
52568 KB |
Output is correct |
89 |
Correct |
967 ms |
61304 KB |
Output is correct |