#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#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) {
for (int i = (int)V.size() - 1; i > 0; i--) {
if (V[i] == x) swap(V[i], V[i - 1]);
}
assert(V[0] == x);
return V;
}
vector<int> connect(vector<int> A, vector<int> B) {
int st = -1;
for (int c : B) {
if (are_connected({A.back()}, {c})) {
st = c;
break;
}
}
assert(st != -1);
return A + beg(B, st);
};
info recursion(vector<int> V) {
if ((int)V.size() == 1)
return {V, {}, 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()) {
info res;
cerr << p << " is a star\n";
V.erase(find(V.begin(), V.end(), p));
info m = recursion(V);
if (m.ord.empty()) {
res.ord = m.A + vector<int>{p} + m.B;
} else {
res.ord = m.ord;
res.ord.push_back(p);
}
return res;
}
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';
shuffle(A.begin(), A.end(), rnd);
shuffle(B.begin(), B.end(), rnd);
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 = fin(A, p) + connect(M.ord, B);
return res;
}
for (int it = 0; it < 2; it++) {
if ((int)A.size() == 1) {
res.ord = M.A + A + connect(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) + connect(M.B, B);
} else {
res.ord = fin(A, p) + connect(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 |
Correct |
23 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
17 ms |
344 KB |
Output is correct |
3 |
Correct |
114 ms |
344 KB |
Output is correct |
4 |
Correct |
325 ms |
868 KB |
Output is correct |
5 |
Correct |
699 ms |
1232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Correct |
131 ms |
344 KB |
Output is correct |
4 |
Correct |
333 ms |
1104 KB |
Output is correct |
5 |
Correct |
651 ms |
1540 KB |
Output is correct |
6 |
Correct |
13 ms |
344 KB |
Output is correct |
7 |
Correct |
24 ms |
344 KB |
Output is correct |
8 |
Correct |
142 ms |
340 KB |
Output is correct |
9 |
Correct |
265 ms |
504 KB |
Output is correct |
10 |
Incorrect |
158 ms |
1104 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
344 KB |
Output is correct |
3 |
Correct |
103 ms |
344 KB |
Output is correct |
4 |
Correct |
320 ms |
596 KB |
Output is correct |
5 |
Correct |
675 ms |
1412 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
18 ms |
344 KB |
Output is correct |
8 |
Correct |
143 ms |
344 KB |
Output is correct |
9 |
Correct |
269 ms |
624 KB |
Output is correct |
10 |
Incorrect |
163 ms |
1504 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
15 ms |
344 KB |
Output is correct |
3 |
Partially correct |
140 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
316 ms |
684 KB |
Output is partially correct |
5 |
Partially correct |
650 ms |
800 KB |
Output is partially correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
23 ms |
344 KB |
Output is correct |
8 |
Partially correct |
152 ms |
340 KB |
Output is partially correct |
9 |
Partially correct |
275 ms |
692 KB |
Output is partially correct |
10 |
Incorrect |
176 ms |
860 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |