#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<int> longest_trip(int n, int D){
vector<int> grp[2];
grp[0] = {0};
grp[1] = {1};
vector<int> num(n);
for (int i=0;i<n;i++)num[i] = i;
// shuffle ?
for (int i=2;i<n;i++){
int u = num[i];
if (i==n-1){
if (are_connected({grp[0].back()}, {u})) grp[0].pb(u);
else if (are_connected({grp[1].back()}, {u})) grp[1].pb(u);
else {
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u};
}
break;
}
int v = num[i+1];
if (are_connected({grp[0].back()}, {u})){
grp[0].pb(u);
continue;
}
if (are_connected({grp[1].back()}, {u})){
// what ever
grp[1].pb(u);
are_connected({u}, {v}) ? grp[1].pb(v) : grp[0].pb(v);
i++;
continue;
}
if (are_connected({u}, {v})){
// mrg grp0 and grp1
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u, v};
}
grp[1].pb(v);
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
grp[1] = {u};
}
bool done = 0;
if (are_connected({grp[0].back()}, {grp[1].back()})){
reverse(all(grp[1]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (!done && are_connected({grp[0].front()}, {grp[1].back()})){
grp[1].insert(grp[1].end(), grp[0].begin(), grp[0].end());
swap(grp[0], grp[1]);
done = 1;
}
if (!done && sz(grp[1]) > 1 && are_connected({grp[0].back()}, {grp[1].front()})){
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (!done && sz(grp[1]) > 1 && are_connected({grp[0].front()}, {grp[1].front()})){
reverse(all(grp[0]));
grp[0].insert(grp[0].end(), grp[1].begin(), grp[1].end());
done = 1;
}
if (done) return grp[0];
if (!are_connected(grp[0], grp[1])) return (sz(grp[0]) >= sz(grp[1]) ? grp[0] : grp[1]);
// binary search
int lo=0, hi = sz(grp[0]);
while (hi-lo>1){
int mi = (lo+hi)/2;
vector<int> tm;
for (int i=lo;i<mi;i++){
tm.pb(grp[0][i]);
}
if (are_connected(tm, grp[1])){
lo = mi;
}
else {
hi = mi;
}
}
int ans1 = lo;
lo=0, hi = sz(grp[1]);
while (hi-lo>1){
int mi = (lo+hi)/2;
vector<int> tm;
for (int i=lo;i<mi;i++){
tm.pb(grp[1][i]);
}
if (are_connected(tm, {ans1})){
lo = mi;
}
else {
hi = mi;
}
}
int ans2=lo;
vector<int> ans;
for (int i=0;i<sz(grp[0]);i++){
int p = (ans1+i+1)%sz(grp[0]);
ans.pb(grp[0][p]);
}
for (int i=0;i<sz(grp[1]);i++){
int p = (ans2+i)%sz(grp[1]);
ans.pb(grp[1][p]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:25:8: error: 'are_connected' was not declared in this scope
25 | if (are_connected({grp[0].back()}, {u})) grp[0].pb(u);
| ^~~~~~~~~~~~~
longesttrip.cpp:35:7: error: 'are_connected' was not declared in this scope
35 | if (are_connected({grp[0].back()}, {u})){
| ^~~~~~~~~~~~~
longesttrip.cpp:39:7: error: 'are_connected' was not declared in this scope
39 | if (are_connected({grp[1].back()}, {u})){
| ^~~~~~~~~~~~~
longesttrip.cpp:46:7: error: 'are_connected' was not declared in this scope
46 | if (are_connected({u}, {v})){
| ^~~~~~~~~~~~~
longesttrip.cpp:58:6: error: 'are_connected' was not declared in this scope
58 | if (are_connected({grp[0].back()}, {grp[1].back()})){
| ^~~~~~~~~~~~~
longesttrip.cpp:63:15: error: 'are_connected' was not declared in this scope
63 | if (!done && are_connected({grp[0].front()}, {grp[1].back()})){
| ^~~~~~~~~~~~~
longesttrip.cpp:68:33: error: 'are_connected' was not declared in this scope
68 | if (!done && sz(grp[1]) > 1 && are_connected({grp[0].back()}, {grp[1].front()})){
| ^~~~~~~~~~~~~
longesttrip.cpp:72:33: error: 'are_connected' was not declared in this scope
72 | if (!done && sz(grp[1]) > 1 && are_connected({grp[0].front()}, {grp[1].front()})){
| ^~~~~~~~~~~~~
longesttrip.cpp:78:7: error: 'are_connected' was not declared in this scope
78 | if (!are_connected(grp[0], grp[1])) return (sz(grp[0]) >= sz(grp[1]) ? grp[0] : grp[1]);
| ^~~~~~~~~~~~~
longesttrip.cpp:87:7: error: 'are_connected' was not declared in this scope
87 | if (are_connected(tm, grp[1])){
| ^~~~~~~~~~~~~
longesttrip.cpp:102:7: error: 'are_connected' was not declared in this scope
102 | if (are_connected(tm, {ans1})){
| ^~~~~~~~~~~~~