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;
typedef long long ll;
int adj[256][256], n;
int E(int x, int y){
assert(x != y);
if (adj[x][y] != -1) return adj[x][y];
adj[x][y] = adj[y][x] = are_connected({x}, {y});
for (int z=0;z<n;z++) if (x!=z && y!=z){
if (!adj[x][y] && !adj[x][z]) adj[y][z] = adj[z][y] = 1;
if (!adj[x][y] && !adj[y][z]) adj[x][z] = adj[z][x] = 1;
}
return adj[x][y];
}
vector<int> rev(vector<int> A){
reverse(A.begin(), A.end());
return A;
}
vector<int> operator + (const vector<int> &A, const vector<int> &B){
vector<int> ret = A;
for (auto &x:B) ret.push_back(x);
return ret;
}
std::vector<int> longest_trip(int N, int D)
{
n = N;
for (int i=0;i<N;i++) for (int j=0;j<N;j++) adj[i][j] = -1;
for (int i=0;i<N;i++) adj[i][i] = 0;
vector<int> A = {0}, B;
for (int i=1;i<N;i++){
if (B.empty()) {B.push_back(i); continue;}
if (E(A.back(), i)) {A.push_back(i); continue;}
if (E(B.back(), i)) {B.push_back(i); continue;}
A = A + rev(B);
B.clear();
B.push_back(i);
}
if (true){
if (A.size() >= B.size()) return A;
return B;
}
if (E(A.back(), B[0])) return A + B;
if (E(A.back(), B.back())) return A + rev(B);
if (E(A[0], B.back())) return rev(A) + rev(B);
if (E(A[0], B[0])) return rev(A) + B;
assert(A.size()==1 || E(A[0], A.back()));
int l = 0, r = (int)A.size()-1, ans = (int)A.size()-1;
while(l<=r){
int mid = (l+r)>>1;
if (are_connected(vector<int>(A.begin(), A.begin()+mid+1), {B.back()})) ans = mid, r = mid-1;
else l = mid+1;
}
rotate(A.begin(), A.begin()+ans, A.end());
return B + A;
}
# | 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... |