# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
847213 | Trisanu_Das | Longest Trip (IOI23_longesttrip) | C++17 | 0 ms | 0 KiB |
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;
int adj[N][N]; memset(adj, -1, sizeof(adj));
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;
}
}
}