#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define cerr cout
#else
#define cerr if (false) cout
#endif
struct info {
vector<int> A;
vector<int> B;
vector<int> ord;
};
int N;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
vector<int> operator + (vector<int> a, vector<int> b) {
for (int c : b) a.push_back(c);
return a;
}
void print(vector<int> a) {
cerr << '{';
for (int c : a)
cerr << c << ' ';
cerr << '}';
cerr << '\n';
}
vector<int> fin(vector<int> V, int x) {
for (int i = 0; i < (int)V.size() - 1; i++) {
if (V[i] == x) swap(V[i], V[i + 1]);
}
assert(V.back() == x);
return V;
}
vector<int> beg(vector<int> V, int x) {
auto ord = fin(V, x);
reverse(ord.begin(), ord.end());
return ord;
}
info recursion(vector<int> V) {
vector<int> A, B;
vector<bool> us(N);
int p = V[rnd() % (int)V.size()];
cerr << "chosen vertex: " << p << '\n';
for (int c : V) if (p != c) {
if (!are_connected({p}, {c})) {
B.push_back(c);
us[c] = true;
}
}
if (B.empty())
return {V, {}, V};
for (int c : V) {
if (us[c]) continue;
if (!are_connected({c}, B)) {
A.push_back(c);
us[c] = true;
}
}
cerr << "Division: \n";
print(A);
print(B);
cerr << '\n';
vector<int> v1;
for (int c : V)
if (!us[c])
v1.push_back(c);
if (v1.empty())
return {A, B, {}};
info M = recursion(v1);
info res;
if (!M.ord.empty()) {
res.ord = A + M.ord + B;
return res;
}
for (int it = 0; it < 2; it++) {
if ((int)A.size() == 1) {
res.ord = M.A + A + M.B + B;
return res;
}
swap(A, B);
}
int c = A[(A[0] == p)], t = M.A[0];
if (are_connected({c}, {t})) {
res.ord = fin(M.A, t) + beg(fin(A, p), c) + M.B + B;
} else {
res.ord = fin(A, p) + M.B + B + beg(M.A, t);
}
return res;
}
vector<int> longest_trip(int N_ins, int D) {
N = N_ins;
vector<int> v(N);
for (int i = 0; i < N; i++) v[i] = i;
info res = recursion(v);
if (res.ord.empty()) {
if ((int)res.A.size() < (int)res.B.size())
swap(res.A, res.B);
return res.A;
}
return res.ord;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
16 ms |
696 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 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 |
4 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 |
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 |
4 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 |
6 ms |
436 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |