#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> solve_d3(int N, int D)
{
vector<int> res(N);
iota(res.begin(), res.end(), 0);
return res;
}
vector<int> solve_d2(int N, int D)
{
queue<int> stops;
for (int i = 1; i < N; i++)
stops.push(i);
deque<int> trip = {0};
while (stops.size() > 1)
{
int next = stops.front();
stops.pop();
int last = trip.back();
if (are_connected({next}, {last}))
{
trip.push_back(next);
}
else
{
trip.push_back(stops.front());
stops.pop();
trip.push_back(next);
}
}
if (stops.size() == 1)
{
int next = stops.front();
int last = trip.back();
if (are_connected({next}, {last}))
{
trip.push_back(next);
}
else
{
trip.push_front(stops.front());
stops.pop();
}
}
vector<int> res;
for (int x : trip)
res.push_back(x);
return res;
}
int pop(queue<int> &q)
{
int x = q.front();
q.pop();
return x;
}
const bool maybe = true;
map<pair<set<int>, set<int>>, bool> cache;
bool are_conn(vector<int> a, vector<int> b) {
// if (a.size() != 1 || b.size() != 1) return are_connected(a, b);
// int x = a[0], y = b[0];
// if (x < y) swap(x, y);
// if (cache.count({x, y})) return cache[{x, y}];
// cache[{x, y}] = are_connected(a, b);
// return cache[{x, y}];
set<int> A, B;
for (int x : a) A.insert(x);
for (int x : b) B.insert(x);
if (cache.count({A, B})) return cache[{A, B}];
if (cache.count({B, A})) return cache[{B, A}];
bool res = are_connected(a, b);
cache[{A, B}] = res;
cache[{B, A}] = res;
return res;
}
vector<int> longest_trip(int N, int D)
{
if (D == 3)
return solve_d3(N, D);
if (D == 2)
return solve_d2(N, D);
cache.clear();
srand(time(0));
vector<int> rand_nodes(N);
iota(rand_nodes.begin(), rand_nodes.end(), 0);
random_shuffle(rand_nodes.begin(), rand_nodes.end());
queue<int> nodes;
for (int x : rand_nodes)
nodes.push(x);
vector<int> a, b;
a.push_back(pop(nodes));
bool ends_connected = maybe;
// false = a and b are not connected
// true = a and b are maybe connected
while (!nodes.empty())
{
int n = pop(nodes);
if (b.empty()) {
if (are_conn({a.back()}, {n})) {
a.push_back(n);
} else {
b.push_back(n);
cache[{{a.back()}, {b.back()}}] = false;
cache[{{b.back()}, {a.back()}}] = false;
ends_connected = false;
}
continue;
}
if (are_conn({a.back()}, {n})) {
a.push_back(n);
ends_connected = maybe;
} else if (!ends_connected || !are_conn({a.back()}, {b.back()})) {
b.push_back(n);
cache[{{a.back()}, {b.back()}}] = false;
cache[{{b.back()}, {a.back()}}] = false;
ends_connected = false;
} else {
while (!b.empty()) {
a.push_back(b.back());
b.pop_back();
}
if (are_conn({a.back()}, {n})) {
a.push_back(n);
ends_connected = maybe;
} else {
b.push_back(n);
cache[{{a.back()}, {b.back()}}] = false;
cache[{{b.back()}, {a.back()}}] = false;
ends_connected = false;
}
}
}
if (b.empty())
{
return a;
}
if (ends_connected == maybe && are_conn({a.back()}, {b.back()}))
{
while (!b.empty())
{
a.push_back(b.back());
b.pop_back();
}
return a;
}
if (!are_conn(a, b))
{
if (a.size() > b.size())
{
return a;
}
else
{
return b;
}
}
if (a.size() < b.size())
swap(a, b);
assert(a.size() >= b.size());
bool a_circle = are_conn({a.front()}, {a.back()});
if (!a_circle)
{
for (int x : a)
b.push_back(x);
return b;
}
// a is a circle
bool b_circle = b.size() == 1 || are_conn({b.front()}, {b.back()});
if (!b_circle) {
for (int x : b) a.push_back(x);
return a;
}
// both a and b are circles
// deque<int> A, B;
// for (int x : a) A.push_back(x);
// for (int x : b) B.push_back(x);
// while (true) {
// int e = A.front(); A.pop_front();
// A.push_back(e);
// if (are_conn({e}, b)) break;
// }
// while (true) {
// int e = B.back(); B.pop_back();
// B.push_front(e);
// if (are_conn({A.back()}, {e})) break;
// }
// vector<int> res;
// for (int x : A) res.push_back(x);
// for (int x : B) res.push_back(x);
// return res;
// both a and b are circles
// there is atleast one edge between a and b
int a_connect;
vector<int> x, y, A;
for (int i: a) A.push_back(i);
while (A.size() > 1) {
x.clear();
y.clear();
for (int i = 0; i < A.size(); i++) {
if (i % 2 == 0) x.push_back(A[i]);
else y.push_back(A[i]);
}
if (are_conn(x, b)) { // the connection is in x
A = x;
} else {
A = y;
}
}
assert(A.size() == 1);
a_connect = A[0];
x.clear();
y.clear();
vector<int> B;
int b_connect;
for (int i: b) B.push_back(i);
while (B.size() > 1) {
x.clear();
y.clear();
for (int i = 0; i < B.size(); i++) {
if (i % 2 == 0) x.push_back(B[i]);
else y.push_back(B[i]);
}
if (are_conn({a_connect}, x)) { // the connection is in x
B = x;
} else {
B = y;
}
}
assert(B.size() == 1);
b_connect = B[0];
deque<int> A_d, B_d;
for (int x : a) A_d.push_back(x);
for (int x : b) B_d.push_back(x);
while (true) {
A_d.push_back(A_d.front());
A_d.pop_front();
if (A_d.back() == a_connect) break;
}
while (true) {
B_d.push_front(B_d.back());
B_d.pop_back();
if (B_d.front() == b_connect) break;
}
vector<int> res;
for (int x: A_d) res.push_back(x);
for (int x: B_d) res.push_back(x);
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:242:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
242 | for (int i = 0; i < A.size(); i++) {
| ~~^~~~~~~~~~
longesttrip.cpp:266:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
266 | for (int i = 0; i < B.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
340 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
7 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
9 ms |
344 KB |
Output is correct |
9 |
Correct |
9 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
344 KB |
Output is correct |
19 |
Correct |
8 ms |
340 KB |
Output is correct |
20 |
Correct |
10 ms |
600 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
10 ms |
600 KB |
Output is correct |
23 |
Correct |
10 ms |
460 KB |
Output is correct |
24 |
Correct |
7 ms |
340 KB |
Output is correct |
25 |
Correct |
12 ms |
344 KB |
Output is correct |
26 |
Correct |
8 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
384 KB |
Output is correct |
28 |
Correct |
10 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
9 ms |
344 KB |
Output is correct |
32 |
Correct |
11 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
496 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
440 KB |
Output is correct |
37 |
Correct |
7 ms |
344 KB |
Output is correct |
38 |
Correct |
11 ms |
600 KB |
Output is correct |
39 |
Correct |
9 ms |
560 KB |
Output is correct |
40 |
Correct |
9 ms |
600 KB |
Output is correct |
41 |
Correct |
11 ms |
600 KB |
Output is correct |
42 |
Correct |
9 ms |
600 KB |
Output is correct |
43 |
Correct |
11 ms |
600 KB |
Output is correct |
44 |
Correct |
12 ms |
636 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
14 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
8 ms |
344 KB |
Output is correct |
51 |
Correct |
10 ms |
504 KB |
Output is correct |
52 |
Correct |
10 ms |
508 KB |
Output is correct |
53 |
Correct |
8 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
344 KB |
Output is correct |
55 |
Correct |
10 ms |
344 KB |
Output is correct |
56 |
Correct |
8 ms |
600 KB |
Output is correct |
57 |
Correct |
9 ms |
600 KB |
Output is correct |
58 |
Correct |
12 ms |
600 KB |
Output is correct |
59 |
Correct |
15 ms |
600 KB |
Output is correct |
60 |
Correct |
10 ms |
600 KB |
Output is correct |
61 |
Correct |
10 ms |
600 KB |
Output is correct |
62 |
Correct |
11 ms |
600 KB |
Output is correct |
63 |
Correct |
15 ms |
856 KB |
Output is correct |
64 |
Correct |
11 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
10 ms |
344 KB |
Output is correct |
11 |
Correct |
8 ms |
460 KB |
Output is correct |
12 |
Correct |
7 ms |
428 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
12 ms |
344 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
11 ms |
600 KB |
Output is correct |
18 |
Correct |
8 ms |
344 KB |
Output is correct |
19 |
Correct |
11 ms |
600 KB |
Output is correct |
20 |
Correct |
10 ms |
340 KB |
Output is correct |
21 |
Correct |
11 ms |
344 KB |
Output is correct |
22 |
Correct |
11 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
11 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
12 ms |
344 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
12 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
10 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
12 ms |
344 KB |
Output is correct |
34 |
Correct |
13 ms |
344 KB |
Output is correct |
35 |
Correct |
10 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
11 ms |
344 KB |
Output is correct |
38 |
Correct |
9 ms |
340 KB |
Output is correct |
39 |
Correct |
13 ms |
344 KB |
Output is correct |
40 |
Correct |
9 ms |
344 KB |
Output is correct |
41 |
Correct |
9 ms |
344 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
10 ms |
868 KB |
Output is correct |
44 |
Correct |
9 ms |
576 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
600 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
520 KB |
Output is correct |
49 |
Correct |
9 ms |
600 KB |
Output is correct |
50 |
Correct |
12 ms |
584 KB |
Output is correct |
51 |
Correct |
10 ms |
424 KB |
Output is correct |
52 |
Correct |
9 ms |
600 KB |
Output is correct |
53 |
Correct |
10 ms |
600 KB |
Output is correct |
54 |
Correct |
13 ms |
428 KB |
Output is correct |
55 |
Correct |
9 ms |
608 KB |
Output is correct |
56 |
Correct |
10 ms |
600 KB |
Output is correct |
57 |
Correct |
9 ms |
612 KB |
Output is correct |
58 |
Correct |
12 ms |
600 KB |
Output is correct |
59 |
Correct |
13 ms |
648 KB |
Output is correct |
60 |
Correct |
10 ms |
868 KB |
Output is correct |
61 |
Correct |
12 ms |
436 KB |
Output is correct |
62 |
Correct |
7 ms |
600 KB |
Output is correct |
63 |
Correct |
8 ms |
600 KB |
Output is correct |
64 |
Correct |
11 ms |
664 KB |
Output is correct |
65 |
Correct |
10 ms |
600 KB |
Output is correct |
66 |
Correct |
11 ms |
600 KB |
Output is correct |
67 |
Correct |
11 ms |
600 KB |
Output is correct |
68 |
Correct |
10 ms |
600 KB |
Output is correct |
69 |
Correct |
12 ms |
600 KB |
Output is correct |
70 |
Correct |
16 ms |
600 KB |
Output is correct |
71 |
Correct |
11 ms |
632 KB |
Output is correct |
72 |
Correct |
12 ms |
668 KB |
Output is correct |
73 |
Correct |
10 ms |
600 KB |
Output is correct |
74 |
Correct |
12 ms |
684 KB |
Output is correct |
75 |
Correct |
10 ms |
600 KB |
Output is correct |
76 |
Correct |
10 ms |
600 KB |
Output is correct |
77 |
Correct |
8 ms |
496 KB |
Output is correct |
78 |
Correct |
9 ms |
600 KB |
Output is correct |
79 |
Correct |
10 ms |
600 KB |
Output is correct |
80 |
Correct |
15 ms |
856 KB |
Output is correct |
81 |
Correct |
10 ms |
600 KB |
Output is correct |
82 |
Correct |
11 ms |
724 KB |
Output is correct |