// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e5 + 10;
int S[N], rt[N], n, m; ll ret; set<int> in[N], out[N], comp[N];
queue<pii> Q;
int Find(int v) {
return !rt[v] ? v : rt[v] = Find(rt[v]);
}
inline void add_edge(int u, int v) {
out[u].insert(v), in[v].insert(u);
if (out[v].count(u)) Q.push({u, v});
}
void Union(int u, int v) {
u = Find(u), v = Find(v);
if (u == v) return;
if (SZ(comp[u]) + SZ(in[u]) + SZ(out[u]) > SZ(comp[v]) + SZ(in[v]) + SZ(out[v])) swap(u, v);
ret += 1ll * S[u] * SZ(comp[v]) + 1ll * S[v] * SZ(comp[u]);
rt[u] = v;
S[v] += S[u];
for (int w : comp[u]) {
if (comp[v].count(w)) ret -= S[v];
else comp[v].insert(w);
}
in[u].erase(v), out[u].erase(v), in[v].erase(u), out[v].erase(u);
for (int w : in[u]) {
out[w].erase(u);
add_edge(w, v);
}
for (int w : out[u]) {
in[w].erase(u);
add_edge(v, w);
}
}
int main() {
fill(S, S + N, 1);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) comp[i].insert(i);
for (; m; m--) {
int u, v; scanf("%d%d", &u, &v);
v = Find(v);
if (Find(u) != v && !comp[v].count(u)) {
comp[v].insert(u);
ret += S[v];
add_edge(Find(u), v);
while (SZ(Q)) {
int U = Q.front().F, V = Q.front().S; Q.pop();
Union(U, V);
}
}
printf("%lld\n", ret);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:53:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14828 KB |
Output is correct |
2 |
Correct |
10 ms |
14828 KB |
Output is correct |
3 |
Correct |
10 ms |
14828 KB |
Output is correct |
4 |
Correct |
10 ms |
14828 KB |
Output is correct |
5 |
Correct |
10 ms |
14848 KB |
Output is correct |
6 |
Correct |
10 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14828 KB |
Output is correct |
8 |
Correct |
11 ms |
14828 KB |
Output is correct |
9 |
Correct |
11 ms |
14828 KB |
Output is correct |
10 |
Correct |
11 ms |
14828 KB |
Output is correct |
11 |
Correct |
11 ms |
14828 KB |
Output is correct |
12 |
Correct |
10 ms |
14828 KB |
Output is correct |
13 |
Correct |
10 ms |
14828 KB |
Output is correct |
14 |
Correct |
10 ms |
14828 KB |
Output is correct |
15 |
Correct |
10 ms |
14828 KB |
Output is correct |
16 |
Correct |
10 ms |
14828 KB |
Output is correct |
17 |
Correct |
10 ms |
14828 KB |
Output is correct |
18 |
Correct |
10 ms |
14828 KB |
Output is correct |
19 |
Correct |
10 ms |
14828 KB |
Output is correct |
20 |
Correct |
11 ms |
14828 KB |
Output is correct |
21 |
Correct |
11 ms |
14828 KB |
Output is correct |
22 |
Correct |
10 ms |
14828 KB |
Output is correct |
23 |
Correct |
11 ms |
14828 KB |
Output is correct |
24 |
Correct |
11 ms |
14828 KB |
Output is correct |
25 |
Correct |
11 ms |
14828 KB |
Output is correct |
26 |
Correct |
10 ms |
14828 KB |
Output is correct |
27 |
Correct |
10 ms |
14828 KB |
Output is correct |
28 |
Correct |
10 ms |
14828 KB |
Output is correct |
29 |
Correct |
10 ms |
14828 KB |
Output is correct |
30 |
Correct |
10 ms |
14828 KB |
Output is correct |
31 |
Correct |
11 ms |
14828 KB |
Output is correct |
32 |
Correct |
11 ms |
14828 KB |
Output is correct |
33 |
Correct |
11 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14828 KB |
Output is correct |
2 |
Correct |
10 ms |
14828 KB |
Output is correct |
3 |
Correct |
10 ms |
14828 KB |
Output is correct |
4 |
Correct |
10 ms |
14828 KB |
Output is correct |
5 |
Correct |
10 ms |
14848 KB |
Output is correct |
6 |
Correct |
10 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14828 KB |
Output is correct |
8 |
Correct |
11 ms |
14828 KB |
Output is correct |
9 |
Correct |
11 ms |
14828 KB |
Output is correct |
10 |
Correct |
11 ms |
14828 KB |
Output is correct |
11 |
Correct |
11 ms |
14828 KB |
Output is correct |
12 |
Correct |
10 ms |
14828 KB |
Output is correct |
13 |
Correct |
10 ms |
14828 KB |
Output is correct |
14 |
Correct |
10 ms |
14828 KB |
Output is correct |
15 |
Correct |
10 ms |
14828 KB |
Output is correct |
16 |
Correct |
10 ms |
14828 KB |
Output is correct |
17 |
Correct |
10 ms |
14828 KB |
Output is correct |
18 |
Correct |
10 ms |
14828 KB |
Output is correct |
19 |
Correct |
10 ms |
14828 KB |
Output is correct |
20 |
Correct |
11 ms |
14828 KB |
Output is correct |
21 |
Correct |
11 ms |
14828 KB |
Output is correct |
22 |
Correct |
10 ms |
14828 KB |
Output is correct |
23 |
Correct |
11 ms |
14828 KB |
Output is correct |
24 |
Correct |
11 ms |
14828 KB |
Output is correct |
25 |
Correct |
11 ms |
14828 KB |
Output is correct |
26 |
Correct |
10 ms |
14828 KB |
Output is correct |
27 |
Correct |
10 ms |
14828 KB |
Output is correct |
28 |
Correct |
10 ms |
14828 KB |
Output is correct |
29 |
Correct |
10 ms |
14828 KB |
Output is correct |
30 |
Correct |
10 ms |
14828 KB |
Output is correct |
31 |
Correct |
11 ms |
14828 KB |
Output is correct |
32 |
Correct |
11 ms |
14828 KB |
Output is correct |
33 |
Correct |
11 ms |
14956 KB |
Output is correct |
34 |
Correct |
14 ms |
14956 KB |
Output is correct |
35 |
Correct |
140 ms |
20780 KB |
Output is correct |
36 |
Correct |
166 ms |
23532 KB |
Output is correct |
37 |
Correct |
158 ms |
23532 KB |
Output is correct |
38 |
Correct |
152 ms |
23148 KB |
Output is correct |
39 |
Correct |
13 ms |
15212 KB |
Output is correct |
40 |
Correct |
15 ms |
15596 KB |
Output is correct |
41 |
Correct |
15 ms |
15468 KB |
Output is correct |
42 |
Correct |
13 ms |
15212 KB |
Output is correct |
43 |
Correct |
19 ms |
15212 KB |
Output is correct |
44 |
Correct |
15 ms |
15212 KB |
Output is correct |
45 |
Correct |
14 ms |
15212 KB |
Output is correct |
46 |
Correct |
15 ms |
15212 KB |
Output is correct |
47 |
Correct |
17 ms |
15340 KB |
Output is correct |
48 |
Correct |
15 ms |
15340 KB |
Output is correct |
49 |
Correct |
25 ms |
16108 KB |
Output is correct |
50 |
Correct |
154 ms |
23660 KB |
Output is correct |
51 |
Correct |
21 ms |
15468 KB |
Output is correct |
52 |
Correct |
138 ms |
22252 KB |
Output is correct |
53 |
Correct |
23 ms |
15980 KB |
Output is correct |
54 |
Correct |
147 ms |
22784 KB |
Output is correct |
55 |
Correct |
17 ms |
15616 KB |
Output is correct |
56 |
Correct |
17 ms |
15596 KB |
Output is correct |
57 |
Correct |
19 ms |
15596 KB |
Output is correct |
58 |
Correct |
16 ms |
15596 KB |
Output is correct |
59 |
Correct |
15 ms |
15596 KB |
Output is correct |
60 |
Correct |
118 ms |
20460 KB |
Output is correct |
61 |
Correct |
16 ms |
15468 KB |
Output is correct |
62 |
Correct |
152 ms |
23148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14828 KB |
Output is correct |
2 |
Correct |
10 ms |
14828 KB |
Output is correct |
3 |
Correct |
10 ms |
14828 KB |
Output is correct |
4 |
Correct |
10 ms |
14828 KB |
Output is correct |
5 |
Correct |
10 ms |
14848 KB |
Output is correct |
6 |
Correct |
10 ms |
14828 KB |
Output is correct |
7 |
Correct |
11 ms |
14828 KB |
Output is correct |
8 |
Correct |
11 ms |
14828 KB |
Output is correct |
9 |
Correct |
11 ms |
14828 KB |
Output is correct |
10 |
Correct |
11 ms |
14828 KB |
Output is correct |
11 |
Correct |
11 ms |
14828 KB |
Output is correct |
12 |
Correct |
10 ms |
14828 KB |
Output is correct |
13 |
Correct |
10 ms |
14828 KB |
Output is correct |
14 |
Correct |
10 ms |
14828 KB |
Output is correct |
15 |
Correct |
10 ms |
14828 KB |
Output is correct |
16 |
Correct |
10 ms |
14828 KB |
Output is correct |
17 |
Correct |
10 ms |
14828 KB |
Output is correct |
18 |
Correct |
10 ms |
14828 KB |
Output is correct |
19 |
Correct |
10 ms |
14828 KB |
Output is correct |
20 |
Correct |
11 ms |
14828 KB |
Output is correct |
21 |
Correct |
11 ms |
14828 KB |
Output is correct |
22 |
Correct |
10 ms |
14828 KB |
Output is correct |
23 |
Correct |
11 ms |
14828 KB |
Output is correct |
24 |
Correct |
11 ms |
14828 KB |
Output is correct |
25 |
Correct |
11 ms |
14828 KB |
Output is correct |
26 |
Correct |
10 ms |
14828 KB |
Output is correct |
27 |
Correct |
10 ms |
14828 KB |
Output is correct |
28 |
Correct |
10 ms |
14828 KB |
Output is correct |
29 |
Correct |
10 ms |
14828 KB |
Output is correct |
30 |
Correct |
10 ms |
14828 KB |
Output is correct |
31 |
Correct |
11 ms |
14828 KB |
Output is correct |
32 |
Correct |
11 ms |
14828 KB |
Output is correct |
33 |
Correct |
11 ms |
14956 KB |
Output is correct |
34 |
Correct |
14 ms |
14956 KB |
Output is correct |
35 |
Correct |
140 ms |
20780 KB |
Output is correct |
36 |
Correct |
166 ms |
23532 KB |
Output is correct |
37 |
Correct |
158 ms |
23532 KB |
Output is correct |
38 |
Correct |
152 ms |
23148 KB |
Output is correct |
39 |
Correct |
13 ms |
15212 KB |
Output is correct |
40 |
Correct |
15 ms |
15596 KB |
Output is correct |
41 |
Correct |
15 ms |
15468 KB |
Output is correct |
42 |
Correct |
13 ms |
15212 KB |
Output is correct |
43 |
Correct |
19 ms |
15212 KB |
Output is correct |
44 |
Correct |
15 ms |
15212 KB |
Output is correct |
45 |
Correct |
14 ms |
15212 KB |
Output is correct |
46 |
Correct |
15 ms |
15212 KB |
Output is correct |
47 |
Correct |
17 ms |
15340 KB |
Output is correct |
48 |
Correct |
15 ms |
15340 KB |
Output is correct |
49 |
Correct |
25 ms |
16108 KB |
Output is correct |
50 |
Correct |
154 ms |
23660 KB |
Output is correct |
51 |
Correct |
21 ms |
15468 KB |
Output is correct |
52 |
Correct |
138 ms |
22252 KB |
Output is correct |
53 |
Correct |
23 ms |
15980 KB |
Output is correct |
54 |
Correct |
147 ms |
22784 KB |
Output is correct |
55 |
Correct |
17 ms |
15616 KB |
Output is correct |
56 |
Correct |
17 ms |
15596 KB |
Output is correct |
57 |
Correct |
19 ms |
15596 KB |
Output is correct |
58 |
Correct |
16 ms |
15596 KB |
Output is correct |
59 |
Correct |
15 ms |
15596 KB |
Output is correct |
60 |
Correct |
118 ms |
20460 KB |
Output is correct |
61 |
Correct |
16 ms |
15468 KB |
Output is correct |
62 |
Correct |
152 ms |
23148 KB |
Output is correct |
63 |
Correct |
653 ms |
67428 KB |
Output is correct |
64 |
Correct |
655 ms |
67308 KB |
Output is correct |
65 |
Correct |
645 ms |
67308 KB |
Output is correct |
66 |
Correct |
294 ms |
33772 KB |
Output is correct |
67 |
Correct |
584 ms |
60780 KB |
Output is correct |
68 |
Correct |
289 ms |
33772 KB |
Output is correct |
69 |
Correct |
594 ms |
35052 KB |
Output is correct |
70 |
Correct |
348 ms |
33772 KB |
Output is correct |
71 |
Correct |
354 ms |
33772 KB |
Output is correct |
72 |
Correct |
613 ms |
44668 KB |
Output is correct |
73 |
Correct |
634 ms |
46400 KB |
Output is correct |
74 |
Correct |
1304 ms |
68516 KB |
Output is correct |
75 |
Correct |
824 ms |
41596 KB |
Output is correct |
76 |
Correct |
1016 ms |
53740 KB |
Output is correct |
77 |
Correct |
1029 ms |
53868 KB |
Output is correct |
78 |
Correct |
324 ms |
37612 KB |
Output is correct |
79 |
Correct |
573 ms |
41708 KB |
Output is correct |
80 |
Correct |
292 ms |
36124 KB |
Output is correct |
81 |
Correct |
510 ms |
39936 KB |
Output is correct |
82 |
Correct |
1083 ms |
54380 KB |
Output is correct |
83 |
Correct |
1128 ms |
54636 KB |
Output is correct |
84 |
Correct |
850 ms |
53100 KB |
Output is correct |
85 |
Correct |
836 ms |
53036 KB |
Output is correct |
86 |
Correct |
431 ms |
66668 KB |
Output is correct |
87 |
Correct |
502 ms |
68716 KB |
Output is correct |
88 |
Correct |
628 ms |
45632 KB |
Output is correct |
89 |
Correct |
1006 ms |
51696 KB |
Output is correct |