#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 1e5 + 7;
int n, m;
ll res;
int p[N], sz[N];
set<int> out[N], in[N];
set<int> st[N];
int Find(int node) {
if(p[node] == node) return node;
p[node] = Find(p[node]);
return p[node];
}
void AddEdge(int a, int b) {
if(Find(a) == Find(b) || out[a].find(Find(b)) != out[a].end()) return;
//printf("Edge added: %d %d\n", a, Find(b));
out[a].insert(Find(b));
in[Find(b)].insert(a);
res += sz[Find(b)];
}
void RemoveEdge(int a, int b) {
//if(Find(a) == Find(b) || out[a].find(Find(b)) == out[a].end()) return;
//printf("Edge removed: %d %d\n", a, Find(b));
out[a].erase(Find(b));
in[Find(b)].erase(a);
res -= sz[Find(b)];
}
bool Union(int a, int b) {
a = Find(a);
b = Find(b);
if(a == b) return false;
if(sz[a] > sz[b]) swap(a, b);
bool ab = false, ba = false;
for(int v: st[a]) ab |= in[b].find(v) != in[b].end();
for(int v: in[a]) ba |= st[b].find(v) != st[b].end();
if(!ab || !ba) return false;
//printf("Union: %d %d\n", a, b);
vector<pair<int, int> > rem;
//printf("Removed edges\n");
for(int v: st[a]) {
if(out[v].find(b) != out[v].end()) rem.push_back({v, b});
}
for(int v: in[a]) {
if(st[b].find(v) != st[b].end()) rem.push_back({v, a});
}
//for(auto& e: rem) printf("%d %d\n", e.first, e.second);
for(auto& e: rem) RemoveEdge(e.first, e.second);
res += 2ll * sz[a] * sz[b];
//if (a == 4 && b == 2) printf("Union 4 2, res = %lld\n", res);
res += 1ll * in[b].size() * sz[a];
vector<int> vnext;
for(int v: in[a]) {
out[v].erase(a);
vnext.push_back(v);
if(in[b].find(v) == in[b].end()){
//printf("Redirect %d -> %d to %d\n", v, a, b);
in[b].insert(v);
res += sz[b];
out[v].insert(b);
} else res -= sz[a];
}
in[a] = {};
for(int v: st[a]){
for(int to: out[v]) vnext.push_back(to);
st[b].insert(v);
}
st[a] = {};
sz[b] += sz[a];
p[a] = b;
for(int v: vnext) Union(v, b);
return true;
}
void Init() {
res = 0;
for(int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
out[i] = in[i] = {};
st[i] = {i};
}
}
int main() {
#ifdef LOCAL
freopen("D:/cp/tmp/input.txt", "r", stdin);
#endif
scanf("%d%d", &n, &m);
Init();
for(int day = 1; day <= m; day++) {
int a, b;
scanf("%d%d", &a, &b);
//printf("%d -> %d, add = %d\n", a, b, sz[b]);
AddEdge(a, b);
/*if(day == 6) {
printf("res: %lld\n", res);
printf("4: ");
for(int v: st[4]) printf("%d ", v);
printf("\n2: ");
for(int v: st[2]) printf("%d ", v);
printf("\n4: ");
for(int v: in[4]) printf("%d ", v);
printf("\n2: ");
for(int v: in[2]) printf("%d ", v);
}*/
Union(a, b);
printf("%lld\n", res);
//printf("After day %d\n", day);
//for(int i = 1; i <= n; i++) printf("p[%d] = %d, sz[%d] = %d\n", i, p[i], i, sz[i]);
}
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:98:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
98 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
12 ms |
14444 KB |
Output is correct |
14 |
Correct |
12 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14572 KB |
Output is correct |
21 |
Correct |
13 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
12 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14520 KB |
Output is correct |
26 |
Correct |
10 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
10 ms |
14444 KB |
Output is correct |
29 |
Correct |
10 ms |
14384 KB |
Output is correct |
30 |
Correct |
13 ms |
14444 KB |
Output is correct |
31 |
Correct |
12 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
12 ms |
14444 KB |
Output is correct |
14 |
Correct |
12 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14572 KB |
Output is correct |
21 |
Correct |
13 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
12 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14520 KB |
Output is correct |
26 |
Correct |
10 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
10 ms |
14444 KB |
Output is correct |
29 |
Correct |
10 ms |
14384 KB |
Output is correct |
30 |
Correct |
13 ms |
14444 KB |
Output is correct |
31 |
Correct |
12 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14444 KB |
Output is correct |
34 |
Correct |
17 ms |
14572 KB |
Output is correct |
35 |
Correct |
136 ms |
19892 KB |
Output is correct |
36 |
Correct |
166 ms |
22408 KB |
Output is correct |
37 |
Correct |
181 ms |
22252 KB |
Output is correct |
38 |
Correct |
147 ms |
21996 KB |
Output is correct |
39 |
Correct |
13 ms |
14572 KB |
Output is correct |
40 |
Correct |
15 ms |
14700 KB |
Output is correct |
41 |
Correct |
16 ms |
14700 KB |
Output is correct |
42 |
Correct |
16 ms |
14572 KB |
Output is correct |
43 |
Correct |
15 ms |
14700 KB |
Output is correct |
44 |
Correct |
14 ms |
14700 KB |
Output is correct |
45 |
Correct |
14 ms |
14572 KB |
Output is correct |
46 |
Correct |
13 ms |
14572 KB |
Output is correct |
47 |
Correct |
15 ms |
14700 KB |
Output is correct |
48 |
Correct |
16 ms |
14700 KB |
Output is correct |
49 |
Correct |
24 ms |
15340 KB |
Output is correct |
50 |
Correct |
155 ms |
22252 KB |
Output is correct |
51 |
Correct |
21 ms |
14956 KB |
Output is correct |
52 |
Correct |
143 ms |
21100 KB |
Output is correct |
53 |
Correct |
22 ms |
15212 KB |
Output is correct |
54 |
Correct |
151 ms |
21612 KB |
Output is correct |
55 |
Correct |
19 ms |
14956 KB |
Output is correct |
56 |
Correct |
17 ms |
14956 KB |
Output is correct |
57 |
Correct |
17 ms |
15468 KB |
Output is correct |
58 |
Correct |
20 ms |
15468 KB |
Output is correct |
59 |
Correct |
15 ms |
14700 KB |
Output is correct |
60 |
Correct |
126 ms |
19564 KB |
Output is correct |
61 |
Correct |
16 ms |
14700 KB |
Output is correct |
62 |
Correct |
143 ms |
21996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
11 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
12 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
12 ms |
14444 KB |
Output is correct |
14 |
Correct |
12 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14572 KB |
Output is correct |
21 |
Correct |
13 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
12 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14520 KB |
Output is correct |
26 |
Correct |
10 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
10 ms |
14444 KB |
Output is correct |
29 |
Correct |
10 ms |
14384 KB |
Output is correct |
30 |
Correct |
13 ms |
14444 KB |
Output is correct |
31 |
Correct |
12 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14444 KB |
Output is correct |
34 |
Correct |
17 ms |
14572 KB |
Output is correct |
35 |
Correct |
136 ms |
19892 KB |
Output is correct |
36 |
Correct |
166 ms |
22408 KB |
Output is correct |
37 |
Correct |
181 ms |
22252 KB |
Output is correct |
38 |
Correct |
147 ms |
21996 KB |
Output is correct |
39 |
Correct |
13 ms |
14572 KB |
Output is correct |
40 |
Correct |
15 ms |
14700 KB |
Output is correct |
41 |
Correct |
16 ms |
14700 KB |
Output is correct |
42 |
Correct |
16 ms |
14572 KB |
Output is correct |
43 |
Correct |
15 ms |
14700 KB |
Output is correct |
44 |
Correct |
14 ms |
14700 KB |
Output is correct |
45 |
Correct |
14 ms |
14572 KB |
Output is correct |
46 |
Correct |
13 ms |
14572 KB |
Output is correct |
47 |
Correct |
15 ms |
14700 KB |
Output is correct |
48 |
Correct |
16 ms |
14700 KB |
Output is correct |
49 |
Correct |
24 ms |
15340 KB |
Output is correct |
50 |
Correct |
155 ms |
22252 KB |
Output is correct |
51 |
Correct |
21 ms |
14956 KB |
Output is correct |
52 |
Correct |
143 ms |
21100 KB |
Output is correct |
53 |
Correct |
22 ms |
15212 KB |
Output is correct |
54 |
Correct |
151 ms |
21612 KB |
Output is correct |
55 |
Correct |
19 ms |
14956 KB |
Output is correct |
56 |
Correct |
17 ms |
14956 KB |
Output is correct |
57 |
Correct |
17 ms |
15468 KB |
Output is correct |
58 |
Correct |
20 ms |
15468 KB |
Output is correct |
59 |
Correct |
15 ms |
14700 KB |
Output is correct |
60 |
Correct |
126 ms |
19564 KB |
Output is correct |
61 |
Correct |
16 ms |
14700 KB |
Output is correct |
62 |
Correct |
143 ms |
21996 KB |
Output is correct |
63 |
Correct |
637 ms |
53484 KB |
Output is correct |
64 |
Correct |
605 ms |
53740 KB |
Output is correct |
65 |
Correct |
605 ms |
53612 KB |
Output is correct |
66 |
Correct |
301 ms |
24300 KB |
Output is correct |
67 |
Correct |
738 ms |
28524 KB |
Output is correct |
68 |
Correct |
294 ms |
24428 KB |
Output is correct |
69 |
Correct |
662 ms |
29112 KB |
Output is correct |
70 |
Correct |
315 ms |
24300 KB |
Output is correct |
71 |
Correct |
312 ms |
24428 KB |
Output is correct |
72 |
Correct |
710 ms |
28644 KB |
Output is correct |
73 |
Correct |
647 ms |
28396 KB |
Output is correct |
74 |
Correct |
1404 ms |
37996 KB |
Output is correct |
75 |
Correct |
842 ms |
33772 KB |
Output is correct |
76 |
Correct |
1114 ms |
37100 KB |
Output is correct |
77 |
Correct |
1107 ms |
37220 KB |
Output is correct |
78 |
Correct |
307 ms |
27940 KB |
Output is correct |
79 |
Correct |
586 ms |
31524 KB |
Output is correct |
80 |
Correct |
336 ms |
30244 KB |
Output is correct |
81 |
Correct |
623 ms |
33444 KB |
Output is correct |
82 |
Correct |
922 ms |
42348 KB |
Output is correct |
83 |
Correct |
1022 ms |
42348 KB |
Output is correct |
84 |
Correct |
819 ms |
59884 KB |
Output is correct |
85 |
Correct |
835 ms |
59656 KB |
Output is correct |
86 |
Correct |
459 ms |
25196 KB |
Output is correct |
87 |
Correct |
610 ms |
27396 KB |
Output is correct |
88 |
Correct |
652 ms |
28424 KB |
Output is correct |
89 |
Correct |
1163 ms |
36204 KB |
Output is correct |