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 <random>
#include "longesttrip.h"
using namespace std;
std::vector<int> longest_trip(int N, int D)
{
vector<int> s1({0}),s2({1});
bool sep = false;
for (int i = 2; i < N; ++i) {
if (are_connected({s1.back()}, {i})) {
s1.push_back(i);
sep = false;
} else if (sep || are_connected({s2.back()}, {i})) {
s2.push_back(i);
sep = true;
} else {
while (!s2.empty()) {
s1.push_back(s2.back());
s2.pop_back();
}
s2.push_back(i);
}
}
// for (auto x : s1) cerr << x << " ";
// cerr << "\n";
// for (auto x : s2) cerr << x << " ";
// cerr << "\n";
if (!are_connected(s1, s2)) {
return s1.size() >= s2.size() ? s1 : s2;
}
if (are_connected(s1,{s2.front()})) {
reverse(s2.begin(),s2.end());
} else {
int lo = 0, hi = s2.size();
while (hi-lo>1) {
int mid = (lo+hi)/2;
vector<int> v;
for (int i = mid; i < hi; ++i) {
v.push_back(s2[i]);
}
if (!are_connected(s1, v)) {
hi = mid;
} else {
lo = mid;
}
}
rotate(s2.begin(),s2.begin()+hi,s2.end());
}
if (are_connected({s1.front()},{s2.back()})) {
reverse(s1.begin(),s1.end());
} else {
int lo = 0, hi = s1.size();
while (hi-lo>1) {
int mid = (lo+hi)/2;
vector<int> v;
for (int i = mid; i < hi; ++i) {
v.push_back(s1[i]);
}
if (!are_connected(v, {s2.back()})) {
hi = mid;
} else {
lo = mid;
}
}
rotate(s1.begin(),s1.begin()+hi,s1.end());
}
// for (auto x : s1) cerr << x << " ";
// cerr << "\n";
// for (auto x : s2) cerr << x << " ";
// cerr << "\n";
while (!s2.empty()) {
s1.push_back(s2.back());
s2.pop_back();
}
return s1;
}
# | 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... |