#include <bits/stdc++.h>
int findSwapPairs(int n, int s[], int m, int u[], int v[], int uu[], int vv[]) {
std::vector <int> a(s, s + n);
std::vector <int> o(s, s + n);
int l = 0, r = m;
std::vector <std::pair <int, int>> last;
while (l < r) {
int mid = (l + r) >> 1;
a = o;
std::vector <bool> vis(n, false);
std::vector <std::pair <int, int>> now;
for (int i = 0; i < mid; i++) {
std::swap(a[u[i]], a[v[i]]);
}
auto f = [&] (auto&& self, int ind, int root) -> void {
vis[ind] = true;
now.emplace_back(ind, a[ind]);
if (a[ind] != root) {
self(self, a[ind], root);
}
};
for (int i = 0; i < n; i++) {
if (!vis[i] && a[i] != i) {
f(f, a[i], i);
}
}
if ((int) now.size() > mid) {
l = mid + 1;
} else {
r = mid;
}
}
a = o;
std::vector <bool> vis(n, false);
auto f = [&] (auto&& self, int ind, int root) -> void {
vis[ind] = true;
last.emplace_back(ind, a[ind]);
if (a[ind] != root) {
self(self, a[ind], root);
}
};
for (int i = 0; i < l; i++) {
std::swap(a[u[i]], a[v[i]]);
}
for (int i = 0; i < n; i++) {
if (!vis[i] && a[i] != i) {
f(f, a[i], i);
}
}
a = o;
std::vector <int> inv(n);
for (int i = 0; i < n; i++) {
inv[a[i]] = i;
}
for (int i = 0; i < l; i++) {
std::swap(a[u[i]], a[v[i]]);
std::swap(inv[a[u[i]]], inv[a[v[i]]]);
if (i < (int) last.size()) {
uu[i] = inv[last[i].first];
vv[i] = inv[last[i].second];
std::swap(a[uu[i]], a[vv[i]]);
std::swap(inv[a[uu[i]]], inv[a[vv[i]]]);
} else {
uu[i] = vv[i] = 0;
}
}
return l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
0 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
0 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
444 KB |
Output is correct |
11 |
Correct |
2 ms |
368 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
444 KB |
Output is correct |
11 |
Correct |
2 ms |
368 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
496 KB |
Output is correct |
14 |
Correct |
1 ms |
436 KB |
Output is correct |
15 |
Correct |
167 ms |
16876 KB |
Output is correct |
16 |
Correct |
186 ms |
17360 KB |
Output is correct |
17 |
Correct |
197 ms |
18384 KB |
Output is correct |
18 |
Correct |
70 ms |
14516 KB |
Output is correct |
19 |
Correct |
139 ms |
15400 KB |
Output is correct |
20 |
Correct |
147 ms |
14696 KB |
Output is correct |
21 |
Correct |
154 ms |
15728 KB |
Output is correct |
22 |
Correct |
163 ms |
15804 KB |
Output is correct |
23 |
Correct |
220 ms |
18768 KB |
Output is correct |
24 |
Correct |
210 ms |
18564 KB |
Output is correct |
25 |
Correct |
211 ms |
18264 KB |
Output is correct |
26 |
Correct |
145 ms |
12752 KB |
Output is correct |
27 |
Correct |
136 ms |
12312 KB |
Output is correct |
28 |
Correct |
193 ms |
17876 KB |
Output is correct |
29 |
Correct |
200 ms |
14456 KB |
Output is correct |
30 |
Correct |
107 ms |
11940 KB |
Output is correct |
31 |
Correct |
186 ms |
14792 KB |
Output is correct |
32 |
Correct |
192 ms |
18312 KB |
Output is correct |
33 |
Correct |
214 ms |
18584 KB |
Output is correct |
34 |
Correct |
212 ms |
15160 KB |
Output is correct |
35 |
Correct |
138 ms |
14792 KB |
Output is correct |
36 |
Correct |
49 ms |
10676 KB |
Output is correct |
37 |
Correct |
213 ms |
19212 KB |
Output is correct |
38 |
Correct |
193 ms |
18552 KB |
Output is correct |
39 |
Correct |
194 ms |
18540 KB |
Output is correct |