#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> longest_trip(int N, int D) {
vector<int> rem(N);
iota(rem.rbegin(), rem.rend(), 0);
vector<int> path1;
vector<int> path2;
while (!rem.empty()) {
int u = rem.back();
rem.pop_back();
if (path1.empty()) {
path1.push_back(u);
continue;
}
if (path2.empty()) {
path2.push_back(u);
continue;
}
if (rem.empty()) {
if (are_connected({path1.back()}, {u})) {
path1.push_back(u);
continue;
}
if (are_connected({path2.back()}, {u})) {
path2.push_back(u);
continue;
}
path1.insert(path1.end(), path2.rbegin(), path2.rend());
path2 = {u};
continue;
}
int v = rem.back();
rem.pop_back();
if (are_connected({u}, {v})) {
if (are_connected({path1.back()}, {u})) {
path1.push_back(u);
path1.push_back(v);
continue;
}
if (are_connected({path2.back()}, {u})) {
path2.push_back(u);
path2.push_back(v);
continue;
}
path1.insert(path1.end(), path2.rbegin(), path2.rend());
path2 = {u, v};
}
else {
int nxt1 = (are_connected({path1.back()}, {u}) ? u : v);
int nxt2 = (are_connected({path2.back()}, {u}) ? u : v);
if (nxt1 == nxt2) {
path1.push_back(nxt1);
path1.insert(path1.end(), path2.rbegin(), path2.rend());
path2 = {u ^ v ^ nxt1};
}
else {
path1.push_back(nxt1);
path2.push_back(nxt2);
}
}
}
if (path1.empty()) {
return path2;
}
if (path2.empty()) {
return path1;
}
/* vector<pair<int, int>> ends = {{path1[0], path2[0]}, {path1.back(), path2[0]}, {path1[0], path2.back()}};
for (auto [u, v]: ends) {
if (are_connected({u}, {v})) {
if (path1.back() != u) {
reverse(path1.begin(), path1.end());
}
if (path2[0] != v) {
reverse(path2.begin(), path2.end());
}
path1.insert(path1.end(), path2.begin(), path2.end());
return path1;
}
}
if (!are_connected(path1, path2)) {*/
if (path1.size() > path2.size()) {
return path1;
}
else {
return path2;
}
//}
vector<int> cand1 = path1;
vector<int> cand2 = path2;
for (int iter = 0; iter < 2; iter++) {
while ((int)cand1.size() > 1) {
vector<int> left_cand(cand1.begin(), cand1.begin() + (int)cand1.size() / 2);
vector<int> right_cand(cand1.begin() + (int)cand1.size() / 2, cand1.end());
if (are_connected(left_cand, cand2)) {
cand1.swap(left_cand);
}
else {
cand1.swap(right_cand);
}
}
cand1.swap(cand2);
}
rotate(path1.begin(), path1.end(), find(path1.begin(), path1.end(), cand1[0]));
rotate(path2.begin(), path2.end(), find(path2.begin(), path2.end(), cand2[0]));
path1.insert(path1.begin(), path2.rbegin(), path2.rend());
return path1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
436 KB |
Output is correct |
6 |
Correct |
4 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
5 ms |
600 KB |
Output is correct |
23 |
Correct |
6 ms |
604 KB |
Output is correct |
24 |
Correct |
5 ms |
436 KB |
Output is correct |
25 |
Correct |
5 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
344 KB |
Output is correct |
37 |
Correct |
4 ms |
608 KB |
Output is correct |
38 |
Correct |
5 ms |
608 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
4 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
4 ms |
344 KB |
Output is correct |
48 |
Correct |
5 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
432 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
4 ms |
344 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
5 ms |
428 KB |
Output is correct |
56 |
Correct |
4 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
5 ms |
344 KB |
Output is correct |
59 |
Correct |
5 ms |
348 KB |
Output is correct |
60 |
Correct |
7 ms |
356 KB |
Output is correct |
61 |
Correct |
6 ms |
352 KB |
Output is correct |
62 |
Correct |
7 ms |
352 KB |
Output is correct |
63 |
Correct |
6 ms |
616 KB |
Output is correct |
64 |
Correct |
7 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |