#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define pii pair<int,int>
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rn(int l, int r){
return uniform_int_distribution<int>(l,r)(rng);
}
int ask(int u, int v){
return are_connected(vi{u}, vi{v});
}
std::vector<int> longest_trip(int n, int D){
vi p;
for(int i = 1; i <= n; ++i){
p.pb(i-1);
swap(p[i-1], p[rn(0, i-1)]);
}
vi A, B;
A.pb(p[0]);
for(int i = 1; i < n; ++i){
int v = p[i];
int u = A.back();
if(ask(u, v)){
A.pb(v);
}else if(B.size()){
u = B.back();
if(ask(u, v)){
B.pb(v);
}else{
reverse(all(B));
for(int x: B) A.pb(x);
B.clear();
B.pb(v);
}
}else B.pb(v);
}
if(B.size()>A.size()) A.swap(B);
if(B.empty()) return A;
int x = B[0], y = B.back();
int x1 = A[0], y1 = A.back();
bool ok = ask(x, x1);
bool ok1 = ask(x, y1);
if(ok){
reverse(all(A));
for(int x: B) A.pb(x);
return A;
}
if(ok1){
for(int x: B) A.pb(x);
return A;
}
ok = ask(y, x1);
ok1 = ask(y, y1);
if(ok){
for(int x: A) B.pb(x);
return B;
}
if(ok1){
reverse(all(A));
for(int x: A) B.pb(x);
return B;
}
// we got two loops
if(are_connected(A, B)){
// full path
int l = 0, r = int(A.size())-2, res = int(A.size())-1;
while(l <= r){
int mid = l+r>>1;
vi AA;
for(int j = 0; j <= mid; ++j) AA.pb(A[j]);
if(are_connected(AA, B)){
res = mid;
r = mid-1;
}else{
l = mid + 1;
}
}
int i = res;
l = 0, r = int(B.size())-2, res = int(B.size())-1;
while(l <= r){
int mid = l+r>>1;
vi BB;
for(int j = 0; j <= mid; ++j) BB.pb(B[j]);
if(are_connected(vi{A[i]}, BB)){
res = mid;
r = mid-1;
}else{
l = mid + 1;
}
}
int j = res;
vi ress;
swap(i, j);
for(int k = i + 1; k < B.size(); ++k) ress.pb(B[k]);
for(int k = 0; k <= i; ++k) ress.pb(B[k]);
for(int k = j; k < A.size(); ++k) ress.pb(A[k]);
for(int k = 0; k < j; ++k) ress.pb(A[k]);
return ress;
}
return 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... |