This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> longest_trip(int N, int D) {
vector<int> ans = {0};
vector<bool> vis(N, false);
vis[0] = true;
vector<vector<int> > adj(N, vector<int>(N, -1));
auto is_connected = [&] (int x, int y) {
if (adj[x][y] >= 0) return adj[x][y];
adj[x][y] = adj[y][x] = are_connected({x}, {y});
return adj[x][y];
};
while (true) {
if (ans.size() == N) return ans;
bool flag = false;
for (int i = 0; i < N && !flag; i++) {
if (!vis[i]) {
if (is_connected(ans[0], i)) {
ans.insert(ans.begin(), i);
flag = vis[i] = true;
break;
}
if (is_connected(ans.back(), i)) {
ans.push_back(i);
flag = vis[i] = true;
break;
}
for (int j = 0; j < ans.size(); j++) {
if (is_connected(ans[j], i)) {
vector<int> new_path;
new_path.insert(new_path.end(), ans.begin() + j + 1, ans.end());
new_path.insert(new_path.end(), ans.begin(), ans.begin() + j + 1);
ans = new_path;
ans.push_back(i);
flag = vis[i] = true;
break;
}
}
}
}
if (!flag) {
if (ans.size() >= (N - ans.size())) return ans;
ans.clear();
for (int i = 0; i < N; i++) if (!vis[i]) ans.push_back(i);
return ans;
}
}
}
Compilation message (stderr)
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:16:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
16 | if (ans.size() == N) return ans;
| ~~~~~~~~~~~^~~~
longesttrip.cpp:30:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int j = 0; j < ans.size(); j++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |