#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
int findit(vector<int>v,vector<int>vf){
if(v.size()==1)return v[0];
vector<int>v1,v2;
for(auto i:v)
v1.push_back(i),
swap(v1,v2);
if(are_connected(v1,vf))
return findit(v1,vf);
return findit(v2,vf);
}
mt19937 rng(0);
vector<int> longest_trip(int N, int D){
vector<int>path1,path2,v(N);
iota(v.begin(),v.end(),0);
shuffle(v.begin(),v.end(),rng);
path1={v[0]},path2={v[1]};
for(int i=1;i*2+1<N;i++){
int U=v[i*2],V=v[i*2+1];
int a=are_connected({U},{path1.back()});
if(are_connected({U},{V})){
int b=are_connected({U},{path2.back()});
if(a) path1.push_back(U),
path1.push_back(V);
else if(b)
path2.push_back(U),
path2.push_back(V);
else{ reverse(path2.begin(),path2.end());
for(auto i:path2)
path1.push_back(i);
path2 = {U,V};
}
} else {
if(!a) swap(U,V);
path1.push_back(U);
if(are_connected({path2.back()},{V}))
path2.push_back(V);
else {reverse(path2.begin(),path2.end());
for(auto i:path2)
path1.push_back(i);
path2 = {V};
}
}
}
if(N%2){
int X=v.back();
int a=are_connected({X},{path1.back()});
int b=are_connected({X},{path2.back()});
if(a) path1.push_back(X);
else if(b)path2.push_back(X);
else{ reverse(path2.begin(),path2.end());
for(auto i:path2)
path1.push_back(i);
path2 = {X};
}
}
if(!are_connected(path1,path2))
return path1.size()>path2.size()?path1:path2;
vector<int>A(1,path1[0]),B(1,path2[0]);
if(path1.size()>1)A.push_back(path1.back());
if(path2.size()>1)B.push_back(path2.back());
if(are_connected(A,B))
for(int i=0;i<A.size();i++)
for(int j=0;j<B.size();j++)
if(are_connected({A[i]},{B[j]})) {
if(!i)reverse(path1.begin(),path1.end());
if(j) reverse(path2.begin(),path2.end());
for(auto i:path2) path1.push_back(i);
return path1;
}
int X=findit(path1,path2);
int Y=findit(path2,{X});
while(path1.back()-X)
path1.push_back(path1[0]),
path1.erase(path1.begin());
while(path2[0]-Y)
path2.push_back(path2[0]),
path2.erase(path2.begin());
for(auto i:path2)
path1.push_back(i);
return path1;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0;i<A.size();i++)
| ~^~~~~~~~~
longesttrip.cpp:66:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int j=0;j<B.size();j++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
9 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
11 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
9 ms |
340 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
12 ms |
344 KB |
Output is correct |
8 |
Correct |
12 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
344 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
596 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
596 KB |
Output is correct |
11 |
Correct |
9 ms |
344 KB |
Output is correct |
12 |
Correct |
8 ms |
344 KB |
Output is correct |
13 |
Correct |
9 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
10 ms |
344 KB |
Output is correct |
18 |
Correct |
11 ms |
432 KB |
Output is correct |
19 |
Correct |
10 ms |
344 KB |
Output is correct |
20 |
Correct |
11 ms |
424 KB |
Output is correct |
21 |
Correct |
10 ms |
344 KB |
Output is correct |
22 |
Correct |
10 ms |
444 KB |
Output is correct |
23 |
Correct |
7 ms |
440 KB |
Output is correct |
24 |
Correct |
11 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
9 ms |
348 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
440 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
440 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
9 ms |
596 KB |
Output is correct |
38 |
Correct |
8 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
10 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
9 ms |
344 KB |
Output is correct |
45 |
Correct |
12 ms |
344 KB |
Output is correct |
46 |
Correct |
13 ms |
588 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
10 ms |
344 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
8 ms |
344 KB |
Output is correct |
51 |
Correct |
12 ms |
444 KB |
Output is correct |
52 |
Correct |
8 ms |
344 KB |
Output is correct |
53 |
Correct |
8 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
420 KB |
Output is correct |
55 |
Correct |
9 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
10 ms |
444 KB |
Output is correct |
58 |
Correct |
8 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
452 KB |
Output is correct |
60 |
Correct |
8 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
11 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
448 KB |
Output is correct |
64 |
Correct |
10 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
344 KB |
Output is correct |
4 |
Correct |
11 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
432 KB |
Output is correct |
10 |
Correct |
9 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
9 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
444 KB |
Output is correct |
19 |
Correct |
7 ms |
344 KB |
Output is correct |
20 |
Correct |
8 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
8 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
600 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
12 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
12 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
8 ms |
344 KB |
Output is correct |
38 |
Correct |
8 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
344 KB |
Output is correct |
41 |
Correct |
10 ms |
344 KB |
Output is correct |
42 |
Correct |
9 ms |
344 KB |
Output is correct |
43 |
Correct |
8 ms |
440 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
8 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
344 KB |
Output is correct |
50 |
Correct |
8 ms |
344 KB |
Output is correct |
51 |
Correct |
9 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
9 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
10 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
344 KB |
Output is correct |
60 |
Correct |
8 ms |
344 KB |
Output is correct |
61 |
Correct |
8 ms |
344 KB |
Output is correct |
62 |
Correct |
8 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
444 KB |
Output is correct |
64 |
Correct |
6 ms |
344 KB |
Output is correct |
65 |
Correct |
9 ms |
344 KB |
Output is correct |
66 |
Correct |
6 ms |
344 KB |
Output is correct |
67 |
Correct |
8 ms |
600 KB |
Output is correct |
68 |
Correct |
7 ms |
344 KB |
Output is correct |
69 |
Correct |
8 ms |
344 KB |
Output is correct |
70 |
Correct |
11 ms |
344 KB |
Output is correct |
71 |
Correct |
8 ms |
344 KB |
Output is correct |
72 |
Correct |
8 ms |
344 KB |
Output is correct |
73 |
Correct |
8 ms |
344 KB |
Output is correct |
74 |
Correct |
6 ms |
344 KB |
Output is correct |
75 |
Correct |
8 ms |
344 KB |
Output is correct |
76 |
Correct |
8 ms |
344 KB |
Output is correct |
77 |
Correct |
8 ms |
344 KB |
Output is correct |
78 |
Correct |
8 ms |
344 KB |
Output is correct |
79 |
Correct |
8 ms |
344 KB |
Output is correct |
80 |
Correct |
6 ms |
344 KB |
Output is correct |
81 |
Correct |
8 ms |
344 KB |
Output is correct |
82 |
Correct |
8 ms |
344 KB |
Output is correct |