#include <bits/stdc++.h>
int main() {
using namespace std;
typedef long long ll;
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
ll ans = 0;
vector<int> label(n);
iota(label.begin(), label.end(), 0);
vector<int> size(n, 1);
vector<set<int>> in(n);
vector<set<pair<int, int>>> out(n);
function<int(int)> get = [&](int u) -> int {
if (label[u] == u)
return u;
return label[u] = get(label[u]);
};
function<void(int, int)> direct = [&](int u, int v) {
int x = get(u);
int y = get(v);
if (x == y)
return;
auto it = out[y].lower_bound({x, 0});
if (it != out[y].end() && (*it).first == x) {
if (in[x].size() + out[x].size() < in[y].size() + out[y].size())
swap(x, y);
vector<pair<int, int>> vout;
for (auto& e : out[y]) {
ans -= size[e.first];
in[e.first].erase(e.second);
if (e.first != x)
vout.push_back(e);
}
out[y].clear();
ans += 1LL * size[y] * (2 * size[x] + in[x].size() - in[y].size());
vector<int> vin;
for (auto& e : in[y]) {
int j = get(e);
out[j].erase({y, e});
if (j != x)
vin.push_back(e);
}
in[y].clear();
label[y] = x;
size[x] += size[y];
for (int& e : vin)
direct(e, x);
for (auto& e : vout)
direct(e.second, e.first);
} else if (!in[y].count(u)) {
ans += size[y];
in[y].insert(u);
out[x].insert({y, u});
}
};
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
direct(u, v);
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
7 ms |
364 KB |
Output is correct |
9 |
Correct |
7 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
8 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
364 KB |
Output is correct |
25 |
Correct |
7 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
7 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
7 ms |
364 KB |
Output is correct |
9 |
Correct |
7 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
8 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
364 KB |
Output is correct |
25 |
Correct |
7 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
7 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
25 ms |
492 KB |
Output is correct |
35 |
Correct |
749 ms |
6032 KB |
Output is correct |
36 |
Correct |
767 ms |
8192 KB |
Output is correct |
37 |
Correct |
794 ms |
8172 KB |
Output is correct |
38 |
Correct |
768 ms |
8044 KB |
Output is correct |
39 |
Correct |
11 ms |
892 KB |
Output is correct |
40 |
Correct |
12 ms |
748 KB |
Output is correct |
41 |
Correct |
12 ms |
748 KB |
Output is correct |
42 |
Correct |
12 ms |
620 KB |
Output is correct |
43 |
Correct |
13 ms |
748 KB |
Output is correct |
44 |
Correct |
12 ms |
748 KB |
Output is correct |
45 |
Correct |
11 ms |
640 KB |
Output is correct |
46 |
Correct |
12 ms |
620 KB |
Output is correct |
47 |
Correct |
12 ms |
748 KB |
Output is correct |
48 |
Correct |
14 ms |
748 KB |
Output is correct |
49 |
Correct |
32 ms |
1260 KB |
Output is correct |
50 |
Correct |
772 ms |
8428 KB |
Output is correct |
51 |
Correct |
29 ms |
876 KB |
Output is correct |
52 |
Correct |
757 ms |
7148 KB |
Output is correct |
53 |
Correct |
32 ms |
1260 KB |
Output is correct |
54 |
Correct |
756 ms |
7788 KB |
Output is correct |
55 |
Correct |
14 ms |
1004 KB |
Output is correct |
56 |
Correct |
14 ms |
1004 KB |
Output is correct |
57 |
Correct |
18 ms |
1004 KB |
Output is correct |
58 |
Correct |
14 ms |
1152 KB |
Output is correct |
59 |
Correct |
12 ms |
620 KB |
Output is correct |
60 |
Correct |
768 ms |
5612 KB |
Output is correct |
61 |
Correct |
14 ms |
748 KB |
Output is correct |
62 |
Correct |
766 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
7 ms |
364 KB |
Output is correct |
8 |
Correct |
7 ms |
364 KB |
Output is correct |
9 |
Correct |
7 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
8 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
364 KB |
Output is correct |
25 |
Correct |
7 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
7 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
25 ms |
492 KB |
Output is correct |
35 |
Correct |
749 ms |
6032 KB |
Output is correct |
36 |
Correct |
767 ms |
8192 KB |
Output is correct |
37 |
Correct |
794 ms |
8172 KB |
Output is correct |
38 |
Correct |
768 ms |
8044 KB |
Output is correct |
39 |
Correct |
11 ms |
892 KB |
Output is correct |
40 |
Correct |
12 ms |
748 KB |
Output is correct |
41 |
Correct |
12 ms |
748 KB |
Output is correct |
42 |
Correct |
12 ms |
620 KB |
Output is correct |
43 |
Correct |
13 ms |
748 KB |
Output is correct |
44 |
Correct |
12 ms |
748 KB |
Output is correct |
45 |
Correct |
11 ms |
640 KB |
Output is correct |
46 |
Correct |
12 ms |
620 KB |
Output is correct |
47 |
Correct |
12 ms |
748 KB |
Output is correct |
48 |
Correct |
14 ms |
748 KB |
Output is correct |
49 |
Correct |
32 ms |
1260 KB |
Output is correct |
50 |
Correct |
772 ms |
8428 KB |
Output is correct |
51 |
Correct |
29 ms |
876 KB |
Output is correct |
52 |
Correct |
757 ms |
7148 KB |
Output is correct |
53 |
Correct |
32 ms |
1260 KB |
Output is correct |
54 |
Correct |
756 ms |
7788 KB |
Output is correct |
55 |
Correct |
14 ms |
1004 KB |
Output is correct |
56 |
Correct |
14 ms |
1004 KB |
Output is correct |
57 |
Correct |
18 ms |
1004 KB |
Output is correct |
58 |
Correct |
14 ms |
1152 KB |
Output is correct |
59 |
Correct |
12 ms |
620 KB |
Output is correct |
60 |
Correct |
768 ms |
5612 KB |
Output is correct |
61 |
Correct |
14 ms |
748 KB |
Output is correct |
62 |
Correct |
766 ms |
8172 KB |
Output is correct |
63 |
Correct |
1107 ms |
44484 KB |
Output is correct |
64 |
Correct |
1115 ms |
44248 KB |
Output is correct |
65 |
Correct |
1130 ms |
44224 KB |
Output is correct |
66 |
Correct |
554 ms |
15084 KB |
Output is correct |
67 |
Correct |
684 ms |
19052 KB |
Output is correct |
68 |
Correct |
553 ms |
15084 KB |
Output is correct |
69 |
Correct |
762 ms |
19564 KB |
Output is correct |
70 |
Correct |
602 ms |
15216 KB |
Output is correct |
71 |
Correct |
593 ms |
14956 KB |
Output is correct |
72 |
Correct |
709 ms |
19024 KB |
Output is correct |
73 |
Correct |
716 ms |
19052 KB |
Output is correct |
74 |
Correct |
1461 ms |
28396 KB |
Output is correct |
75 |
Correct |
1187 ms |
24428 KB |
Output is correct |
76 |
Correct |
1242 ms |
27244 KB |
Output is correct |
77 |
Correct |
1246 ms |
27500 KB |
Output is correct |
78 |
Correct |
568 ms |
18704 KB |
Output is correct |
79 |
Correct |
1016 ms |
22380 KB |
Output is correct |
80 |
Correct |
548 ms |
18660 KB |
Output is correct |
81 |
Correct |
983 ms |
21868 KB |
Output is correct |
82 |
Correct |
956 ms |
33100 KB |
Output is correct |
83 |
Correct |
942 ms |
33004 KB |
Output is correct |
84 |
Correct |
896 ms |
33320 KB |
Output is correct |
85 |
Correct |
886 ms |
33388 KB |
Output is correct |
86 |
Correct |
564 ms |
14372 KB |
Output is correct |
87 |
Correct |
821 ms |
16388 KB |
Output is correct |
88 |
Correct |
691 ms |
19052 KB |
Output is correct |
89 |
Correct |
1232 ms |
26732 KB |
Output is correct |