#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())
#ifdef LOCAL
#include <print.h>
#else
#define trace(...)
#define endl "\n" // remove in interactive
#endif
vector<vector<int>> remove(vector<vector<int>> P, int a, int b){
vector<vector<int>> ret;
for(int i = 0; i < sz(P); i++) if(i != a && i != b) ret.push_back(P[i]);
return ret;
}
vector<int> longest_trip(int n, int d){
vector<vector<int>> paths;
vector<int> nodes(n), b(n - 1);
iota(all(nodes), 0);
random_shuffle(all(nodes));
// for(int i = 0; i < n - 1; i++){
// b[i] = are_connected({nodes[i]}, {nodes[i + 1]});
// }
// vector<int> vis(n);
// for(int i = 0; i < n; i++) if(!vis[i]){
// int j = i;
// vector<int> path;
// while(j < n){
// path.push_back(nodes[j]);
// vis[j] = true;
// if(j == n - 1) break;
// if(j < n - 1 && b[j]){
// j++;
// continue;
// } else if(j < n - 2 && !b[j + 1]){
// j += 2;
// continue;
// }
// break;
// }
// paths.push_back(path);
// }
for(int i = 0; i < n; i++) paths.push_back({i});
if(sz(paths) == 1) return paths[0];
// return {};
while(sz(paths) > 2){
int k = sz(paths);
int p = rand() % k, q = rand() % k, r = rand() % k;
while(p == q || q == r || p == r){
p = rand() % k, q = rand() % k, r = rand() % k;
}
int pp = p, qq = q;
if(!are_connected({paths[p][0]}, {paths[q][0]})){
if(rand() % 2) swap(p, q);
if(are_connected({paths[q][0]}, {paths[r][0]})){
pp = q; qq = r;
} else{
// qq = r;
pp = p; qq = r;
}
}
vector<int> newpath = paths[pp];
reverse(all(newpath));
copy(all(paths[qq]), back_inserter(newpath));
paths = remove(paths, pp, qq);
paths.push_back(newpath);
}
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
if(are_connected({paths[0][0]}, {paths[1][0]})){
vector<int> Q = paths[0];
reverse(all(Q));
copy(all(paths[1]), back_inserter(Q));
return Q;
}
reverse(all(paths[1]));
}
reverse(all(paths[0]));
}
vector<int> P = paths[0], Q = paths[1];
if(are_connected(paths[0], paths[1])){
for(int i = 0; i < 2; i++){
while(sz(paths[i]) > 1){
int k = sz(paths[i]) / 2;
vector<int> lft(paths[i].begin(), paths[i].begin() + k);
vector<int> rgt(paths[i].begin() + k, paths[i].end());
if(are_connected(lft, paths[i ^ 1])){
paths[i] = lft;
} else paths[i] = rgt;
}
}
// assert(are_connected({paths[0][0]}, {paths[1][0]}));
vector<int> final_path;
int posP = find(all(P), paths[0][0]) - P.begin();
int posQ = find(all(Q), paths[1][0]) - Q.begin();
posP = (posP + 1) % sz(P);
rotate(P.begin(), P.begin() + posP, P.end());
rotate(Q.begin(), Q.begin() + posQ, Q.end());
copy(all(Q), back_inserter(P));
return P;
}
return sz(paths[0]) > sz(paths[1]) ? paths[0] : paths[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
6 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
336 KB |
Output is correct |
4 |
Correct |
17 ms |
352 KB |
Output is correct |
5 |
Correct |
27 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
8 ms |
208 KB |
Output is correct |
3 |
Correct |
12 ms |
308 KB |
Output is correct |
4 |
Correct |
18 ms |
356 KB |
Output is correct |
5 |
Correct |
24 ms |
380 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
304 KB |
Output is correct |
9 |
Correct |
16 ms |
336 KB |
Output is correct |
10 |
Correct |
20 ms |
364 KB |
Output is correct |
11 |
Correct |
22 ms |
368 KB |
Output is correct |
12 |
Correct |
22 ms |
372 KB |
Output is correct |
13 |
Correct |
24 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
13 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
16 ms |
352 KB |
Output is correct |
5 |
Correct |
24 ms |
372 KB |
Output is correct |
6 |
Correct |
7 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
336 KB |
Output is correct |
9 |
Correct |
14 ms |
356 KB |
Output is correct |
10 |
Correct |
24 ms |
380 KB |
Output is correct |
11 |
Correct |
25 ms |
380 KB |
Output is correct |
12 |
Correct |
21 ms |
336 KB |
Output is correct |
13 |
Correct |
26 ms |
364 KB |
Output is correct |
14 |
Correct |
13 ms |
208 KB |
Output is correct |
15 |
Correct |
10 ms |
208 KB |
Output is correct |
16 |
Correct |
11 ms |
208 KB |
Output is correct |
17 |
Correct |
14 ms |
208 KB |
Output is correct |
18 |
Correct |
11 ms |
336 KB |
Output is correct |
19 |
Correct |
17 ms |
332 KB |
Output is correct |
20 |
Correct |
18 ms |
360 KB |
Output is correct |
21 |
Correct |
28 ms |
380 KB |
Output is correct |
22 |
Correct |
24 ms |
368 KB |
Output is correct |
23 |
Correct |
19 ms |
380 KB |
Output is correct |
24 |
Correct |
21 ms |
376 KB |
Output is correct |
25 |
Correct |
15 ms |
208 KB |
Output is correct |
26 |
Correct |
19 ms |
208 KB |
Output is correct |
27 |
Correct |
19 ms |
256 KB |
Output is correct |
28 |
Correct |
14 ms |
208 KB |
Output is correct |
29 |
Correct |
17 ms |
300 KB |
Output is correct |
30 |
Correct |
12 ms |
332 KB |
Output is correct |
31 |
Correct |
17 ms |
336 KB |
Output is correct |
32 |
Correct |
10 ms |
356 KB |
Output is correct |
33 |
Correct |
14 ms |
352 KB |
Output is correct |
34 |
Correct |
14 ms |
352 KB |
Output is correct |
35 |
Correct |
13 ms |
352 KB |
Output is correct |
36 |
Correct |
22 ms |
376 KB |
Output is correct |
37 |
Correct |
24 ms |
364 KB |
Output is correct |
38 |
Correct |
22 ms |
376 KB |
Output is correct |
39 |
Correct |
23 ms |
364 KB |
Output is correct |
40 |
Correct |
21 ms |
352 KB |
Output is correct |
41 |
Correct |
22 ms |
376 KB |
Output is correct |
42 |
Correct |
26 ms |
356 KB |
Output is correct |
43 |
Correct |
35 ms |
376 KB |
Output is correct |
44 |
Correct |
27 ms |
376 KB |
Output is correct |
45 |
Correct |
9 ms |
212 KB |
Output is correct |
46 |
Correct |
12 ms |
208 KB |
Output is correct |
47 |
Correct |
14 ms |
208 KB |
Output is correct |
48 |
Correct |
12 ms |
212 KB |
Output is correct |
49 |
Correct |
15 ms |
208 KB |
Output is correct |
50 |
Correct |
14 ms |
336 KB |
Output is correct |
51 |
Correct |
19 ms |
336 KB |
Output is correct |
52 |
Correct |
21 ms |
316 KB |
Output is correct |
53 |
Correct |
17 ms |
332 KB |
Output is correct |
54 |
Correct |
16 ms |
352 KB |
Output is correct |
55 |
Correct |
18 ms |
384 KB |
Output is correct |
56 |
Correct |
26 ms |
384 KB |
Output is correct |
57 |
Correct |
31 ms |
360 KB |
Output is correct |
58 |
Correct |
31 ms |
372 KB |
Output is correct |
59 |
Correct |
28 ms |
472 KB |
Output is correct |
60 |
Correct |
19 ms |
348 KB |
Output is correct |
61 |
Correct |
27 ms |
384 KB |
Output is correct |
62 |
Correct |
24 ms |
364 KB |
Output is correct |
63 |
Correct |
28 ms |
364 KB |
Output is correct |
64 |
Correct |
30 ms |
372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
12 ms |
340 KB |
Output is correct |
4 |
Correct |
16 ms |
356 KB |
Output is correct |
5 |
Correct |
25 ms |
360 KB |
Output is correct |
6 |
Correct |
14 ms |
208 KB |
Output is correct |
7 |
Correct |
12 ms |
208 KB |
Output is correct |
8 |
Correct |
12 ms |
208 KB |
Output is correct |
9 |
Correct |
13 ms |
328 KB |
Output is correct |
10 |
Correct |
22 ms |
352 KB |
Output is correct |
11 |
Correct |
27 ms |
380 KB |
Output is correct |
12 |
Correct |
20 ms |
368 KB |
Output is correct |
13 |
Correct |
21 ms |
356 KB |
Output is correct |
14 |
Correct |
17 ms |
256 KB |
Output is correct |
15 |
Correct |
16 ms |
256 KB |
Output is correct |
16 |
Correct |
17 ms |
212 KB |
Output is correct |
17 |
Correct |
8 ms |
220 KB |
Output is correct |
18 |
Correct |
14 ms |
304 KB |
Output is correct |
19 |
Correct |
18 ms |
360 KB |
Output is correct |
20 |
Correct |
15 ms |
380 KB |
Output is correct |
21 |
Correct |
23 ms |
208 KB |
Output is correct |
22 |
Correct |
23 ms |
236 KB |
Output is correct |
23 |
Correct |
16 ms |
212 KB |
Output is correct |
24 |
Correct |
14 ms |
296 KB |
Output is correct |
25 |
Correct |
11 ms |
220 KB |
Output is correct |
26 |
Correct |
10 ms |
336 KB |
Output is correct |
27 |
Correct |
12 ms |
344 KB |
Output is correct |
28 |
Correct |
16 ms |
328 KB |
Output is correct |
29 |
Correct |
20 ms |
340 KB |
Output is correct |
30 |
Correct |
15 ms |
464 KB |
Output is correct |
31 |
Correct |
16 ms |
340 KB |
Output is correct |
32 |
Correct |
15 ms |
256 KB |
Output is correct |
33 |
Correct |
16 ms |
208 KB |
Output is correct |
34 |
Correct |
16 ms |
208 KB |
Output is correct |
35 |
Correct |
15 ms |
304 KB |
Output is correct |
36 |
Correct |
10 ms |
208 KB |
Output is correct |
37 |
Correct |
14 ms |
336 KB |
Output is correct |
38 |
Correct |
18 ms |
340 KB |
Output is correct |
39 |
Correct |
16 ms |
336 KB |
Output is correct |
40 |
Correct |
18 ms |
336 KB |
Output is correct |
41 |
Correct |
18 ms |
348 KB |
Output is correct |
42 |
Correct |
17 ms |
336 KB |
Output is correct |
43 |
Correct |
23 ms |
380 KB |
Output is correct |
44 |
Correct |
22 ms |
348 KB |
Output is correct |
45 |
Correct |
28 ms |
368 KB |
Output is correct |
46 |
Correct |
22 ms |
356 KB |
Output is correct |
47 |
Correct |
24 ms |
504 KB |
Output is correct |
48 |
Correct |
18 ms |
396 KB |
Output is correct |
49 |
Correct |
21 ms |
364 KB |
Output is correct |
50 |
Correct |
32 ms |
376 KB |
Output is correct |
51 |
Correct |
29 ms |
364 KB |
Output is correct |
52 |
Correct |
26 ms |
364 KB |
Output is correct |
53 |
Correct |
28 ms |
492 KB |
Output is correct |
54 |
Correct |
26 ms |
368 KB |
Output is correct |
55 |
Partially correct |
25 ms |
360 KB |
Output is partially correct |
56 |
Correct |
20 ms |
352 KB |
Output is correct |
57 |
Correct |
23 ms |
392 KB |
Output is correct |
58 |
Correct |
31 ms |
356 KB |
Output is correct |
59 |
Correct |
25 ms |
372 KB |
Output is correct |
60 |
Correct |
23 ms |
380 KB |
Output is correct |
61 |
Correct |
26 ms |
384 KB |
Output is correct |
62 |
Correct |
21 ms |
392 KB |
Output is correct |
63 |
Correct |
22 ms |
388 KB |
Output is correct |
64 |
Correct |
25 ms |
368 KB |
Output is correct |
65 |
Partially correct |
23 ms |
336 KB |
Output is partially correct |
66 |
Partially correct |
25 ms |
508 KB |
Output is partially correct |
67 |
Partially correct |
27 ms |
340 KB |
Output is partially correct |
68 |
Correct |
26 ms |
368 KB |
Output is correct |
69 |
Partially correct |
21 ms |
364 KB |
Output is partially correct |
70 |
Partially correct |
26 ms |
368 KB |
Output is partially correct |
71 |
Correct |
21 ms |
348 KB |
Output is correct |
72 |
Correct |
24 ms |
360 KB |
Output is correct |
73 |
Partially correct |
27 ms |
364 KB |
Output is partially correct |
74 |
Correct |
26 ms |
372 KB |
Output is correct |
75 |
Partially correct |
27 ms |
352 KB |
Output is partially correct |
76 |
Partially correct |
25 ms |
464 KB |
Output is partially correct |
77 |
Correct |
28 ms |
372 KB |
Output is correct |
78 |
Correct |
30 ms |
364 KB |
Output is correct |
79 |
Partially correct |
24 ms |
384 KB |
Output is partially correct |
80 |
Partially correct |
32 ms |
352 KB |
Output is partially correct |
81 |
Partially correct |
25 ms |
372 KB |
Output is partially correct |
82 |
Partially correct |
27 ms |
364 KB |
Output is partially correct |