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
vector<int> longest_trip(int N, int D)
{
if(D>=2){
vector<int> ans;
vector<int> v1;
vector<int> v2;
if(N==3){
v1={0};
v2={1};
if(are_connected(v1,v2)){
v1={0};
v2={2};
if(are_connected(v1,v2)){
ans={1,0,2};
return ans;
}
else{
ans={0,1,2};
return ans;
}
}
else{
ans={0,2,1};
return ans;
}
}
ans.pb(0);
for(int i=1; i<N-1; ++i){
v1={ans[ans.size()-1]};
v2={i};
if(are_connected(v1, v2)){
ans.pb(i);
}
else{
i++;
ans.pb(i);
ans.pb(i-1);
}
}
if(ans.size()==N) return ans;
v1={ans[ans.size()-1]};
v2={N-1};
if(are_connected(v1, v2)){
ans.pb(N-1);
}
else{
int a=ans[ans.size()-1];
ans.pop_back();
int b=ans[ans.size()-1];
ans.pop_back();
ans.pb(N-1);
ans.pb(b);
ans.pb(a);
}
return ans;
}
stack<int> sk1;
stack<int> sk2;
sk1.push(0);
vector<int> v1;
vector<int> v2;
for(int i=1; i<N; ++i){
v1={sk1.top()};
v2={i};
if(are_connected(v1,v2)){
sk1.push(i);
}
else{
if(!sk2.empty()){
v1={sk2.top()};
if(are_connected(v1,v2)){
sk2.push(i);
}
else{
while(!sk2.empty()){
sk1.push(sk2.top());
sk2.pop();
}
sk2.push(i);
}
}
else{
sk2.push(i);
}
}
}
vector<int> ans1;
vector<int> ans2;
while(!sk1.empty()){
ans1.pb(sk1.top());
sk1.pop();
}
while(!sk2.empty()){
ans2.pb(sk2.top());
sk2.pop();
}
if(ans1.size()>ans2.size()) return ans1;
return ans2;
}
Compilation message (stderr)
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:44:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(ans.size()==N) return ans;
| ~~~~~~~~~~^~~
# | 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... |