#include "longesttrip.h"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* DEFINES */
#define S second
#define F first
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define ss(a) set<a>
#define pp(a, b) pair<a, b>
#define mm(a, b) map<a, b>
#define qq(a) queue<a>
#define pq(a) priority_queue<a>
#define ump(a, b) unordered_map<a, b>
#define ANDROID \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define elif else if
#define endl "\n"
#define pb push_back
#define ins insert
#define logi __lg
#define sqrt sqrtl
#define mpr make_pair
using namespace std;
using namespace __cxx11;
using namespace __gnu_pbds;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 256;
vv(ll) g[N];
bool mat[N][N];
std::vector<int> longest_trip(int n, int d)
{
for (ll i = 0; i < n; i++)
for (ll j = 0; j < n; j++)
mat[i][j] = 0;
for (ll i = 0; i < n; i++)
g[i].clear();
vv(int) ans;
vector<int> comp, comp1;
ll p[n];
iota(p, p + n, 0);
shuffle(p, p + n, rng);
// p[0] = 4;
// p[1] = 0;
// p[2] = 1;
// p[3] = 2;
// p[4] = 3;
// for (ll i : p)
// cout << i << " ";
// cout << endl;
for (int i : p)
if (comp1.empty())
{
if (comp.empty())
{
comp = {i};
continue;
}
if (!are_connected({comp.back()}, {i}))
comp1 = {i};
else
comp.push_back(i);
}
else if (comp.empty())
{
if (!are_connected({comp1.back()}, {i}))
comp = {i};
else
comp1.push_back(i);
}
else
{
bool ok = are_connected({comp.back()}, {i});
if (ok)
{
bool ok1 = are_connected({comp1.back()}, {i});
if (ok1)
{
comp.push_back(i);
reverse(comp1.begin(), comp1.end());
for (ll j : comp1)
comp.push_back(j);
comp1.clear();
}
else
comp.push_back(i);
}
else
comp1.push_back(i);
}
// cout << endl;
// for (ll i : comp)
// cout << i << " ";
// cout << endl;
// for (ll i : comp1)
// cout << i << " ";
// cout << endl;
// cout << "OK";
if (comp1.empty())
return comp;
if (comp.size() > comp1.size())
swap(comp, comp1);
if (!are_connected(comp, comp1))
return comp1;
if (comp1.size() >= 3)
{
bool ok;
if (comp.size() >= 3)
{
ok = are_connected({comp.front()}, {comp.back()});
if (!ok)
{
ans = comp;
if (!are_connected({comp.back()}, {comp1.front()}))
reverse(ans.begin(), ans.end());
for (ll i : comp1)
ans.push_back(i);
return ans;
}
}
swap(comp, comp1);
if (comp.size() >= 3)
{
ok = are_connected({comp.front()}, {comp.back()});
if (!ok)
{
ans = comp;
if (!are_connected({comp.back()}, {comp1.front()}))
reverse(ans.begin(), ans.end());
for (ll i : comp1)
ans.push_back(i);
return ans;
}
}
swap(comp, comp1);
}
ll l = 0, r = comp.size() - 1;
int first = comp.size() - 1;
vector<int> tmp;
while (l <= r)
{
tmp.clear();
ll mid = (l + r) >> 1;
for (ll i = 0; i <= mid; i++)
tmp.push_back(comp[i]);
if (are_connected(tmp, comp1))
r = mid - 1, first = mid;
else
l = mid + 1;
}
l = 0, r = comp1.size() - 1;
int second = comp1.size() - 1;
while (l <= r)
{
tmp.clear();
ll mid = (l + r) >> 1;
for (ll i = 0; i <= mid; i++)
tmp.push_back(comp1[i]);
if (are_connected({comp[first]}, tmp))
r = mid - 1, second = mid;
else
l = mid + 1;
}
// cout << comp[first] << " " << comp1[second] << endl;
for (ll i = first + comp.size() - 1; i >= first; i--)
ans.push_back(comp[i % comp.size()]);
for (ll i = second; i < second + comp1.size(); i++)
ans.push_back(comp1[i % comp1.size()]);
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:184:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for (ll i = second; i < second + comp1.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
500 KB |
Output is correct |
21 |
Correct |
6 ms |
600 KB |
Output is correct |
22 |
Correct |
5 ms |
516 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
500 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
8 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
344 KB |
Output is correct |
37 |
Correct |
8 ms |
344 KB |
Output is correct |
38 |
Correct |
8 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
600 KB |
Output is correct |
43 |
Correct |
7 ms |
344 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
596 KB |
Output is correct |
49 |
Correct |
11 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
468 KB |
Output is correct |
51 |
Correct |
9 ms |
464 KB |
Output is correct |
52 |
Correct |
8 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
600 KB |
Output is correct |
55 |
Correct |
8 ms |
604 KB |
Output is correct |
56 |
Correct |
6 ms |
768 KB |
Output is correct |
57 |
Correct |
9 ms |
860 KB |
Output is correct |
58 |
Correct |
8 ms |
772 KB |
Output is correct |
59 |
Correct |
9 ms |
604 KB |
Output is correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
7 ms |
512 KB |
Output is correct |
62 |
Correct |
7 ms |
600 KB |
Output is correct |
63 |
Correct |
9 ms |
604 KB |
Output is correct |
64 |
Correct |
7 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
480 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
508 KB |
Output is correct |
12 |
Correct |
4 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
464 KB |
Output is correct |
38 |
Correct |
8 ms |
468 KB |
Output is correct |
39 |
Correct |
8 ms |
468 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
12 ms |
600 KB |
Output is correct |
43 |
Correct |
5 ms |
600 KB |
Output is correct |
44 |
Correct |
5 ms |
600 KB |
Output is correct |
45 |
Correct |
5 ms |
600 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
48 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
49 |
Partially correct |
8 ms |
596 KB |
Output is partially correct |
50 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
51 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
52 |
Correct |
8 ms |
600 KB |
Output is correct |
53 |
Correct |
7 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
57 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
58 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
59 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
596 KB |
Output is correct |
62 |
Partially correct |
8 ms |
512 KB |
Output is partially correct |
63 |
Partially correct |
11 ms |
780 KB |
Output is partially correct |
64 |
Partially correct |
8 ms |
608 KB |
Output is partially correct |
65 |
Partially correct |
7 ms |
608 KB |
Output is partially correct |
66 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
67 |
Correct |
9 ms |
772 KB |
Output is correct |
68 |
Correct |
7 ms |
600 KB |
Output is correct |
69 |
Correct |
7 ms |
600 KB |
Output is correct |
70 |
Partially correct |
8 ms |
1104 KB |
Output is partially correct |
71 |
Partially correct |
10 ms |
1052 KB |
Output is partially correct |
72 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
73 |
Partially correct |
7 ms |
604 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
768 KB |
Output is partially correct |
75 |
Correct |
7 ms |
600 KB |
Output is correct |
76 |
Correct |
9 ms |
856 KB |
Output is correct |
77 |
Partially correct |
10 ms |
520 KB |
Output is partially correct |
78 |
Partially correct |
8 ms |
604 KB |
Output is partially correct |
79 |
Partially correct |
8 ms |
516 KB |
Output is partially correct |
80 |
Correct |
8 ms |
600 KB |
Output is correct |
81 |
Correct |
8 ms |
856 KB |
Output is correct |
82 |
Correct |
8 ms |
768 KB |
Output is correct |