#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5+5;
int n, m, par[N], sz[N];
set<int> in[N], out[N], E[N];
long ans;
void update(int x, int sign) { ans += sign * (1ll * (E[x].size() - sz[x]) * sz[x] + 1ll * sz[x] * (sz[x] - 1)); }
int find(int x) { return x == par[x] ? x : find(par[x]); }
queue<pii> Q;
void unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
update(a, -1), update(b, -1);
if(in[a].size() + out[a].size() + E[a].size() > in[b].size() + out[b].size() + E[b].size())
swap(a, b);
in[a].erase(b), out[a].erase(b);
in[b].erase(a), out[b].erase(a);
for(int x : in[a]) {
if(out[b].count(x)) Q.emplace(x, b);
out[x].erase(a), out[x].emplace(b);
}
for(int x : out[a]) {
if(in[b].count(x)) Q.emplace(x, b);
in[x].erase(a), in[x].emplace(b);
}
for(int x : in[a]) in[b].emplace(x);
for(int x : out[a]) out[b].emplace(x);
for(int x : E[a]) E[b].emplace(x);
in[a].clear(), out[a].clear(), E[a].clear();
par[a] = b, sz[b] += sz[a];
update(b, 1);
}
int main() {
iota(par, par + N, 0), fill_n(sz, N, 1);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) E[i].emplace(i);
for(int i = 1, a, b; i <= m; i++) {
scanf("%d %d", &a, &b);
int pa = find(a), pb = find(b);
if(out[pb].count(pa)) {
Q.emplace(a, b);
while(!Q.empty()) {
pii now = Q.front(); Q.pop();
unite(now.x, now.y);
}
} else {
update(pb, -1);
E[pb].emplace(a);
update(pb, 1);
out[pa].emplace(pb), in[pb].emplace(pa);
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15232 KB |
Output is correct |
2 |
Correct |
14 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
13 ms |
15232 KB |
Output is correct |
6 |
Correct |
13 ms |
15232 KB |
Output is correct |
7 |
Correct |
21 ms |
15232 KB |
Output is correct |
8 |
Correct |
15 ms |
15232 KB |
Output is correct |
9 |
Correct |
13 ms |
15232 KB |
Output is correct |
10 |
Correct |
14 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
12 ms |
15232 KB |
Output is correct |
17 |
Correct |
13 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
14 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
15 ms |
15232 KB |
Output is correct |
22 |
Correct |
13 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
13 ms |
15232 KB |
Output is correct |
27 |
Correct |
15 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
13 ms |
15232 KB |
Output is correct |
33 |
Correct |
13 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15232 KB |
Output is correct |
2 |
Correct |
14 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
13 ms |
15232 KB |
Output is correct |
6 |
Correct |
13 ms |
15232 KB |
Output is correct |
7 |
Correct |
21 ms |
15232 KB |
Output is correct |
8 |
Correct |
15 ms |
15232 KB |
Output is correct |
9 |
Correct |
13 ms |
15232 KB |
Output is correct |
10 |
Correct |
14 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
12 ms |
15232 KB |
Output is correct |
17 |
Correct |
13 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
14 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
15 ms |
15232 KB |
Output is correct |
22 |
Correct |
13 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
13 ms |
15232 KB |
Output is correct |
27 |
Correct |
15 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
13 ms |
15232 KB |
Output is correct |
33 |
Correct |
13 ms |
15232 KB |
Output is correct |
34 |
Correct |
17 ms |
15360 KB |
Output is correct |
35 |
Correct |
144 ms |
18648 KB |
Output is correct |
36 |
Correct |
175 ms |
21052 KB |
Output is correct |
37 |
Correct |
168 ms |
20984 KB |
Output is correct |
38 |
Correct |
165 ms |
20728 KB |
Output is correct |
39 |
Correct |
16 ms |
15360 KB |
Output is correct |
40 |
Correct |
17 ms |
15488 KB |
Output is correct |
41 |
Correct |
17 ms |
15488 KB |
Output is correct |
42 |
Correct |
16 ms |
15360 KB |
Output is correct |
43 |
Correct |
17 ms |
15488 KB |
Output is correct |
44 |
Correct |
16 ms |
15488 KB |
Output is correct |
45 |
Correct |
16 ms |
15360 KB |
Output is correct |
46 |
Correct |
15 ms |
15360 KB |
Output is correct |
47 |
Correct |
17 ms |
15488 KB |
Output is correct |
48 |
Correct |
19 ms |
15488 KB |
Output is correct |
49 |
Correct |
26 ms |
16124 KB |
Output is correct |
50 |
Correct |
175 ms |
21196 KB |
Output is correct |
51 |
Correct |
23 ms |
15616 KB |
Output is correct |
52 |
Correct |
150 ms |
19576 KB |
Output is correct |
53 |
Correct |
26 ms |
16000 KB |
Output is correct |
54 |
Correct |
163 ms |
20320 KB |
Output is correct |
55 |
Correct |
19 ms |
15872 KB |
Output is correct |
56 |
Correct |
19 ms |
15872 KB |
Output is correct |
57 |
Correct |
19 ms |
15872 KB |
Output is correct |
58 |
Correct |
20 ms |
15872 KB |
Output is correct |
59 |
Correct |
17 ms |
15360 KB |
Output is correct |
60 |
Correct |
151 ms |
17912 KB |
Output is correct |
61 |
Correct |
18 ms |
15520 KB |
Output is correct |
62 |
Correct |
171 ms |
20728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15232 KB |
Output is correct |
2 |
Correct |
14 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
13 ms |
15232 KB |
Output is correct |
6 |
Correct |
13 ms |
15232 KB |
Output is correct |
7 |
Correct |
21 ms |
15232 KB |
Output is correct |
8 |
Correct |
15 ms |
15232 KB |
Output is correct |
9 |
Correct |
13 ms |
15232 KB |
Output is correct |
10 |
Correct |
14 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
12 ms |
15232 KB |
Output is correct |
17 |
Correct |
13 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
14 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
15 ms |
15232 KB |
Output is correct |
22 |
Correct |
13 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
13 ms |
15232 KB |
Output is correct |
27 |
Correct |
15 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
13 ms |
15232 KB |
Output is correct |
33 |
Correct |
13 ms |
15232 KB |
Output is correct |
34 |
Correct |
17 ms |
15360 KB |
Output is correct |
35 |
Correct |
144 ms |
18648 KB |
Output is correct |
36 |
Correct |
175 ms |
21052 KB |
Output is correct |
37 |
Correct |
168 ms |
20984 KB |
Output is correct |
38 |
Correct |
165 ms |
20728 KB |
Output is correct |
39 |
Correct |
16 ms |
15360 KB |
Output is correct |
40 |
Correct |
17 ms |
15488 KB |
Output is correct |
41 |
Correct |
17 ms |
15488 KB |
Output is correct |
42 |
Correct |
16 ms |
15360 KB |
Output is correct |
43 |
Correct |
17 ms |
15488 KB |
Output is correct |
44 |
Correct |
16 ms |
15488 KB |
Output is correct |
45 |
Correct |
16 ms |
15360 KB |
Output is correct |
46 |
Correct |
15 ms |
15360 KB |
Output is correct |
47 |
Correct |
17 ms |
15488 KB |
Output is correct |
48 |
Correct |
19 ms |
15488 KB |
Output is correct |
49 |
Correct |
26 ms |
16124 KB |
Output is correct |
50 |
Correct |
175 ms |
21196 KB |
Output is correct |
51 |
Correct |
23 ms |
15616 KB |
Output is correct |
52 |
Correct |
150 ms |
19576 KB |
Output is correct |
53 |
Correct |
26 ms |
16000 KB |
Output is correct |
54 |
Correct |
163 ms |
20320 KB |
Output is correct |
55 |
Correct |
19 ms |
15872 KB |
Output is correct |
56 |
Correct |
19 ms |
15872 KB |
Output is correct |
57 |
Correct |
19 ms |
15872 KB |
Output is correct |
58 |
Correct |
20 ms |
15872 KB |
Output is correct |
59 |
Correct |
17 ms |
15360 KB |
Output is correct |
60 |
Correct |
151 ms |
17912 KB |
Output is correct |
61 |
Correct |
18 ms |
15520 KB |
Output is correct |
62 |
Correct |
171 ms |
20728 KB |
Output is correct |
63 |
Correct |
626 ms |
64120 KB |
Output is correct |
64 |
Correct |
624 ms |
64248 KB |
Output is correct |
65 |
Correct |
624 ms |
64248 KB |
Output is correct |
66 |
Correct |
279 ms |
22136 KB |
Output is correct |
67 |
Correct |
460 ms |
28308 KB |
Output is correct |
68 |
Correct |
241 ms |
22136 KB |
Output is correct |
69 |
Correct |
463 ms |
28924 KB |
Output is correct |
70 |
Correct |
274 ms |
22264 KB |
Output is correct |
71 |
Correct |
278 ms |
22008 KB |
Output is correct |
72 |
Correct |
470 ms |
28664 KB |
Output is correct |
73 |
Correct |
470 ms |
28408 KB |
Output is correct |
74 |
Correct |
1094 ms |
38368 KB |
Output is correct |
75 |
Correct |
713 ms |
33812 KB |
Output is correct |
76 |
Correct |
878 ms |
38140 KB |
Output is correct |
77 |
Correct |
888 ms |
38340 KB |
Output is correct |
78 |
Correct |
274 ms |
28408 KB |
Output is correct |
79 |
Correct |
499 ms |
29944 KB |
Output is correct |
80 |
Correct |
264 ms |
28280 KB |
Output is correct |
81 |
Correct |
439 ms |
29944 KB |
Output is correct |
82 |
Correct |
1024 ms |
49528 KB |
Output is correct |
83 |
Correct |
1045 ms |
49564 KB |
Output is correct |
84 |
Correct |
738 ms |
49400 KB |
Output is correct |
85 |
Correct |
722 ms |
49400 KB |
Output is correct |
86 |
Correct |
354 ms |
22776 KB |
Output is correct |
87 |
Correct |
404 ms |
24056 KB |
Output is correct |
88 |
Correct |
506 ms |
28612 KB |
Output is correct |
89 |
Correct |
815 ms |
37884 KB |
Output is correct |