#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN=300;
int N;
bool Chk[mxN];
vector <int> makev(int a){
vector <int> tmp;
tmp.push_back(a);
return tmp;
}
vector <int> makevlong(vector <int> v, int s, int e){
vector <int> res;
for(int i=s;i<=e;i++) res.push_back(v[i]);
return res;
}
vector <int> solv_bipartite(vector <int> v1, vector <int> v2){
if(!are_connected(v1, v2)){
if(v1.size()>v2.size()) swap(v1, v2);
return v2;
}
if(v1.size()>=2 && !are_connected(makev(v1[0]), makev(v1.back()))){
if(are_connected(makev(v1.back()), makev(v2.back()))){
while(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
return v1;
}
else{
for(int x : v1) v2.push_back(x);
return v2;
}
}
if(v2.size()>=2 && !are_connected(makev(v2[0]), makev(v2.back()))){
if(are_connected(makev(v2.back()), makev(v1.back()))){
while(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
return v1;
}
else{
for(int x : v2) v1.push_back(x);
return v1;
}
}
int s1=0, e1=(int)v1.size()-1;
while(s1!=e1){
int mid=(s1+e1)/2;
if(are_connected(makevlong(v1, s1, mid), v2)) e1=mid;
else s1=mid+1;
}
int s2=0, e2=(int)v2.size()-1;
while(s2!=e2){
int mid=(s2+e2)/2;
if(are_connected(makev(v1[s1]), makevlong(v2, s2, mid))) e2=mid;
else s2=mid+1;
}
vector <int> ans;
for(int i=s1+1;i<v1.size();i++) ans.push_back(v1[i]);
for(int i=0;i<=s1;i++) ans.push_back(v1[i]);
for(int i=s2;i<v2.size();i++) ans.push_back(v2[i]);
for(int i=0;i<s2;i++) ans.push_back(v2[i]);
return ans;
}
vector <int> solv1(vector <int> v1);
vector <int> solv2(vector <int> v1, vector <int> v2);
vector <int> solv2(vector <int> v1, vector <int> v2){
vector <int> nc;
for(int i=0;i<N;i++) Chk[i]=false;
for(int x : v1) Chk[x]=true;
for(int x : v2) Chk[x]=true;
for(int i=0;i<N;i++) if(!Chk[i]) nc.push_back(i);
if(nc.empty()){
return solv_bipartite(v1, v2);
}
else if(nc.size()==1){
int p=nc.back();
int l=v1.back(), r=v2.back();
bool res1=are_connected(makev(p), makev(l)), res2=are_connected(makev(p), makev(r));
if(res1 && res2){
v1.push_back(p);
while(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
return v1;
}
else if(res1){
v1.push_back(p);
return solv_bipartite(v1, v2);
}
else if(res2){
v2.push_back(p);
return solv_bipartite(v1, v2);
}
}
else{
int p=nc[0], q=nc[1];
int l=v1.back(), r=v2.back();
bool res1=are_connected(makev(p), makev(l));
if(!res1){
v2.push_back(p);
return solv2(v1, v2);
}
else{
bool res2=are_connected(makev(p), makev(q)), res3=are_connected(makev(q), makev(r));
if(res2 && res3){
v1.push_back(p), v1.push_back(q);
while(v2.size()){
v1.push_back(v2.back());
v2.pop_back();
}
return solv1(v1);
}
else if(res2 && !res3){
v1.push_back(p), v1.push_back(q);
return solv2(v1, v2);
}
else if(!res2 && res3){
v1.push_back(p), v2.push_back(q);
return solv2(v1, v2);
}
else{
v1.push_back(q), v2.push_back(p);
return solv2(v1, v2);
}
}
}
}
vector <int> solv1(vector <int> v1){
for(int i=0;i<N;i++) Chk[i]=false;
for(int x : v1) Chk[x]=true;
for(int i=0;i<N;i++) if(!Chk[i]){
if(are_connected(makev(i), makev(v1.back()))) v1.push_back(i);
else{
vector <int> v2;
v2.push_back(i);
return solv2(v1, v2);
}
}
return v1;
}
vector<int> longest_trip(int n, int D)
{
N=n;
return solv1(makev(0));
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> solv_bipartite(std::vector<int>, std::vector<int>)':
longesttrip.cpp:61:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=s1+1;i<v1.size();i++) ans.push_back(v1[i]);
| ~^~~~~~~~~~
longesttrip.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=s2;i<v2.size();i++) ans.push_back(v2[i]);
| ~^~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> solv2(std::vector<int>, std::vector<int>)':
longesttrip.cpp:70:18: warning: control reaches end of non-void function [-Wreturn-type]
70 | vector <int> nc;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
696 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
608 KB |
Output is correct |
12 |
Correct |
4 ms |
444 KB |
Output is correct |
13 |
Correct |
4 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
956 KB |
Output is correct |
10 |
Correct |
4 ms |
436 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
448 KB |
Output is correct |
13 |
Correct |
4 ms |
864 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
944 KB |
Output is correct |
20 |
Correct |
5 ms |
864 KB |
Output is correct |
21 |
Correct |
5 ms |
564 KB |
Output is correct |
22 |
Correct |
5 ms |
692 KB |
Output is correct |
23 |
Correct |
5 ms |
724 KB |
Output is correct |
24 |
Correct |
5 ms |
856 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
8 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
600 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
436 KB |
Output is correct |
31 |
Correct |
7 ms |
948 KB |
Output is correct |
32 |
Correct |
6 ms |
688 KB |
Output is correct |
33 |
Correct |
6 ms |
600 KB |
Output is correct |
34 |
Correct |
6 ms |
864 KB |
Output is correct |
35 |
Correct |
6 ms |
600 KB |
Output is correct |
36 |
Correct |
6 ms |
856 KB |
Output is correct |
37 |
Correct |
7 ms |
944 KB |
Output is correct |
38 |
Correct |
7 ms |
968 KB |
Output is correct |
39 |
Correct |
8 ms |
1172 KB |
Output is correct |
40 |
Correct |
8 ms |
856 KB |
Output is correct |
41 |
Correct |
7 ms |
924 KB |
Output is correct |
42 |
Correct |
7 ms |
740 KB |
Output is correct |
43 |
Correct |
7 ms |
992 KB |
Output is correct |
44 |
Correct |
7 ms |
924 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
11 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
352 KB |
Output is correct |
50 |
Correct |
6 ms |
700 KB |
Output is correct |
51 |
Correct |
7 ms |
692 KB |
Output is correct |
52 |
Correct |
7 ms |
688 KB |
Output is correct |
53 |
Correct |
6 ms |
904 KB |
Output is correct |
54 |
Correct |
8 ms |
600 KB |
Output is correct |
55 |
Correct |
8 ms |
600 KB |
Output is correct |
56 |
Correct |
7 ms |
1372 KB |
Output is correct |
57 |
Correct |
8 ms |
1256 KB |
Output is correct |
58 |
Correct |
8 ms |
1120 KB |
Output is correct |
59 |
Correct |
7 ms |
916 KB |
Output is correct |
60 |
Correct |
7 ms |
1024 KB |
Output is correct |
61 |
Correct |
8 ms |
1216 KB |
Output is correct |
62 |
Correct |
7 ms |
996 KB |
Output is correct |
63 |
Correct |
8 ms |
1116 KB |
Output is correct |
64 |
Correct |
7 ms |
904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
856 KB |
Output is correct |
10 |
Correct |
5 ms |
692 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
440 KB |
Output is correct |
13 |
Correct |
4 ms |
704 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
856 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
600 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
8 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
436 KB |
Output is correct |
27 |
Correct |
7 ms |
716 KB |
Output is correct |
28 |
Correct |
6 ms |
436 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
700 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
944 KB |
Output is correct |
38 |
Correct |
8 ms |
948 KB |
Output is correct |
39 |
Correct |
8 ms |
692 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
608 KB |
Output is correct |
43 |
Correct |
5 ms |
820 KB |
Output is correct |
44 |
Correct |
5 ms |
792 KB |
Output is correct |
45 |
Correct |
7 ms |
896 KB |
Output is correct |
46 |
Correct |
5 ms |
1040 KB |
Output is correct |
47 |
Correct |
7 ms |
600 KB |
Output is correct |
48 |
Correct |
8 ms |
1200 KB |
Output is correct |
49 |
Correct |
9 ms |
716 KB |
Output is correct |
50 |
Correct |
8 ms |
888 KB |
Output is correct |
51 |
Correct |
8 ms |
876 KB |
Output is correct |
52 |
Correct |
9 ms |
1112 KB |
Output is correct |
53 |
Correct |
8 ms |
1184 KB |
Output is correct |
54 |
Correct |
11 ms |
848 KB |
Output is correct |
55 |
Correct |
7 ms |
1168 KB |
Output is correct |
56 |
Correct |
7 ms |
856 KB |
Output is correct |
57 |
Correct |
7 ms |
856 KB |
Output is correct |
58 |
Correct |
7 ms |
716 KB |
Output is correct |
59 |
Correct |
6 ms |
780 KB |
Output is correct |
60 |
Correct |
8 ms |
924 KB |
Output is correct |
61 |
Correct |
7 ms |
996 KB |
Output is correct |
62 |
Correct |
6 ms |
1128 KB |
Output is correct |
63 |
Correct |
8 ms |
1364 KB |
Output is correct |
64 |
Correct |
7 ms |
1296 KB |
Output is correct |
65 |
Correct |
8 ms |
1372 KB |
Output is correct |
66 |
Correct |
8 ms |
1132 KB |
Output is correct |
67 |
Correct |
9 ms |
1376 KB |
Output is correct |
68 |
Correct |
8 ms |
1116 KB |
Output is correct |
69 |
Correct |
8 ms |
996 KB |
Output is correct |
70 |
Correct |
7 ms |
1416 KB |
Output is correct |
71 |
Correct |
9 ms |
1160 KB |
Output is correct |
72 |
Correct |
7 ms |
1112 KB |
Output is correct |
73 |
Correct |
7 ms |
1288 KB |
Output is correct |
74 |
Correct |
7 ms |
940 KB |
Output is correct |
75 |
Correct |
8 ms |
1160 KB |
Output is correct |
76 |
Correct |
9 ms |
1120 KB |
Output is correct |
77 |
Correct |
9 ms |
712 KB |
Output is correct |
78 |
Correct |
8 ms |
1160 KB |
Output is correct |
79 |
Correct |
8 ms |
1192 KB |
Output is correct |
80 |
Correct |
7 ms |
724 KB |
Output is correct |
81 |
Correct |
9 ms |
1128 KB |
Output is correct |
82 |
Correct |
10 ms |
1120 KB |
Output is correct |