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> path = {0};
    vector<bool> inside(N, false);
    inside[0] = true;
    vector<vector<int>> memo(N, vector<int>(N, -1));
    auto is_connected = [&] (int x, int y) {
        if (memo[x][y] >= 0) {
            return memo[x][y];
        }
        memo[x][y] = memo[y][x] = are_connected({x}, {y});
        return memo[x][y];
    };
    while (true) {
        if (path.size() == N) {
            return path;
        }
        bool found = false;
        for (int i = 0; i < N && !found; ++i) {
            if (!inside[i]) {
                if (is_connected(path[0], i)) {
                    path.insert(path.begin(), i);
                    found = true;
                    inside[i] = true;
                    break;
                }
                if (is_connected(path[path.size() - 1], i)) {
                    path.insert(path.end(), i);
                    found = true;
                    inside[i] = true;
                    break;
                }
                for (int j = 0; j < path.size(); ++j) {
                    if (is_connected(path[j], i)) {
                        vector<int> new_path;
                        new_path.insert(new_path.end(), path.begin() + j + 1, path.end());
                        new_path.insert(new_path.end(), path.begin(), path.begin() + j + 1);
                        path = new_path;
                        path.insert(path.end(), i);
                        found = true;
                        inside[i] = true;
                        break;
                    }
                }
            }
        }
        if (!found) {
            if (path.size() >= (N - path.size())) {
                return path;
            }
            path.clear();
            for (int i = 0; i < N; ++i) {
                if (!inside[i]) {
                    path.insert(path.end(), i);
                }
            }
            return path;
        }
    }
}
Compilation message (stderr)
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:21:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   21 |         if (path.size() == N) {
      |             ~~~~~~~~~~~~^~~~
longesttrip.cpp:39:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |                 for (int j = 0; j < path.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... |