#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> solve3(int n) {
vector<int> ans(n);
iota(ans.begin(), ans.end(), 0);
return ans;
}
vector<int> solve2(int n) {
vector<int> nxt(n);
int pair;
for (int i = 1; i < n; i++)
if (are_connected({0}, {i})) pair = i;
int head = 0, tail = pair;
nxt[head] = pair;
for (int i = 0; i < n; i++) {
if (i == 0 || i == pair) continue;
if (are_connected({head}, {i})) {
nxt[i] = head;
head = i;
} else {
nxt[tail] = i;
tail = i;
}
}
vector<int> ans;
while (true) {
ans.push_back(head);
if (head == tail) break;
head = nxt[head];
}
return ans;
}
int get_connected(vector<int> x, vector<int> y) {
if (x.empty() || y.empty()) return 0;
return are_connected(x, y);
}
vector<int> solve1(int n) {
vector<int> a({0}), b;
for (int i = 1; i < n; i++) {
int connect_a = get_connected(a, {i});
int connect_b = get_connected(b, {i});
if (!connect_a && !connect_b) {
a.insert(a.end(), b.begin(), b.end());
b = {i};
} else {
if (connect_b) {
swap(a, b);
swap(connect_a, connect_b);
}
// head
if (are_connected({a[0]}, {i})) a.insert(a.begin(), i);
// tail
else if (are_connected({a[a.size() - 1]}, {i}))
a.push_back(i);
else {
for (int j = 0; j < (int)a.size(); j++) {
if (are_connected({a[j]}, {i})) {
// [i] + [j, ...] + [0, ..., j-1]
vector<int> vec;
vec.push_back(i);
for (int k = j; k < (int)a.size(); k++) vec.push_back(a[k]);
for (int k = 0; k < j; k++) vec.push_back(a[k]);
a = vec;
break;
}
}
}
if (connect_b) {
// connect to head
if (are_connected({b[0]}, {i})) {
// [bn, ..., b0] + [a0, ..., an]
reverse(b.begin(), b.end());
b.insert(b.end(), a.begin(), a.end());
a = b;
b = {};
}
// connect to tail
else if (are_connected({b[b.size() - 1]}, {i})) {
// [b0, ..., bn] + [a0, ..., an]
b.insert(b.end(), a.begin(), a.end());
a = b;
b = {};
}
// connect to middle
else {
for (int j = 0; j < (int)b.size(); j++) {
if (are_connected({b[j]}, {i})) {
// // [bj+1, ..., bn] [b0, ..., bj] [a0, ..., an]
vector<int> vec;
for (int k = j + 1; k < (int)b.size(); k++) vec.push_back(b[k]);
for (int k = 0; k <= j; k++) vec.push_back(b[k]);
vec.insert(vec.end(), a.begin(), a.end());
a = vec;
b = {};
break;
}
}
}
}
}
}
if (a.size() > b.size()) return a;
return b;
}
std::vector<int> longest_trip(int n, int density) {
if (density == 3) return solve3(n);
if (density == 2) return solve2(n);
if (density == 1) return solve1(n);
return {};
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> solve2(int)':
longesttrip.cpp:18:13: warning: 'pair' may be used uninitialized in this function [-Wmaybe-uninitialized]
18 | nxt[head] = pair;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
10 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
10 ms |
344 KB |
Output is correct |
8 |
Correct |
9 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
9 ms |
608 KB |
Output is correct |
11 |
Correct |
9 ms |
440 KB |
Output is correct |
12 |
Correct |
9 ms |
600 KB |
Output is correct |
13 |
Correct |
9 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
9 ms |
600 KB |
Output is correct |
4 |
Correct |
14 ms |
600 KB |
Output is correct |
5 |
Correct |
17 ms |
996 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
15 ms |
448 KB |
Output is correct |
10 |
Correct |
18 ms |
1248 KB |
Output is correct |
11 |
Correct |
18 ms |
748 KB |
Output is correct |
12 |
Correct |
18 ms |
492 KB |
Output is correct |
13 |
Correct |
19 ms |
1000 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
11 ms |
344 KB |
Output is correct |
18 |
Correct |
11 ms |
344 KB |
Output is correct |
19 |
Correct |
16 ms |
696 KB |
Output is correct |
20 |
Correct |
13 ms |
696 KB |
Output is correct |
21 |
Correct |
20 ms |
748 KB |
Output is correct |
22 |
Correct |
19 ms |
504 KB |
Output is correct |
23 |
Correct |
21 ms |
1488 KB |
Output is correct |
24 |
Correct |
20 ms |
1004 KB |
Output is correct |
25 |
Correct |
10 ms |
340 KB |
Output is correct |
26 |
Correct |
11 ms |
344 KB |
Output is correct |
27 |
Correct |
10 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
11 ms |
344 KB |
Output is correct |
30 |
Correct |
13 ms |
432 KB |
Output is correct |
31 |
Correct |
16 ms |
436 KB |
Output is correct |
32 |
Correct |
14 ms |
436 KB |
Output is correct |
33 |
Correct |
16 ms |
600 KB |
Output is correct |
34 |
Correct |
15 ms |
444 KB |
Output is correct |
35 |
Correct |
17 ms |
444 KB |
Output is correct |
36 |
Correct |
21 ms |
912 KB |
Output is correct |
37 |
Correct |
25 ms |
964 KB |
Output is correct |
38 |
Correct |
23 ms |
724 KB |
Output is correct |
39 |
Correct |
22 ms |
600 KB |
Output is correct |
40 |
Correct |
23 ms |
624 KB |
Output is correct |
41 |
Correct |
23 ms |
1204 KB |
Output is correct |
42 |
Correct |
21 ms |
448 KB |
Output is correct |
43 |
Correct |
23 ms |
700 KB |
Output is correct |
44 |
Correct |
23 ms |
952 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
13 ms |
596 KB |
Output is correct |
47 |
Correct |
14 ms |
344 KB |
Output is correct |
48 |
Correct |
12 ms |
344 KB |
Output is correct |
49 |
Correct |
12 ms |
344 KB |
Output is correct |
50 |
Correct |
13 ms |
684 KB |
Output is correct |
51 |
Correct |
18 ms |
688 KB |
Output is correct |
52 |
Correct |
14 ms |
692 KB |
Output is correct |
53 |
Correct |
16 ms |
852 KB |
Output is correct |
54 |
Incorrect |
6 ms |
700 KB |
Incorrect |
55 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
13 ms |
700 KB |
Output is correct |
5 |
Partially correct |
20 ms |
1240 KB |
Output is partially correct |
6 |
Correct |
10 ms |
500 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
12 ms |
448 KB |
Output is correct |
10 |
Partially correct |
19 ms |
996 KB |
Output is partially correct |
11 |
Partially correct |
20 ms |
612 KB |
Output is partially correct |
12 |
Partially correct |
18 ms |
748 KB |
Output is partially correct |
13 |
Partially correct |
19 ms |
704 KB |
Output is partially correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
11 ms |
600 KB |
Output is correct |
18 |
Correct |
11 ms |
344 KB |
Output is correct |
19 |
Correct |
13 ms |
688 KB |
Output is correct |
20 |
Correct |
14 ms |
604 KB |
Output is correct |
21 |
Correct |
8 ms |
344 KB |
Output is correct |
22 |
Correct |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
9 ms |
344 KB |
Output is correct |
24 |
Correct |
11 ms |
344 KB |
Output is correct |
25 |
Correct |
10 ms |
344 KB |
Output is correct |
26 |
Correct |
14 ms |
440 KB |
Output is correct |
27 |
Correct |
14 ms |
436 KB |
Output is correct |
28 |
Correct |
14 ms |
440 KB |
Output is correct |
29 |
Correct |
15 ms |
440 KB |
Output is correct |
30 |
Correct |
15 ms |
444 KB |
Output is correct |
31 |
Correct |
17 ms |
608 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
12 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
13 ms |
344 KB |
Output is correct |
36 |
Correct |
11 ms |
344 KB |
Output is correct |
37 |
Correct |
12 ms |
436 KB |
Output is correct |
38 |
Correct |
15 ms |
696 KB |
Output is correct |
39 |
Correct |
16 ms |
688 KB |
Output is correct |
40 |
Correct |
15 ms |
696 KB |
Output is correct |
41 |
Incorrect |
5 ms |
436 KB |
Incorrect |
42 |
Halted |
0 ms |
0 KB |
- |