#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int n;
int G[100001];
vector<int> L[100001];
set<pii> in[100001], out[100001];
int size(int x) {
return L[x].size() + in[x].size() + out[x].size();
}
bool exist(const set<pii> &sp, int g) {
auto it = sp.lower_bound(pii(g, 0));
return it != sp.end() && it->first == g;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) G[i] = i, L[i].push_back(i);
long long cnt = 0;
while (m--) {
int a, b;
cin >> a >> b;
if (G[a] == G[b] || in[G[b]].count(pii(G[a], a))) {
printf("%lld\n", cnt);
continue;
}
cnt += L[G[b]].size();
in[G[b]].emplace(G[a], a);
out[G[a]].emplace(G[b], a);
vector<pii> V;
if (exist(in[G[a]], G[b])) V.emplace_back(a, b);
while (!V.empty()) {
auto [a, b] = V.back();
V.pop_back();
a = G[a], b = G[b];
if (a == b) continue;
if (size(a) < size(b)) swap(a, b);
int sa = L[a].size(), sb = L[b].size();
cnt += (2ll * sa + int(in[a].size())) * sb;
for (int i : L[b]) {
G[i] = a;
L[a].push_back(i);
}
L[b].clear();
for (auto [g, i] : in[b]) {
out[g].erase(pii(b, i));
if (g == a) {
cnt -= sb;
}
else if (in[a].count(pii(g, i))) {
cnt -= sb;
continue;
}
else {
if (exist(out[a], g)) V.emplace_back(a, g);
cnt += sa;
out[g].emplace(a, i);
in[a].emplace(g, i);
}
}
in[b].clear();
for (auto [g, i] : out[b]) {
in[g].erase(pii(b, i));
if (g == a) {
cnt -= sa + sb;
}
else {
if (exist(in[a], g)) V.emplace_back(a, g);
in[g].emplace(a, i);
out[a].emplace(g, i);
}
}
out[b].clear();
}
printf("%lld\n", cnt);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12160 KB |
Output is correct |
2 |
Correct |
9 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
7 ms |
12032 KB |
Output is correct |
12 |
Correct |
7 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
7 ms |
12032 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
10 ms |
12032 KB |
Output is correct |
19 |
Correct |
7 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12032 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
7 ms |
12032 KB |
Output is correct |
27 |
Correct |
10 ms |
12160 KB |
Output is correct |
28 |
Correct |
8 ms |
12088 KB |
Output is correct |
29 |
Correct |
8 ms |
12032 KB |
Output is correct |
30 |
Correct |
10 ms |
12032 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
7 ms |
12032 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12160 KB |
Output is correct |
2 |
Correct |
9 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
7 ms |
12032 KB |
Output is correct |
12 |
Correct |
7 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
7 ms |
12032 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
10 ms |
12032 KB |
Output is correct |
19 |
Correct |
7 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12032 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
7 ms |
12032 KB |
Output is correct |
27 |
Correct |
10 ms |
12160 KB |
Output is correct |
28 |
Correct |
8 ms |
12088 KB |
Output is correct |
29 |
Correct |
8 ms |
12032 KB |
Output is correct |
30 |
Correct |
10 ms |
12032 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
7 ms |
12032 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
10 ms |
12288 KB |
Output is correct |
35 |
Correct |
99 ms |
17400 KB |
Output is correct |
36 |
Correct |
122 ms |
19520 KB |
Output is correct |
37 |
Correct |
124 ms |
19560 KB |
Output is correct |
38 |
Correct |
123 ms |
19348 KB |
Output is correct |
39 |
Correct |
10 ms |
12288 KB |
Output is correct |
40 |
Correct |
10 ms |
12416 KB |
Output is correct |
41 |
Correct |
10 ms |
12288 KB |
Output is correct |
42 |
Correct |
10 ms |
12288 KB |
Output is correct |
43 |
Correct |
10 ms |
12288 KB |
Output is correct |
44 |
Correct |
11 ms |
12288 KB |
Output is correct |
45 |
Correct |
13 ms |
12288 KB |
Output is correct |
46 |
Correct |
9 ms |
12288 KB |
Output is correct |
47 |
Correct |
10 ms |
12288 KB |
Output is correct |
48 |
Correct |
10 ms |
12288 KB |
Output is correct |
49 |
Correct |
16 ms |
12800 KB |
Output is correct |
50 |
Correct |
127 ms |
19516 KB |
Output is correct |
51 |
Correct |
14 ms |
12544 KB |
Output is correct |
52 |
Correct |
108 ms |
18424 KB |
Output is correct |
53 |
Correct |
16 ms |
12800 KB |
Output is correct |
54 |
Correct |
115 ms |
18936 KB |
Output is correct |
55 |
Correct |
12 ms |
12672 KB |
Output is correct |
56 |
Correct |
15 ms |
12544 KB |
Output is correct |
57 |
Correct |
16 ms |
12672 KB |
Output is correct |
58 |
Correct |
12 ms |
12672 KB |
Output is correct |
59 |
Correct |
10 ms |
12288 KB |
Output is correct |
60 |
Correct |
97 ms |
17272 KB |
Output is correct |
61 |
Correct |
11 ms |
12416 KB |
Output is correct |
62 |
Correct |
121 ms |
19192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12160 KB |
Output is correct |
2 |
Correct |
9 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
7 ms |
12032 KB |
Output is correct |
6 |
Correct |
7 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
7 ms |
12032 KB |
Output is correct |
12 |
Correct |
7 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
7 ms |
12032 KB |
Output is correct |
15 |
Correct |
7 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
7 ms |
12032 KB |
Output is correct |
18 |
Correct |
10 ms |
12032 KB |
Output is correct |
19 |
Correct |
7 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12032 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
7 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
7 ms |
12032 KB |
Output is correct |
27 |
Correct |
10 ms |
12160 KB |
Output is correct |
28 |
Correct |
8 ms |
12088 KB |
Output is correct |
29 |
Correct |
8 ms |
12032 KB |
Output is correct |
30 |
Correct |
10 ms |
12032 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
7 ms |
12032 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
10 ms |
12288 KB |
Output is correct |
35 |
Correct |
99 ms |
17400 KB |
Output is correct |
36 |
Correct |
122 ms |
19520 KB |
Output is correct |
37 |
Correct |
124 ms |
19560 KB |
Output is correct |
38 |
Correct |
123 ms |
19348 KB |
Output is correct |
39 |
Correct |
10 ms |
12288 KB |
Output is correct |
40 |
Correct |
10 ms |
12416 KB |
Output is correct |
41 |
Correct |
10 ms |
12288 KB |
Output is correct |
42 |
Correct |
10 ms |
12288 KB |
Output is correct |
43 |
Correct |
10 ms |
12288 KB |
Output is correct |
44 |
Correct |
11 ms |
12288 KB |
Output is correct |
45 |
Correct |
13 ms |
12288 KB |
Output is correct |
46 |
Correct |
9 ms |
12288 KB |
Output is correct |
47 |
Correct |
10 ms |
12288 KB |
Output is correct |
48 |
Correct |
10 ms |
12288 KB |
Output is correct |
49 |
Correct |
16 ms |
12800 KB |
Output is correct |
50 |
Correct |
127 ms |
19516 KB |
Output is correct |
51 |
Correct |
14 ms |
12544 KB |
Output is correct |
52 |
Correct |
108 ms |
18424 KB |
Output is correct |
53 |
Correct |
16 ms |
12800 KB |
Output is correct |
54 |
Correct |
115 ms |
18936 KB |
Output is correct |
55 |
Correct |
12 ms |
12672 KB |
Output is correct |
56 |
Correct |
15 ms |
12544 KB |
Output is correct |
57 |
Correct |
16 ms |
12672 KB |
Output is correct |
58 |
Correct |
12 ms |
12672 KB |
Output is correct |
59 |
Correct |
10 ms |
12288 KB |
Output is correct |
60 |
Correct |
97 ms |
17272 KB |
Output is correct |
61 |
Correct |
11 ms |
12416 KB |
Output is correct |
62 |
Correct |
121 ms |
19192 KB |
Output is correct |
63 |
Correct |
453 ms |
49272 KB |
Output is correct |
64 |
Correct |
444 ms |
49272 KB |
Output is correct |
65 |
Correct |
477 ms |
49272 KB |
Output is correct |
66 |
Correct |
148 ms |
20592 KB |
Output is correct |
67 |
Correct |
305 ms |
26224 KB |
Output is correct |
68 |
Correct |
153 ms |
20592 KB |
Output is correct |
69 |
Correct |
358 ms |
25068 KB |
Output is correct |
70 |
Correct |
154 ms |
20592 KB |
Output is correct |
71 |
Correct |
150 ms |
20588 KB |
Output is correct |
72 |
Correct |
304 ms |
25204 KB |
Output is correct |
73 |
Correct |
298 ms |
25204 KB |
Output is correct |
74 |
Correct |
775 ms |
33524 KB |
Output is correct |
75 |
Correct |
513 ms |
29932 KB |
Output is correct |
76 |
Correct |
585 ms |
32828 KB |
Output is correct |
77 |
Correct |
605 ms |
32880 KB |
Output is correct |
78 |
Correct |
176 ms |
23796 KB |
Output is correct |
79 |
Correct |
325 ms |
27508 KB |
Output is correct |
80 |
Correct |
196 ms |
23800 KB |
Output is correct |
81 |
Correct |
335 ms |
27248 KB |
Output is correct |
82 |
Correct |
605 ms |
38644 KB |
Output is correct |
83 |
Correct |
566 ms |
38772 KB |
Output is correct |
84 |
Correct |
510 ms |
38784 KB |
Output is correct |
85 |
Correct |
489 ms |
38644 KB |
Output is correct |
86 |
Correct |
183 ms |
25072 KB |
Output is correct |
87 |
Correct |
215 ms |
27116 KB |
Output is correct |
88 |
Correct |
313 ms |
25336 KB |
Output is correct |
89 |
Correct |
568 ms |
32492 KB |
Output is correct |