#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
// Subtask 1: D == 3
vector<int> sub1(int n) {
vector<int> res(n);
std::iota(res.begin(), res.end(), 0);
return res;
}
// Subtask 2: D == 2
bool has_edge(int u, int v) {
return are_connected({u}, {v});
}
vector<int> sub2(int n) {
vector<int> res {0};
for (int i = 1; i < n; i++) {
if (has_edge(0, i)) {
res.push_back(i);
break;
}
}
for (int i = 1; i < n; i++) {
if (i == res[1]) continue;
if (has_edge(res.back(), i)) {
res.push_back(i);
} else {
res.insert(res.begin(), i);
}
}
return res;
}
// Subtask 3: D == 1, return path with length >= Lmax / 2
vector<int> sub3(int n) {
// maintain 2 paths
vector<int> p1 {0}, p2 {1};
for (int i = 2; i < n; ++i) {
// consider p1.back(), p2.back(), i
// -> there's at least 1 edge
if (has_edge(p1.back(), i)) {
p1.push_back(i);
} else if (has_edge(p2.back(), i)) {
p2.push_back(i);
} else {
reverse(p2.begin(), p2.end());
p1.insert(p1.end(), p2.begin(), p2.end());
p2 = {i};
}
}
if (p1.size() > p2.size()) return p1;
return p2;
}
// Subtask 4: D == 1
vector<int> sub4(int n) {
// Use subtask 3 to get 2 paths {{{
vector<int> p1 {0}, p2 {1};
for (int i = 2; i < n; ++i) {
if (has_edge(p1.back(), i)) {
p1.push_back(i);
} else if (has_edge(p2.back(), i)) {
p2.push_back(i);
} else {
reverse(p2.begin(), p2.end());
p1.insert(p1.end(), p2.begin(), p2.end());
p2 = {i};
}
}
// }}}
// Make p1 longer path
if (p1.size() < p2.size()) swap(p1, p2);
if (p2.empty()) return p1;
// Impossible to merge 2 paths
if (!are_connected(p1, p2)) {
return p1;
}
// Merge 2 paths:
// Consider: p1[0], p1.back() and p2[0] -> at least 1 edge
if (has_edge(p1.back(), p2[0])) {
p1.insert(p1.end(), p2.begin(), p2.end());
return p1;
} else if (has_edge(p1[0], p2[0])) {
reverse(p2.begin(), p2.end());
p2.insert(p2.end(), p1.begin(), p1.end());
return p2;
}
// p1 is now a circle
// corner case: |p2| == 1
if (p2.size() == 1) {
for (int i = 0; i < p1.size(); ++i) {
if (has_edge(p1[i], p2[0])) {
reverse(p2.begin(), p2.end());
p2.insert(p2.end(), p1.begin() + i, p1.end());
p2.insert(p2.end(), p1.begin(), p1.begin() + i);
return p2;
}
}
}
// Consider: p2[0], p2.back() and p1[0] -> at least 1 edge
if (has_edge(p2[0], p1[0])) {
reverse(p1.begin(), p1.end());
p1.insert(p1.end(), p2.begin(), p2.end());
return p1;
} else if (has_edge(p2.back(), p1[0])) {
p2.insert(p2.end(), p1.begin(), p1.end());
return p2;
}
// p2 is now also a circle
// Find one edge from p1 -> p2
int l1 = 0, r1 = p1.size() - 1; // p1[l1..r1] has at least one edge to p2
while (l1 < r1) {
int mid = (l1 + r1) / 2;
if (are_connected(vector<int> (p1.begin() + l1, p1.begin() + mid + 1), p2)) {
r1 = mid;
} else l1 = mid + 1;
}
for (int i = l1; i <= l1; i++) {
for (int j = 0; j < p2.size(); j++) {
if (has_edge(p1[i], p2[j])) {
vector<int> res;
res.insert(res.end(), p1.begin() + i + 1, p1.end());
res.insert(res.end(), p1.begin(), p1.begin() + i + 1);
res.insert(res.end(), p2.begin() + j, p2.end());
res.insert(res.end(), p2.begin(), p2.begin() + j);
return res;
}
}
}
return p1;
}
vector<int> longest_trip(int n, int d) {
if (d == 3) return sub1(n);
if (d == 2) return sub2(n);
return sub4(n);
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> sub4(int)':
longesttrip.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int i = 0; i < p1.size(); ++i) {
| ~~^~~~~~~~~~~
longesttrip.cpp:133:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for (int j = 0; j < p2.size(); j++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
260 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
9 ms |
208 KB |
Output is correct |
6 |
Correct |
9 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
208 KB |
Output is correct |
9 |
Correct |
9 ms |
208 KB |
Output is correct |
10 |
Correct |
6 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
5 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
10 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
208 KB |
Output is correct |
6 |
Correct |
14 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
10 ms |
292 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
9 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
208 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
11 ms |
208 KB |
Output is correct |
15 |
Correct |
9 ms |
208 KB |
Output is correct |
16 |
Correct |
12 ms |
208 KB |
Output is correct |
17 |
Correct |
10 ms |
332 KB |
Output is correct |
18 |
Correct |
11 ms |
304 KB |
Output is correct |
19 |
Correct |
11 ms |
208 KB |
Output is correct |
20 |
Correct |
9 ms |
208 KB |
Output is correct |
21 |
Correct |
9 ms |
360 KB |
Output is correct |
22 |
Correct |
7 ms |
208 KB |
Output is correct |
23 |
Correct |
6 ms |
208 KB |
Output is correct |
24 |
Correct |
6 ms |
304 KB |
Output is correct |
25 |
Correct |
10 ms |
208 KB |
Output is correct |
26 |
Correct |
10 ms |
208 KB |
Output is correct |
27 |
Correct |
8 ms |
208 KB |
Output is correct |
28 |
Correct |
11 ms |
208 KB |
Output is correct |
29 |
Correct |
9 ms |
208 KB |
Output is correct |
30 |
Correct |
6 ms |
208 KB |
Output is correct |
31 |
Correct |
13 ms |
208 KB |
Output is correct |
32 |
Correct |
13 ms |
208 KB |
Output is correct |
33 |
Correct |
5 ms |
208 KB |
Output is correct |
34 |
Correct |
7 ms |
208 KB |
Output is correct |
35 |
Correct |
8 ms |
208 KB |
Output is correct |
36 |
Correct |
7 ms |
208 KB |
Output is correct |
37 |
Correct |
7 ms |
208 KB |
Output is correct |
38 |
Correct |
8 ms |
208 KB |
Output is correct |
39 |
Correct |
5 ms |
208 KB |
Output is correct |
40 |
Correct |
11 ms |
208 KB |
Output is correct |
41 |
Correct |
9 ms |
208 KB |
Output is correct |
42 |
Correct |
9 ms |
208 KB |
Output is correct |
43 |
Correct |
14 ms |
208 KB |
Output is correct |
44 |
Correct |
13 ms |
208 KB |
Output is correct |
45 |
Correct |
10 ms |
208 KB |
Output is correct |
46 |
Correct |
17 ms |
208 KB |
Output is correct |
47 |
Correct |
10 ms |
208 KB |
Output is correct |
48 |
Correct |
15 ms |
208 KB |
Output is correct |
49 |
Correct |
9 ms |
260 KB |
Output is correct |
50 |
Correct |
13 ms |
208 KB |
Output is correct |
51 |
Correct |
9 ms |
208 KB |
Output is correct |
52 |
Correct |
12 ms |
208 KB |
Output is correct |
53 |
Correct |
9 ms |
208 KB |
Output is correct |
54 |
Correct |
11 ms |
208 KB |
Output is correct |
55 |
Correct |
9 ms |
300 KB |
Output is correct |
56 |
Correct |
10 ms |
208 KB |
Output is correct |
57 |
Correct |
6 ms |
340 KB |
Output is correct |
58 |
Correct |
14 ms |
300 KB |
Output is correct |
59 |
Correct |
13 ms |
208 KB |
Output is correct |
60 |
Correct |
15 ms |
208 KB |
Output is correct |
61 |
Correct |
10 ms |
304 KB |
Output is correct |
62 |
Correct |
7 ms |
428 KB |
Output is correct |
63 |
Correct |
11 ms |
208 KB |
Output is correct |
64 |
Correct |
8 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
304 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
10 ms |
208 KB |
Output is correct |
9 |
Correct |
7 ms |
208 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
9 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
332 KB |
Output is correct |
13 |
Correct |
7 ms |
208 KB |
Output is correct |
14 |
Correct |
12 ms |
208 KB |
Output is correct |
15 |
Correct |
8 ms |
208 KB |
Output is correct |
16 |
Correct |
13 ms |
208 KB |
Output is correct |
17 |
Correct |
10 ms |
332 KB |
Output is correct |
18 |
Correct |
8 ms |
208 KB |
Output is correct |
19 |
Correct |
7 ms |
208 KB |
Output is correct |
20 |
Correct |
5 ms |
208 KB |
Output is correct |
21 |
Correct |
6 ms |
332 KB |
Output is correct |
22 |
Correct |
10 ms |
208 KB |
Output is correct |
23 |
Correct |
5 ms |
208 KB |
Output is correct |
24 |
Correct |
8 ms |
208 KB |
Output is correct |
25 |
Correct |
8 ms |
208 KB |
Output is correct |
26 |
Correct |
7 ms |
208 KB |
Output is correct |
27 |
Correct |
5 ms |
208 KB |
Output is correct |
28 |
Correct |
11 ms |
208 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
8 ms |
208 KB |
Output is correct |
31 |
Correct |
7 ms |
208 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Correct |
15 ms |
208 KB |
Output is correct |
34 |
Correct |
13 ms |
208 KB |
Output is correct |
35 |
Correct |
11 ms |
208 KB |
Output is correct |
36 |
Correct |
12 ms |
332 KB |
Output is correct |
37 |
Correct |
13 ms |
208 KB |
Output is correct |
38 |
Correct |
9 ms |
300 KB |
Output is correct |
39 |
Correct |
14 ms |
208 KB |
Output is correct |
40 |
Correct |
6 ms |
404 KB |
Output is correct |
41 |
Correct |
8 ms |
208 KB |
Output is correct |
42 |
Correct |
10 ms |
208 KB |
Output is correct |
43 |
Correct |
7 ms |
208 KB |
Output is correct |
44 |
Correct |
9 ms |
296 KB |
Output is correct |
45 |
Correct |
10 ms |
208 KB |
Output is correct |
46 |
Correct |
8 ms |
208 KB |
Output is correct |
47 |
Correct |
8 ms |
208 KB |
Output is correct |
48 |
Correct |
10 ms |
208 KB |
Output is correct |
49 |
Correct |
9 ms |
212 KB |
Output is correct |
50 |
Correct |
11 ms |
208 KB |
Output is correct |
51 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
52 |
Correct |
9 ms |
300 KB |
Output is correct |
53 |
Correct |
8 ms |
208 KB |
Output is correct |
54 |
Correct |
10 ms |
208 KB |
Output is correct |
55 |
Correct |
10 ms |
208 KB |
Output is correct |
56 |
Correct |
8 ms |
208 KB |
Output is correct |
57 |
Partially correct |
9 ms |
208 KB |
Output is partially correct |
58 |
Partially correct |
9 ms |
208 KB |
Output is partially correct |
59 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
60 |
Correct |
11 ms |
208 KB |
Output is correct |
61 |
Correct |
8 ms |
208 KB |
Output is correct |
62 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
63 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
64 |
Partially correct |
15 ms |
208 KB |
Output is partially correct |
65 |
Partially correct |
11 ms |
304 KB |
Output is partially correct |
66 |
Partially correct |
15 ms |
308 KB |
Output is partially correct |
67 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
68 |
Partially correct |
12 ms |
300 KB |
Output is partially correct |
69 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
70 |
Partially correct |
17 ms |
284 KB |
Output is partially correct |
71 |
Correct |
6 ms |
256 KB |
Output is correct |
72 |
Partially correct |
14 ms |
208 KB |
Output is partially correct |
73 |
Partially correct |
14 ms |
208 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
312 KB |
Output is partially correct |
75 |
Partially correct |
14 ms |
308 KB |
Output is partially correct |
76 |
Partially correct |
12 ms |
332 KB |
Output is partially correct |
77 |
Correct |
10 ms |
208 KB |
Output is correct |
78 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
79 |
Correct |
11 ms |
208 KB |
Output is correct |
80 |
Partially correct |
10 ms |
308 KB |
Output is partially correct |
81 |
Partially correct |
16 ms |
296 KB |
Output is partially correct |
82 |
Partially correct |
15 ms |
208 KB |
Output is partially correct |