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;
int ch[256]; for (int i = 0; i < n; i++) ch[i] = 0;
vector<int> path, clique;
path.push_back(0);
ch[0] = 1;
while (true) {
vector<int> temp;
for (int i = 0; i < n; i++) if (ch[i] == 0) temp.push_back(i);
int v = find_connected({path.back()}, temp);
if (v == -1) break;
path.push_back(v);
ch[v] = 1;
}
for (int i = 0; i < n; i++) if (ch[i] == 0) clique.push_back(i);
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:59:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (t = 0; t < clique.size(); t++) if (clique[t] == target) break;
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:71:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int j = 0; j < clique.size(); j++) res.push_back(clique[j]);
| ~~^~~~~~~~~~~~~~~
longesttrip.cpp:84:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | 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... |