# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1088000 | Valaki2 | Longest Trip (IOI23_longesttrip) | C++17 | 892 ms | 1112 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;
#define pb push_back
const int maxn = 256;
int n;
int edge[maxn][maxn];
void getedges() {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
int val = are_connected({i}, {j});
edge[i][j] = edge[j][i] = val;
}
}
}
bool vis[maxn];
void reset() {
for(int i = 0; i < n; i++) {
vis[i] = false;
for(int j = 0; j < n; j++) {
edge[i][j] = 0;
}
}
}
void insert_at(vector<int> &path, int x, int y) {
/*vis[y] = true;
path.pb(-1);
for(int i = path.size(); i >= 1; i--) {
if(path[i - 1] == x) {
path[i] = y;
break;
} else {
path[i] = path[i - 1];
}
}*/
vis[y] = true;
vector<int> a, b;
for(int i = 0; i < path.size(); i++) {
if(path[i] == x) {
a = path;
a.resize(i + 1);
a.pb(y);
for(int j = i + 1; j < path.size(); j++) {
b.pb(path[j]);
}
break;
}
}
path.clear();
for(int cur : b) {
path.pb(cur);
}
for(int cur : a) {
path.pb(cur);
}
}
bool iterate(vector<int> &path) {
for(int i = 0; i < n; i++) {
if(vis[i]) {
continue;
}
if(edge[i][path[0]]) {
reverse(path.begin(), path.end());
path.pb(i);
vis[i] = true;
reverse(path.begin(), path.end());
return true;
}
if(edge[i][path.back()]) {
path.pb(i);
vis[i] = true;
return true;
}
}
for(int x : path) {
for(int y = 0; y < n; y++) {
if(vis[y]) {
continue;
}
if(edge[x][y]) {
insert_at(path, x, y);
return true;
}
}
}
return false;
}
vector<int> longest_trip(int N, int D){
reset();
n = N;
getedges();
vector<int> path = {0};
vis[0] = true;
while(path.size() < n) {
bool isgood = iterate(path);
if(!isgood) {
if(path.size() > n - path.size()) {
return path;
} else {
path.clear();
for(int i = 0; i < n; i++) {
if(!vis[i]) {
path.pb(i);
}
}
return path;
}
}
}
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... |