This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "longesttrip.h"
namespace {
vector<int> D3(int N){
vector<int> res(N);
iota(res.begin(), res.end(), 0);
return res;
}
vector<int> D2(int N){
bool edge1 = are_connected({0}, {1});
bool edge2 = are_connected({1}, {2});
bool edge3 = are_connected({2}, {0});
deque<int> res;
if (edge1 && edge2){
res.push_back(0); res.push_back(1); res.push_back(2);
}
else if (edge2 && edge3){
res.push_back(1); res.push_back(2); res.push_back(0);
}
else{
res.push_back(2); res.push_back(0); res.push_back(1);
}
for (int i = 3; i < N; i++){
bool e1 = are_connected({i}, {res.front()});
if (e1) res.push_front(i);
else res.push_back(i);
}
vector<int> vres(res.begin(), res.end());
return vres;
}
vector<int> opt, crr;
vector<int> vst;
void find_optimal(int x, vector<vector<int>> &adj){
crr.push_back(x);
if (crr.size() > opt.size()) opt = crr;
vst[x] = 1;
for (auto k: adj[x]){
if (vst[k]) continue;
if (!adj[x][k]) continue;
find_optimal(k, adj);
}
crr.pop_back();
}
}
vector<int> longest_trip(int N, int D){
if (D == 3){
return D3(N);
}
if (D == 2){
return D2(N);
}
vector<vector<int>> adj(N, vector<int>(N));
for (int i = 0; i < N; i++){
for (int j = i+1; j < N; j++){
bool edge = are_connected({i}, {j});
adj[i][j] = adj[j][i] = edge;
}
}
for (int i = 0; i < N; i++){
vst.assign(N, 0);
find_optimal(i, adj);
}
return opt;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |