#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> longest_trip(int N, int D)
{
vector<int> S1;
vector<int> S2;
S1.push_back(0);
S2.push_back(1);
for(int i=2;i<N;i++){
if(are_connected({S1.back()},{i})){
S1.push_back(i);
}
else if(are_connected({S2.back()},{i})){
S2.push_back(i);
}
else{
while(S2.size()){
S1.push_back(S2.back());
S2.pop_back();
}
S2.push_back(i);
}
}
if(S1.size()<S2.size())swap(S1,S2);
if(!are_connected(S1,S2))
return S1;
if(!are_connected({S1[0]},{S1.back()})){
while(S2.size()){
S1.push_back(S2.back());
S2.pop_back();
}
return S1;
}
if(S2.size()!=1&&!are_connected({S2[0]},{S2.back()})){
while(S1.size()){
S2.push_back(S1.back());
S1.pop_back();
}
return S2;
}
vector<int> cand1=S1;
vector<int> cand2=S2;
while(cand1.size()!=1){
vector<int> tmp1=cand1;
vector<int> tmp2;
int target=tmp1.size()/2;
while(tmp1.size()!=target){
tmp2.push_back(tmp1.back());
tmp1.pop_back();
}
if(are_connected(tmp1,cand2))cand1=tmp1;
else cand1=tmp2;
}
swap(cand1,cand2);
while(cand1.size()!=1){
vector<int> tmp1=cand1;
vector<int> tmp2;
int target=tmp1.size()/2;
while(tmp1.size()!=target){
tmp2.push_back(tmp1.back());
tmp1.pop_back();
}
if(are_connected(tmp1,cand2))cand1=tmp1;
else cand1=tmp2;
}
swap(cand1,cand2);
int pos1=0;
for(int i=0;i<S1.size();i++){
if(S1[i]==cand1[0])pos1=i;
}
int pos2=0;
for(int i=0;i<S2.size();i++){
if(S2[i]==cand2[0])pos2=i;
}
vector<int> S3;
S3.push_back(S1[pos1]);
for(int i=(pos1+1)%S1.size();i!=pos1;i=(i+1)%S1.size())S3.push_back(S1[i]);
reverse(S3.begin(),S3.end());
S3.push_back(S2[pos2]);
for(int i=(pos2+1)%S2.size();i!=pos2;i=(i+1)%S2.size())S3.push_back(S2[i]);
return S3;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:56:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | while(tmp1.size()!=target){
| ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:69:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
69 | while(tmp1.size()!=target){
| ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i=0;i<S1.size();i++){
| ~^~~~~~~~~~
longesttrip.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=0;i<S2.size();i++){
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
11 ms |
208 KB |
Output is correct |
5 |
Correct |
10 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
6 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
292 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
11 ms |
208 KB |
Output is correct |
9 |
Correct |
11 ms |
208 KB |
Output is correct |
10 |
Correct |
8 ms |
304 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
11 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
6 |
Correct |
17 ms |
208 KB |
Output is correct |
7 |
Correct |
15 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
208 KB |
Output is correct |
9 |
Correct |
4 ms |
296 KB |
Output is correct |
10 |
Correct |
9 ms |
304 KB |
Output is correct |
11 |
Correct |
8 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
428 KB |
Output is correct |
14 |
Correct |
9 ms |
208 KB |
Output is correct |
15 |
Incorrect |
2 ms |
208 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
15 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
208 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Correct |
14 ms |
208 KB |
Output is correct |
8 |
Correct |
14 ms |
208 KB |
Output is correct |
9 |
Correct |
10 ms |
208 KB |
Output is correct |
10 |
Correct |
7 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
10 ms |
304 KB |
Output is correct |
13 |
Correct |
5 ms |
300 KB |
Output is correct |
14 |
Correct |
6 ms |
208 KB |
Output is correct |
15 |
Incorrect |
2 ms |
208 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |