# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1217885 | im2xtreme | Longest Trip (IOI23_longesttrip) | C++20 | 0 ms | 0 KiB |
#include <vector>
#include <algorithm>
using namespace std;
// Provided API
extern bool are_connected(const vector<int>& A, const vector<int>& B);
vector<vector<int>> adj;
vector<bool> visited;
vector<int> current_path, best_path;
void dfs(int u) {
visited[u] = true;
current_path.push_back(u);
if (current_path.size() > best_path.size()) {
best_path = current_path;
}
for (int v : adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
visited[u] = false;
current_path.pop_back();
}
int* longest_trip(int N, int D) {
adj = vector<vector<int>>(N);
// Step 1: Discover edges using are_connected
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
vector<int> A = {i}, B = {j};
if (are_connected(A, B)) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
// Step 2: Run DFS from all nodes
visited = vector<bool>(N, false);
best_path.clear();
for (int i = 0; i < N; ++i) {
current_path.clear();
dfs(i);
}
// Step 3: Convert vector to static array
int* result = new int[best_path.size()];
for (int i = 0; i < (int)best_path.size(); ++i) {
result[i] = best_path[i];
}
return result;
}