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 "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxn = 256;
int n;
int edge[maxn][maxn];
void getedges() {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
int val = are_connected({i}, {j});
edge[i][j] = edge[j][i] = val;
}
}
}
bool vis[maxn];
void reset() {
for(int i = 0; i < n; i++) {
vis[i] = false;
for(int j = 0; j < n; j++) {
edge[i][j] = 0;
}
}
}
int binsearch(vector<int> a, vector<int> b) {
// find one in b that is connected to one in a
while(b.size() > 1) {
vector<int> c;
for(int i = 0; i < b.size(); i++) {
if(i % 2 == 0) {
c.pb(b[i]);
}
}
int res = are_connected(a, c);
if(res == 1) {
b = c;
} else {
c.clear();
for(int i = 0; i < b.size(); i++) {
if(i % 2 == 1) {
c.pb(b[i]);
}
}
b = c;
}
}
return b[0];
}
void insert_at(vector<int> &path, int x, int y) {
/*vis[y] = true;
path.pb(-1);
for(int i = path.size(); i >= 1; i--) {
if(path[i - 1] == x) {
path[i] = y;
break;
} else {
path[i] = path[i - 1];
}
}*/
vis[y] = true;
vector<int> a, b;
for(int i = 0; i < path.size(); i++) {
if(path[i] == x) {
a = path;
a.resize(i + 1);
a.pb(y);
for(int j = i + 1; j < path.size(); j++) {
b.pb(path[j]);
}
break;
}
}
path.clear();
for(int cur : b) {
path.pb(cur);
}
for(int cur : a) {
path.pb(cur);
}
}
bool iterate(vector<int> &path) {
/*for(int i = 0; i < n; i++) {
if(vis[i]) {
continue;
}
if(edge[i][path[0]]) {
reverse(path.begin(), path.end());
path.pb(i);
vis[i] = true;
reverse(path.begin(), path.end());
return true;
}
if(edge[i][path.back()]) {
path.pb(i);
vis[i] = true;
return true;
}
}
for(int x : path) {
for(int y = 0; y < n; y++) {
if(vis[y]) {
continue;
}
if(edge[x][y]) {
insert_at(path, x, y);
return true;
}
}
}
return false;*/
vector<int> v;
v.pb(path[0]);
v.pb(path.back());
if(v[0] == v[1]) {
v.resize(1);
}
// vector<int> v = {path.back()};
vector<int> rem;
for(int i = 0; i < n; i++) {
if(!vis[i]) {
rem.pb(i);
}
}
if(are_connected(v, rem)) {
int newitem = binsearch(v, rem);
if(path.size() == 1) {
path.pb(newitem);
vis[newitem] = true;
return true;
} else {
if(are_connected({v[0]}, {newitem})) {
reverse(path.begin(), path.end());
path.pb(newitem);
vis[newitem] = true;
reverse(path.begin(), path.end());
return true;
} else {
path.pb(newitem);
vis[newitem] = true;
return true;
}
}
/*path.pb(newitem);
vis[newitem] = true;
return true;*/
}
if(!are_connected(path, rem)) {
return false;
}
int b = binsearch(path, rem);
int a = binsearch({b}, path);
insert_at(path, a, b);
for(int cur : rem) {
if(cur != b) {
path.pb(cur);
}
}
return true;
}
vector<int> longest_trip(int N, int D){
reset();
n = N;
// getedges();
vector<int> path = {0};
vis[0] = true;
while(path.size() < n) {
bool isgood = iterate(path);
if(!isgood) {
if(path.size() > n - path.size()) {
return path;
} else {
path.clear();
for(int i = 0; i < n; i++) {
if(!vis[i]) {
path.pb(i);
}
}
return path;
}
}
}
return path;
}
Compilation message (stderr)
longesttrip.cpp: In function 'int binsearch(std::vector<int>, std::vector<int>)':
longesttrip.cpp:36:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i = 0; i < b.size(); i++) {
| ~~^~~~~~~~~~
longesttrip.cpp:46:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i = 0; i < b.size(); i++) {
| ~~^~~~~~~~~~
longesttrip.cpp: In function 'void insert_at(std::vector<int>&, int, int)':
longesttrip.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < path.size(); i++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:75:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int j = i + 1; j < path.size(); j++) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:176:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
176 | while(path.size() < n) {
| ~~~~~~~~~~~~^~~
# | 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... |