#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 = A + 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 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Correct |
137 ms |
344 KB |
Output is correct |
4 |
Correct |
334 ms |
508 KB |
Output is correct |
5 |
Correct |
727 ms |
608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
18 ms |
344 KB |
Output is correct |
3 |
Correct |
118 ms |
344 KB |
Output is correct |
4 |
Correct |
321 ms |
992 KB |
Output is correct |
5 |
Correct |
692 ms |
756 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
16 ms |
344 KB |
Output is correct |
8 |
Correct |
125 ms |
344 KB |
Output is correct |
9 |
Correct |
278 ms |
596 KB |
Output is correct |
10 |
Incorrect |
146 ms |
1108 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 |
23 ms |
344 KB |
Output is correct |
3 |
Correct |
131 ms |
604 KB |
Output is correct |
4 |
Correct |
349 ms |
592 KB |
Output is correct |
5 |
Correct |
739 ms |
644 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
30 ms |
344 KB |
Output is correct |
8 |
Correct |
118 ms |
344 KB |
Output is correct |
9 |
Correct |
277 ms |
712 KB |
Output is correct |
10 |
Incorrect |
167 ms |
816 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 |
23 ms |
344 KB |
Output is correct |
3 |
Partially correct |
139 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
363 ms |
588 KB |
Output is partially correct |
5 |
Partially correct |
660 ms |
892 KB |
Output is partially correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
24 ms |
344 KB |
Output is correct |
8 |
Partially correct |
123 ms |
344 KB |
Output is partially correct |
9 |
Partially correct |
302 ms |
712 KB |
Output is partially correct |
10 |
Incorrect |
155 ms |
960 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |