#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> lines;
vector<int> chk;
vector<int> head;
bool ask(int u, int v) {
if (u == v) return true;
return are_connected({u}, {v});
}
int Get() {
for (int i=0; i<n; i++) {
if (!chk[i]) return i;
}
return -1;
}
int Get2() {
for (int i=0; i<n; i++) {
if (head[i]) return i;
}
return -1;
}
void make_groups() {
chk[0] = 1;
while (Get() != -1) {
int a = Get();
chk[a] = 1;
int b = Get();
if (b == -1) break;
chk[b] = 1;
if (ask(a, b)) {
lines[a].push_back(b);
head[b] = 0;
continue;
}
if (ask(a, lines[0].back())) swap(a, b);
lines[0].push_back(b);
head[b] = 0;
int c = Get();
if (c == -1) break;
chk[c] = 1;
if (ask(b, c)) {
lines[0].push_back(c);
head[c] = 0;
chk[a] = 0;
continue;
}
lines[c].push_back(a);
head[a] = 0;
int d = Get();
if (d == -1) break;
chk[d] = 1;
if (ask(b, d)) {
lines[0].push_back(d);
head[d] = 0;
continue;
}
lines[c].push_back(d);
head[d] = 0;
}
}
vector<int> longest_trip(int N, int D) {
n = N;
lines.resize(n);
for (int i=0; i<n; i++) {
lines[i].clear();
lines[i].push_back(i);
}
chk.resize(n);
fill(chk.begin(), chk.end(), 0);
head.resize(n);
fill(head.begin(), head.end(), 1);
make_groups();
int l1 = 0;
head[0] = 0;
int l2 = Get2();
if (l2 == -1) return lines[0];
head[l2] = 0;
while (Get2() != -1) {
int a = Get2();
head[a] = 0;
if (ask(lines[l1].back(), a)) {
for (auto &i : lines[a]) {
lines[l1].push_back(i);
}
continue;
}
if (ask(lines[l2].back(), a)) {
for (auto &i : lines[a]) {
lines[l2].push_back(i);
}
continue;
}
reverse(lines[l2].begin(), lines[l2].end());
for (auto &i : lines[l2]) {
lines[l1].push_back(i);
}
l2 = a;
}
if (!are_connected(lines[l1], lines[l2])) {
if (lines[l1].size() >= lines[l2].size()) return lines[l1];
return lines[l2];
}
if (!ask(lines[l1][0], lines[l1].back())) {
if (ask(lines[l1][0], lines[l2][0])) {
reverse(lines[l1].begin(), lines[l1].end());
}
for (auto &i : lines[l2]) {
lines[l1].push_back(i);
}
return lines[l1];
}
if (!ask(lines[l2][0], lines[l2].back())) {
if (ask(lines[l1][0], lines[l2][0])) {
reverse(lines[l2].begin(), lines[l2].end());
}
for (auto &i : lines[l1]) {
lines[l2].push_back(i);
}
return lines[l2];
}
int lo = 0, hi = (int)lines[l1].size() - 1;
while (lo < hi) {
int mid = lo + hi >> 1;
vector<int> t(lines[l1].begin(), lines[l1].begin() + mid + 1);
if (are_connected(t, lines[l2])) {
hi = mid;
}
else {
lo = mid + 1;
}
}
int s1 = lo;
lo = 0; hi = (int)lines[l2].size() - 1;
while (lo < hi) {
int mid = lo + hi >> 1;
vector<int> t(lines[l2].begin(), lines[l2].begin() + mid + 1);
if (are_connected({lines[l1][s1]}, t)) {
hi = mid;
}
else {
lo = mid + 1;
}
}
int s2 = lo;
vector<int> ans;
int sz = (int)lines[l1].size();
for (int i=0; i<sz; i++) {
ans.push_back(lines[l1][(s1+i+1)%sz]);
}
sz = (int)lines[l2].size();
for (int i=0; i<sz; i++) {
ans.push_back(lines[l2][(s2+i)%sz]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'bool ask(int, int)':
longesttrip.cpp:12:12: error: 'are_connected' was not declared in this scope
12 | return are_connected({u}, {v});
| ^~~~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:107:10: error: 'are_connected' was not declared in this scope
107 | if (!are_connected(lines[l1], lines[l2])) {
| ^~~~~~~~~~~~~
longesttrip.cpp:131:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
131 | int mid = lo + hi >> 1;
| ~~~^~~~
longesttrip.cpp:133:13: error: 'are_connected' was not declared in this scope
133 | if (are_connected(t, lines[l2])) {
| ^~~~~~~~~~~~~
longesttrip.cpp:143:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
143 | int mid = lo + hi >> 1;
| ~~~^~~~
longesttrip.cpp:145:13: error: 'are_connected' was not declared in this scope
145 | if (are_connected({lines[l1][s1]}, t)) {
| ^~~~~~~~~~~~~