#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
void MLE(){
vector<int>v;
for(int i=0;i<1e9;i++)
v.push_back(i);
}
int connected(vector<int>a,vector<int>b){
if(a.empty())
MLE();
if(b.empty())
MLE();
return are_connected(a,b);
}
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(connected(v1,vf))
return findit(v1,vf);
return findit(v2,vf);
}
void assert2(int k){
if(!k)MLE();
}
mt19937 rng(random_device{}());
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];
assert2(!path2.empty());
assert2(!path1.empty());
if(connected({U},{V})){
if(connected({U},{path1.back()}))
path1.push_back(U),path1.push_back(V);
else if(connected({U},{path2.back()}))
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(!connected({U},{path1.back()}))swap(U,V);
path1.push_back(U);
if(connected({path2.back()},{V}))
path2.push_back(V);
else {reverse(path2.begin(),path2.end());
for(auto i:path2)
path1.push_back(i);
path2 = {V};
}
}
}
assert2(!path2.empty());
assert2(!path1.empty());
if(N%2){
int X=v.back();
if(connected({X},{path1.back()}))
path1.push_back(X);
else if(connected({X},{path2.back()}))
path2.push_back(X);
else{ reverse(path2.begin(),path2.end());
for(auto i:path2)
path1.push_back(i);
path2 = {X};
}
}
if(!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(connected(A,B))
for(int i=0;i<A.size();i++)
for(int j=0;j<B.size();j++)
if(connected({A[i]},{B[j]})) {
if(!i)reverse(path1.begin(),path1.end());
if(j) reverse(path2.begin(),path1.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:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i=0;i<A.size();i++)
| ~^~~~~~~~~
longesttrip.cpp:83:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int j=0;j<B.size();j++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 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 |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 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 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
9 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 |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Incorrect |
3 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |