#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 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
448 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 |
123 ms |
344 KB |
Output is correct |
4 |
Correct |
338 ms |
736 KB |
Output is correct |
5 |
Correct |
657 ms |
1272 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 |
101 ms |
344 KB |
Output is correct |
4 |
Correct |
370 ms |
716 KB |
Output is correct |
5 |
Correct |
694 ms |
1280 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
23 ms |
344 KB |
Output is correct |
8 |
Correct |
134 ms |
344 KB |
Output is correct |
9 |
Correct |
282 ms |
600 KB |
Output is correct |
10 |
Incorrect |
144 ms |
1024 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
344 KB |
Output is correct |
3 |
Correct |
109 ms |
344 KB |
Output is correct |
4 |
Correct |
323 ms |
604 KB |
Output is correct |
5 |
Correct |
712 ms |
1140 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
25 ms |
344 KB |
Output is correct |
8 |
Correct |
121 ms |
600 KB |
Output is correct |
9 |
Correct |
263 ms |
700 KB |
Output is correct |
10 |
Incorrect |
184 ms |
988 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 |
20 ms |
344 KB |
Output is correct |
3 |
Partially correct |
131 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
347 ms |
592 KB |
Output is partially correct |
5 |
Partially correct |
724 ms |
1332 KB |
Output is partially correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Partially correct |
120 ms |
344 KB |
Output is partially correct |
9 |
Partially correct |
270 ms |
1008 KB |
Output is partially correct |
10 |
Incorrect |
165 ms |
1296 KB |
too many calls |
11 |
Halted |
0 ms |
0 KB |
- |