#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
bool g[300][300] = {{false}}, og[300][300] = {{false}};
bool ej[256] = {false};
pair<vector<int>,vector<int>> fl(const vector<int>& V){
memset(ej, 0, sizeof(ej));
queue<int> q;q.push(V[0]);
ej[V[0]]=true;
vector<int> a;a.push_back(V[0]);
while(!q.empty()){
int t = q.front();
q.pop();
for(int j : V){
if(g[t][j] && !ej[j]){
ej[j]=true;
q.push(j);
a.push_back(j);
}
}
}
vector<int> b, c;
if(a.size()<V.size()){
for(int i : V) if(!ej[i]) b.push_back(i);
return {a,b};
}
int i, j;
for(i = 0;i < V.size();i++){
for(j = i+1;j < V.size();j++){
if(!g[V[i]][V[j]]) break;
}
}
if(i >= V.size() || j >= V.size()) return {a, {}};
a.clear();
memset(ej, 0, sizeof(ej));i=V[i];j=V[j];
for(int k : V){
if(k==i||k==j) continue;
if(!g[k][j]) a.push_back(k);
else if(!g[k][i]) c.push_back(k);
else {
b.push_back(k);
ej[k]=true;
}
}
if(b.empty()){
for(int& k : a){
for(int& l : c){
if(g[k][l]){
a.push_back(i);
c.push_back(j);
swap(k, a.back());
swap(l, c[0]);
for(int x : c) a.push_back(x);
return {a, {}};
}
}
}
}
for(int k : b){
for(int l : V){
g[k][l] &= ej[l];
}
}
auto L = fl(b);
if(L.second.empty()){
a.push_back(i);
for(int k : L.first) a.push_back(k);
a.push_back(j);
for(int k : c) a.push_back(k);
return {a, {}};
} else {
if(a.empty()){
L.first.push_back(i);
for(int k : L.second) L.first.push_back(k);
L.first.push_back(j);
for(int k : c) L.first.push_back(k);
return {L.first, {}};
}
if(og[a[0]][L.first[0]]){
a.push_back(i);
swap(a.back(), a[0]);
for(int k : L.first) a.push_back(k);
for(int k : a) L.second.push_back(k);
L.second.push_back(j);
for(int k : c) L.second.push_back(k);
return {L.second, {}};
} else {
a.push_back(i);
swap(a.back(), a[0]);
for(int k : L.second) a.push_back(k);
for(int k : a) L.first.push_back(k);
L.first.push_back(j);
for(int k : c) L.first.push_back(k);
return {L.first, {}};
}
}
}
std::vector<int> longest_trip(int N, int D)
{
vector<int> V;
for(int i = 0;i < N;i++){
memset(g[i], 0, sizeof(g[i]));
memset(og[i], 0, sizeof(og[i]));
V.push_back(i);
for(int j = i+1;j < N;j++){
if(are_connected({i}, {j})){
g[i][j]=g[j][i]=true;
og[i][j]=og[j][i]=true;
}
}
}
auto L = fl(V);
if(L.first.size()>=L.second.size()) return L.first;
return L.second;
}
Compilation message
longesttrip.cpp: In function 'std::pair<std::vector<int>, std::vector<int> > fl(const std::vector<int>&)':
longesttrip.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(i = 0;i < V.size();i++){
| ~~^~~~~~~~~~
longesttrip.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(j = i+1;j < V.size();j++){
| ~~^~~~~~~~~~
longesttrip.cpp:33:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if(i >= V.size() || j >= V.size()) return {a, {}};
| ~~^~~~~~~~~~~
longesttrip.cpp:33:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if(i >= V.size() || j >= V.size()) return {a, {}};
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
215 ms |
572 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
149 ms |
476 KB |
Output is correct |
4 |
Correct |
368 ms |
496 KB |
Output is correct |
5 |
Correct |
756 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Correct |
151 ms |
600 KB |
Output is correct |
4 |
Correct |
414 ms |
508 KB |
Output is correct |
5 |
Correct |
825 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
31 ms |
344 KB |
Output is correct |
3 |
Correct |
137 ms |
600 KB |
Output is correct |
4 |
Correct |
412 ms |
492 KB |
Output is correct |
5 |
Correct |
792 ms |
576 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Partially correct |
127 ms |
456 KB |
Output is partially correct |
4 |
Partially correct |
380 ms |
496 KB |
Output is partially correct |
5 |
Partially correct |
752 ms |
576 KB |
Output is partially correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |