# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030210 | Agreb | Longest Trip (IOI23_longesttrip) | C++17 | 864 ms | 852 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 <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
vector <int> longest_trip(int N, int D){
vector <vector<int>> gr(N, vector<int>(N, 0));
vector <int> path;
for (int i = 0; i < N; i++){
for (int j = 0; j < i; j++){
vector <int> a{i}, b{j};
int f = are_connected(a, b);
gr[i][j] = gr[j][i] = f;
if (gr[i][j]){
path = {i, j};
}
}
}
if (N == 1){
return vector<int>{0};
}
if (N == 2){
if (gr[0][1]) return vector<int>{0, 1};
else return vector <int>{0};
}
assert(!path.empty());
vector <int> on_path(N);
for (int u : path) on_path[u] = 1;
while (path.size() < N){
int P = path.size();
int st = path[0], fin = path[P-1];
int done = 0;
for (int v = 0; v < N; v++){
if (!on_path[v]){
if (gr[st][v]){
reverse(path.begin(), path.end());
path.push_back(v);
on_path[v] = 1;
done = 1;
break;
}
if (gr[fin][v]){
path.push_back(v);
on_path[v] = 1;
done = 1;
break;
}
}
}
if (done) continue;
assert(gr[st][fin] == 1);
for (int v = 0; v < N; v++){
if (!on_path[v]){
for (int i = 0; i < P; i++){
if (gr[path[i]][v]){
vector <int> npath;
for (int j = 0; j < P; j++)
npath.push_back(path[(j+i+1)%P]);
npath.push_back(v);
path = npath;
on_path[v] = 1;
done = 1;
break;
}
}
if (done) break;
}
}
if (done) continue;
if (P >= N - P) return path;
else{
vector <int> npath;
for (int v = 0; v < N; v++){
if (!on_path[v]) npath.push_back(v);
}
return npath;
}
}
return path;
}
Compilation message (stderr)
# | 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... |