#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
vector<int> longest_trip(int n, int d) {
if (d == 3) {
vector<int> ans(n);
iota(ans.begin(), ans.end(), 0);
return ans;
}
if (d == 2) {
vector<int> ans = {0};
vector<int> used(n);
used[0] = 1;
while (ans.size() < n) {
int nxt = -1;
for (int i = 0; i < n; ++i) {
if (!used[i] && are_connected({ans.back()}, {i})) {
nxt = i;
used[i] = 1;
break;
}
}
if (nxt == -1) {
reverse(ans.begin(), ans.end());
for (int i = 0; i < n; ++i) {
if (!used[i] && are_connected({ans.back()}, {i})) {
nxt = i;
used[i] = 1;
break;
}
}
}
ans.push_back(nxt);
}
return ans;
}
if (d == 1) {
vector<int> ans = {0};
vector<int> used(n);
used[0] = 1;
auto try_to_add = [&]() {
vector<int> vals;
for (int i = 0; i < n; ++i) {
if (ans.back() != i && !used[i]) {
vals.push_back(i);
}
}
if (!are_connected({ans.back()}, vals)) {
return false;
}
while (vals.size() > 1) {
int m = vals.size() / 2;
vector<int> vals1, vals2;
for (int i = 0; i < (int)vals.size(); ++i) {
if (i < m) {
vals1.push_back(vals[i]);
} else {
vals2.push_back(vals[i]);
}
}
if (are_connected({ans.back()}, vals1)) {
vals = vals1;
} else {
vals = vals2;
}
}
used[vals[0]] = 1;
ans.push_back(vals[0]);
return true;
};
while (true) {
if (!try_to_add()) {
break;
}
}
if (ans.size() < n) {
reverse(ans.begin(), ans.end());
if (try_to_add()) {
while (true) {
if (!try_to_add()) {
break;
}
}
} else {
for (int _ = 0; _ < (int)ans.size(); ++_) {
reverse(ans.begin(), ans.end());
int vl = ans.back();
ans.pop_back();
reverse(ans.begin(), ans.end());
ans.push_back(vl);
if (try_to_add()) {
while (true) {
if (!try_to_add()) {
break;
}
}
break;
}
}
}
}
if (ans.size() * 2 < n) {
ans.clear();
for (int i = 0; i < n; ++i) {
if (!used[i]) {
ans.push_back(i);
}
}
}
return ans;
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:17:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
17 | while (ans.size() < n) {
| ~~~~~~~~~~~^~~
longesttrip.cpp:79:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
79 | if (ans.size() < n) {
| ~~~~~~~~~~~^~~
longesttrip.cpp:105:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
105 | if (ans.size() * 2 < n) {
| ~~~~~~~~~~~~~~~^~~
longesttrip.cpp:115:1: warning: control reaches end of non-void function [-Wreturn-type]
115 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
4 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
288 KB |
Output is correct |
4 |
Correct |
9 ms |
256 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
6 |
Correct |
6 ms |
256 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
12 ms |
256 KB |
Output is correct |
9 |
Correct |
9 ms |
244 KB |
Output is correct |
10 |
Correct |
5 ms |
300 KB |
Output is correct |
11 |
Correct |
10 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
352 KB |
Output is correct |
13 |
Correct |
5 ms |
260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
invalid array |
2 |
Halted |
0 ms |
0 KB |
- |