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 "longesttrip.h"
using namespace std;
int find_connected(vector<int> v, vector<int> arr) {
if (arr.size() == 0 || !are_connected(v, arr)) return -1;
int s = 0, e = arr.size() - 1;
while (s < e) {
int m = (s + e) / 2;
vector<int> temp;
for (int i = s; i <= m; i++) temp.push_back(arr[i]);
if (are_connected(v, temp)) e = m;
else s = m + 1;
}
return arr[s];
}
int find_connected_idx(vector<int> v, vector<int> arr) {
if (arr.size() == 0 || !are_connected(v, arr)) return -1;
int s = 0, e = arr.size() - 1;
while (s < e) {
int m = (s + e) / 2;
vector<int> temp;
for (int i = s; i <= m; i++) temp.push_back(arr[i]);
if (are_connected(v, temp)) e = m;
else s = m + 1;
}
return s;
}
vector<int> longest_trip(int N, int D) {
int n = N;
vector<int> res;
vector<int> p1, p2;
vector<int> path, clique;
for (int i = 0; i < n; i++) {
if (p1.size() == 0 || are_connected({p1.back()}, {i})) p1.push_back(i);
else if (p2.size() == 0 || are_connected({p2.back()}, {i})) p2.push_back(i);
else {
for (int j = p2.size() - 1; j >= 0; j--) p1.push_back(p2[j]);
p2.clear();
p2.push_back(i);
}
}
path = p1;
clique = p2;
bool connected = false;
if (path.size() > 0 && clique.size() > 0) connected = are_connected(path, clique);
if (connected) {
int target = find_connected({path[0]}, clique);
if (target != -1) {
res = path;
reverse(res.begin(), res.end());
int t = 0;
for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
swap(clique[0], clique[t]);
for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
return res;
}
int s = path.size() - 1;
target = find_connected({path[s]}, clique);
if (target != -1) {
res = path;
int t = 0;
for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
swap(clique[0], clique[t]);
for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
return res;
}
int idx = find_connected_idx(path, clique);
target = find_connected({clique[idx]}, path);
swap(clique[idx], clique.back());
res = clique;
while (path[0] != target) {
int front = path[0];
path.erase(path.begin());
path.push_back(front);
}
for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
return res;
}
else {
if (path.size() > clique.size()) return path;
return clique;
}
return {}; // never reached
}
Compilation message (stderr)
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:60:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:62:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:72:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int k = 0; k < path.size(); k++) res.push_back(path[k]);
| ~~^~~~~~~~~~~~~| # | 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... |