#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
vector<int>join(vector<int>&A,vector<int>&B)
{
vector<int>ret=A;
for(int i=0;i<B.size();i++){
ret.push_back(B[i]);
}
return ret;
}
bool are_connected(int a,int b)
{
return are_connected(vector<int>(1,a),vector<int>(1,b));
}
void split(vector<int>&v,int sz,vector<int>&a,vector<int>&b)
{
for(int i=0;i<sz;i++){
a.push_back(v[i]);
}
for(int i=sz;i<v.size();i++){
b.push_back(v[i]);
}
}
void BS(vector<int>A,vector<int>B,int&s,int&e)
{
while(A.size()>1){
vector<int>a,b;
split(A,(A.size())/2,a,b);
if(are_connected(a,B)){
swap(A,a);
}
else{
swap(A,b);
}
}
while(B.size()>1){
vector<int>a,b;
split(B,(B.size())/2,a,b);
if(are_connected(A,a)){
swap(B,a);
}
else{
swap(B,b);
}
}
s=A[0];
e=B[0];
}
std::vector<int> longest_trip(int N, int D)
{
vector<vector<int> >path(N);
for(int i=0;i<N;i++){
path[i].push_back(i);
}
while(path.size()>2){
int sz=path.size();
vector<int>A=path[sz-1],B=path[sz-2],C=path[sz-3];
if(are_connected(A.back(),B[0])){
path.pop_back();
path.pop_back();
path.push_back(join(A,B));
continue;
}
if(are_connected(A.back(),C[0])){
path.pop_back();
path.pop_back();
path.pop_back();
path.push_back(join(A,C));
path.push_back(B);
continue;
}
path.pop_back();
path.pop_back();
path.pop_back();
path.push_back(A);
reverse(all(B));
path.push_back(join(B,C));
}
if(are_connected(path[0][0],path[1][0])){
reverse(all(path[0]));
return join(path[0],path[1]);
}
if(are_connected(path[0][0],path[1].back())){
return join(path[1],path[0]);
}
if(are_connected(path[0].back(),path[1][0])){
return join(path[0],path[1]);
}
if(are_connected(path[0].back(),path[1].back())){
reverse(all(path[1]));
return join(path[0],path[1]);
}
if(!are_connected(path[0],path[1])){
if(path[0].size()>=path[1].size()){
return path[0];
}
return path[1];
}
int c,d;
BS(path[0],path[1],c,d);
for(int i=0;i<path[0].size();i++){
if(path[0][i]==c){
c=i;
break;
}
}
for(int i=0;i<path[1].size();i++){
if(path[1][i]==d){
d=i;
break;
}
}
vector<int>ans;
for(int i=c+1;i<path[0].size();i++){
ans.push_back(path[0][i]);
}
for(int i=0;i<=c;i++){
ans.push_back(path[0][i]);
}
for(int i=d;i<path[1].size();i++){
ans.push_back(path[1][i]);
}
for(int i=0;i<d;i++){
ans.push_back(path[1][i]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> join(std::vector<int>&, std::vector<int>&)':
longesttrip.cpp:10:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i=0;i<B.size();i++){
| ~^~~~~~~~~
longesttrip.cpp: In function 'void split(std::vector<int>&, int, std::vector<int>&, std::vector<int>&)':
longesttrip.cpp:26:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i=sz;i<v.size();i++){
| ~^~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:108:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=0;i<path[0].size();i++){
| ~^~~~~~~~~~~~~~~
longesttrip.cpp:114:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int i=0;i<path[1].size();i++){
| ~^~~~~~~~~~~~~~~
longesttrip.cpp:121:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for(int i=c+1;i<path[0].size();i++){
| ~^~~~~~~~~~~~~~~
longesttrip.cpp:127:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for(int i=d;i<path[1].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
336 KB |
Output is correct |
5 |
Correct |
7 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
8 ms |
336 KB |
Output is correct |
5 |
Correct |
9 ms |
336 KB |
Output is correct |
6 |
Correct |
9 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
9 ms |
208 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Correct |
6 ms |
336 KB |
Output is correct |
12 |
Correct |
7 ms |
340 KB |
Output is correct |
13 |
Correct |
7 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
6 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
336 KB |
Output is correct |
5 |
Correct |
9 ms |
336 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
9 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
208 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Correct |
7 ms |
336 KB |
Output is correct |
12 |
Correct |
7 ms |
336 KB |
Output is correct |
13 |
Correct |
10 ms |
356 KB |
Output is correct |
14 |
Correct |
17 ms |
208 KB |
Output is correct |
15 |
Correct |
18 ms |
208 KB |
Output is correct |
16 |
Correct |
14 ms |
300 KB |
Output is correct |
17 |
Correct |
11 ms |
296 KB |
Output is correct |
18 |
Correct |
10 ms |
208 KB |
Output is correct |
19 |
Correct |
9 ms |
336 KB |
Output is correct |
20 |
Correct |
10 ms |
208 KB |
Output is correct |
21 |
Correct |
5 ms |
440 KB |
Output is correct |
22 |
Correct |
8 ms |
380 KB |
Output is correct |
23 |
Correct |
8 ms |
336 KB |
Output is correct |
24 |
Correct |
8 ms |
376 KB |
Output is correct |
25 |
Correct |
12 ms |
208 KB |
Output is correct |
26 |
Correct |
20 ms |
208 KB |
Output is correct |
27 |
Correct |
13 ms |
208 KB |
Output is correct |
28 |
Correct |
8 ms |
208 KB |
Output is correct |
29 |
Correct |
13 ms |
208 KB |
Output is correct |
30 |
Correct |
10 ms |
208 KB |
Output is correct |
31 |
Correct |
14 ms |
304 KB |
Output is correct |
32 |
Correct |
8 ms |
304 KB |
Output is correct |
33 |
Correct |
9 ms |
208 KB |
Output is correct |
34 |
Correct |
12 ms |
208 KB |
Output is correct |
35 |
Correct |
14 ms |
208 KB |
Output is correct |
36 |
Correct |
9 ms |
328 KB |
Output is correct |
37 |
Correct |
8 ms |
372 KB |
Output is correct |
38 |
Correct |
13 ms |
336 KB |
Output is correct |
39 |
Correct |
14 ms |
368 KB |
Output is correct |
40 |
Correct |
14 ms |
336 KB |
Output is correct |
41 |
Correct |
13 ms |
348 KB |
Output is correct |
42 |
Correct |
15 ms |
336 KB |
Output is correct |
43 |
Correct |
12 ms |
368 KB |
Output is correct |
44 |
Correct |
15 ms |
424 KB |
Output is correct |
45 |
Correct |
10 ms |
208 KB |
Output is correct |
46 |
Correct |
11 ms |
208 KB |
Output is correct |
47 |
Correct |
12 ms |
208 KB |
Output is correct |
48 |
Correct |
7 ms |
208 KB |
Output is correct |
49 |
Correct |
11 ms |
208 KB |
Output is correct |
50 |
Correct |
10 ms |
208 KB |
Output is correct |
51 |
Correct |
16 ms |
208 KB |
Output is correct |
52 |
Correct |
10 ms |
292 KB |
Output is correct |
53 |
Correct |
9 ms |
208 KB |
Output is correct |
54 |
Correct |
12 ms |
208 KB |
Output is correct |
55 |
Correct |
17 ms |
208 KB |
Output is correct |
56 |
Correct |
14 ms |
372 KB |
Output is correct |
57 |
Correct |
14 ms |
336 KB |
Output is correct |
58 |
Correct |
13 ms |
360 KB |
Output is correct |
59 |
Correct |
15 ms |
336 KB |
Output is correct |
60 |
Correct |
16 ms |
356 KB |
Output is correct |
61 |
Correct |
15 ms |
360 KB |
Output is correct |
62 |
Correct |
15 ms |
336 KB |
Output is correct |
63 |
Correct |
13 ms |
356 KB |
Output is correct |
64 |
Correct |
15 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
208 KB |
Output is correct |
2 |
Correct |
8 ms |
280 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
5 ms |
304 KB |
Output is correct |
5 |
Correct |
7 ms |
380 KB |
Output is correct |
6 |
Correct |
9 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
240 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
9 ms |
208 KB |
Output is correct |
10 |
Correct |
7 ms |
292 KB |
Output is correct |
11 |
Correct |
7 ms |
372 KB |
Output is correct |
12 |
Correct |
7 ms |
336 KB |
Output is correct |
13 |
Correct |
7 ms |
336 KB |
Output is correct |
14 |
Correct |
16 ms |
208 KB |
Output is correct |
15 |
Correct |
14 ms |
208 KB |
Output is correct |
16 |
Correct |
10 ms |
208 KB |
Output is correct |
17 |
Correct |
9 ms |
208 KB |
Output is correct |
18 |
Correct |
9 ms |
208 KB |
Output is correct |
19 |
Correct |
8 ms |
208 KB |
Output is correct |
20 |
Correct |
9 ms |
208 KB |
Output is correct |
21 |
Correct |
21 ms |
208 KB |
Output is correct |
22 |
Correct |
15 ms |
208 KB |
Output is correct |
23 |
Correct |
15 ms |
208 KB |
Output is correct |
24 |
Correct |
12 ms |
208 KB |
Output is correct |
25 |
Correct |
15 ms |
208 KB |
Output is correct |
26 |
Correct |
14 ms |
208 KB |
Output is correct |
27 |
Correct |
16 ms |
304 KB |
Output is correct |
28 |
Correct |
18 ms |
208 KB |
Output is correct |
29 |
Correct |
8 ms |
208 KB |
Output is correct |
30 |
Correct |
13 ms |
208 KB |
Output is correct |
31 |
Correct |
12 ms |
208 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Correct |
8 ms |
208 KB |
Output is correct |
34 |
Correct |
12 ms |
208 KB |
Output is correct |
35 |
Correct |
10 ms |
208 KB |
Output is correct |
36 |
Correct |
12 ms |
208 KB |
Output is correct |
37 |
Correct |
9 ms |
208 KB |
Output is correct |
38 |
Correct |
14 ms |
208 KB |
Output is correct |
39 |
Correct |
15 ms |
428 KB |
Output is correct |
40 |
Correct |
11 ms |
336 KB |
Output is correct |
41 |
Correct |
11 ms |
208 KB |
Output is correct |
42 |
Correct |
13 ms |
208 KB |
Output is correct |
43 |
Correct |
8 ms |
336 KB |
Output is correct |
44 |
Correct |
8 ms |
376 KB |
Output is correct |
45 |
Correct |
8 ms |
336 KB |
Output is correct |
46 |
Correct |
9 ms |
336 KB |
Output is correct |
47 |
Partially correct |
11 ms |
336 KB |
Output is partially correct |
48 |
Partially correct |
13 ms |
360 KB |
Output is partially correct |
49 |
Partially correct |
16 ms |
336 KB |
Output is partially correct |
50 |
Partially correct |
10 ms |
336 KB |
Output is partially correct |
51 |
Partially correct |
12 ms |
336 KB |
Output is partially correct |
52 |
Partially correct |
14 ms |
336 KB |
Output is partially correct |
53 |
Partially correct |
13 ms |
348 KB |
Output is partially correct |
54 |
Partially correct |
14 ms |
316 KB |
Output is partially correct |
55 |
Partially correct |
15 ms |
336 KB |
Output is partially correct |
56 |
Partially correct |
16 ms |
364 KB |
Output is partially correct |
57 |
Partially correct |
13 ms |
364 KB |
Output is partially correct |
58 |
Partially correct |
18 ms |
364 KB |
Output is partially correct |
59 |
Partially correct |
11 ms |
336 KB |
Output is partially correct |
60 |
Partially correct |
16 ms |
364 KB |
Output is partially correct |
61 |
Partially correct |
16 ms |
436 KB |
Output is partially correct |
62 |
Partially correct |
10 ms |
336 KB |
Output is partially correct |
63 |
Partially correct |
13 ms |
336 KB |
Output is partially correct |
64 |
Partially correct |
14 ms |
336 KB |
Output is partially correct |
65 |
Partially correct |
15 ms |
336 KB |
Output is partially correct |
66 |
Partially correct |
13 ms |
336 KB |
Output is partially correct |
67 |
Partially correct |
15 ms |
336 KB |
Output is partially correct |
68 |
Partially correct |
14 ms |
364 KB |
Output is partially correct |
69 |
Partially correct |
13 ms |
336 KB |
Output is partially correct |
70 |
Partially correct |
14 ms |
336 KB |
Output is partially correct |
71 |
Partially correct |
14 ms |
416 KB |
Output is partially correct |
72 |
Partially correct |
15 ms |
336 KB |
Output is partially correct |
73 |
Partially correct |
14 ms |
336 KB |
Output is partially correct |
74 |
Partially correct |
13 ms |
336 KB |
Output is partially correct |
75 |
Partially correct |
17 ms |
352 KB |
Output is partially correct |
76 |
Partially correct |
14 ms |
348 KB |
Output is partially correct |
77 |
Partially correct |
15 ms |
364 KB |
Output is partially correct |
78 |
Partially correct |
12 ms |
336 KB |
Output is partially correct |
79 |
Partially correct |
13 ms |
476 KB |
Output is partially correct |
80 |
Partially correct |
17 ms |
336 KB |
Output is partially correct |
81 |
Partially correct |
17 ms |
316 KB |
Output is partially correct |
82 |
Partially correct |
14 ms |
356 KB |
Output is partially correct |