#include "longesttrip.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using indexed_set = indexed_map<T, null_type>;
#define loop(x, i) for (int i = 0; i < (x); i++)
#define loop1(x, i) for (int i = 1; i <= (x); i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define itloop(x) for (auto it = begin(x); x != end(x); it++)
#define itrev(x) for (auto it = rbegin(x); x != rend(x); it++)
#define INF32 ((int32_t)(2e9 + 1))
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define removeIn(x, l) l.erase(find(ALL(l), x))
#define pb push_back
#define sz(x) (int)(x).size()
#define F first
#define S second
#define var const auto &
#define foreach(l) for (var e : l)
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<i32> vi32;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vi32> vvi32;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vii> vvii;
typedef vector<viii> vviii;
typedef set<int> si;
typedef set<ii> sii;
typedef set<iii> siii;
typedef vector<si> vsi;
typedef vector<sii> vsii;
typedef vector<vsi> vvsi;
typedef vector<string> vstr;
typedef vector<vector<string>> vvstr;
typedef vector<bool> vb;
typedef vector<vb> vvb;
vi longest_trip(int N, int D)
{
if (D >= 2)
{
vi res(N);
iota(ALL(res), 0);
if (D == 3)
{
return res;
}
si resterend;
loop1(N - 1, i) resterend.insert(res[i]);
loop1(N - 1, i)
{
auto it = resterend.begin();
if (are_connected({res[i - 1]}, {*it}))
{
res[i] = *it;
resterend.erase(it);
}
else
{
if (resterend.size() == 1)
{
res.insert(res.begin(), *it);
res.pop_back();
return res;
}
res[i] = *next(it);
resterend.erase(next(it));
}
}
return res;
}
else
{
vsi adj(N);
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (are_connected({i}, {j}))
{
// cerr << i << ' ' << j << endl;
adj[i].insert(j);
adj[j].insert(i);
}
}
}
vb vis(N);
int cnt = 0;
auto dfs = [&](auto &&self, int i) -> void
{
vis[i] = 1;
cnt++;
for (int j : adj[i])
{
if (!vis[j])
self(self, j);
}
};
dfs(dfs, 0);
vi res;
if (cnt != N)
{
loop(N, i)
{
if (vis[i] == (cnt >= N / 2))
res.pb(i);
}
loop(sz(res) - 1, i) assert(adj[res[i]].count(res[i + 1]));
return res;
}
deque<int> path;
path.pb(0);
int i2 = *adj[0].begin();
path.pb(i2);
int i3 = -1;
for (int j : adj[i2])
{
if (j != 0)
{
i3 = j;
path.pb(i3);
break;
}
}
if (i3 == -1)
{
for (int j : adj[0])
{
if (j != i2)
{
i3 = j;
path.push_front(i3);
break;
}
}
}
deque<int> left;
for (int i = 1; i < N; i++)
if (i != i2 && i != i3)
left.pb(i);
while (!left.empty())
{
int i = left.back();
left.pop_back();
if (adj[i].count(path[0]))
{
path.push_front(i);
}
else if (adj[i].count(path.back()))
{
path.push_back(i);
}
else
{
assert(adj[path[0]].count(path.back()));
int newBegin = -1;
for (int j = 1; j < sz(path) - 1; j++)
{
if (adj[i].count(path[j]))
{
newBegin = j;
break;
}
}
if (newBegin == -1)
{
left.push_front(i);
continue;
}
deque<int> newPath;
newPath.pb(i);
for (int j = newBegin; j < sz(path); j++)
{
newPath.pb(path[j]);
}
for (int j = 0; j < newBegin; j++)
{
newPath.pb(path[j]);
}
path = newPath;
}
}
for (int v : path)
res.pb(v);
assert(sz(res) == N);
// loop(sz(res) - 1, i) assert(adj[res[i]].count(res[i + 1]));
return res;
}
throw;
return {};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
206 ms |
2992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Correct |
162 ms |
1112 KB |
Output is correct |
4 |
Correct |
416 ms |
1360 KB |
Output is correct |
5 |
Correct |
932 ms |
3920 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
28 ms |
600 KB |
Output is correct |
8 |
Correct |
185 ms |
344 KB |
Output is correct |
9 |
Correct |
350 ms |
852 KB |
Output is correct |
10 |
Correct |
913 ms |
3392 KB |
Output is correct |
11 |
Correct |
910 ms |
3628 KB |
Output is correct |
12 |
Correct |
892 ms |
3592 KB |
Output is correct |
13 |
Correct |
898 ms |
3524 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
11 ms |
344 KB |
Output is correct |
16 |
Correct |
45 ms |
344 KB |
Output is correct |
17 |
Correct |
81 ms |
592 KB |
Output is correct |
18 |
Correct |
148 ms |
516 KB |
Output is correct |
19 |
Correct |
328 ms |
720 KB |
Output is correct |
20 |
Correct |
330 ms |
600 KB |
Output is correct |
21 |
Correct |
923 ms |
3500 KB |
Output is correct |
22 |
Correct |
872 ms |
3104 KB |
Output is correct |
23 |
Correct |
925 ms |
3120 KB |
Output is correct |
24 |
Correct |
896 ms |
3572 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
23 ms |
344 KB |
Output is correct |
28 |
Correct |
28 ms |
344 KB |
Output is correct |
29 |
Correct |
23 ms |
344 KB |
Output is correct |
30 |
Correct |
209 ms |
600 KB |
Output is correct |
31 |
Correct |
222 ms |
600 KB |
Output is correct |
32 |
Correct |
186 ms |
344 KB |
Output is correct |
33 |
Correct |
332 ms |
1076 KB |
Output is correct |
34 |
Correct |
319 ms |
836 KB |
Output is correct |
35 |
Correct |
353 ms |
856 KB |
Output is correct |
36 |
Correct |
943 ms |
3560 KB |
Output is correct |
37 |
Correct |
890 ms |
3680 KB |
Output is correct |
38 |
Correct |
910 ms |
2756 KB |
Output is correct |
39 |
Correct |
861 ms |
2420 KB |
Output is correct |
40 |
Correct |
881 ms |
2296 KB |
Output is correct |
41 |
Correct |
882 ms |
2120 KB |
Output is correct |
42 |
Correct |
898 ms |
2340 KB |
Output is correct |
43 |
Correct |
867 ms |
2260 KB |
Output is correct |
44 |
Correct |
887 ms |
2224 KB |
Output is correct |
45 |
Correct |
9 ms |
348 KB |
Output is correct |
46 |
Correct |
12 ms |
344 KB |
Output is correct |
47 |
Correct |
26 ms |
344 KB |
Output is correct |
48 |
Correct |
24 ms |
344 KB |
Output is correct |
49 |
Correct |
23 ms |
344 KB |
Output is correct |
50 |
Correct |
197 ms |
600 KB |
Output is correct |
51 |
Correct |
210 ms |
600 KB |
Output is correct |
52 |
Correct |
227 ms |
344 KB |
Output is correct |
53 |
Correct |
341 ms |
1024 KB |
Output is correct |
54 |
Correct |
336 ms |
956 KB |
Output is correct |
55 |
Correct |
333 ms |
704 KB |
Output is correct |
56 |
Correct |
877 ms |
3856 KB |
Output is correct |
57 |
Correct |
882 ms |
3700 KB |
Output is correct |
58 |
Correct |
870 ms |
2608 KB |
Output is correct |
59 |
Correct |
873 ms |
2760 KB |
Output is correct |
60 |
Correct |
868 ms |
2304 KB |
Output is correct |
61 |
Correct |
914 ms |
2468 KB |
Output is correct |
62 |
Correct |
866 ms |
1928 KB |
Output is correct |
63 |
Correct |
875 ms |
2360 KB |
Output is correct |
64 |
Correct |
835 ms |
2600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Partially correct |
146 ms |
344 KB |
Output is partially correct |
4 |
Partially correct |
444 ms |
1268 KB |
Output is partially correct |
5 |
Partially correct |
938 ms |
3460 KB |
Output is partially correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
21 ms |
344 KB |
Output is correct |
8 |
Partially correct |
167 ms |
344 KB |
Output is partially correct |
9 |
Partially correct |
313 ms |
1040 KB |
Output is partially correct |
10 |
Partially correct |
912 ms |
3944 KB |
Output is partially correct |
11 |
Partially correct |
970 ms |
3536 KB |
Output is partially correct |
12 |
Execution timed out |
1022 ms |
3224 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |