#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define sz(x) (int)(x.size())
mt19937 rng(42);
map<pair<int, int>, bool> cache;
bool qry(int a, int b){
if(a > b) swap(a, b);
auto p = make_pair(a, b);
if(cache.count(p)) return cache[p];
else return cache[p] = are_connected({a}, {b});
}
vector<int> longest_trip(int N, int D){
cache.clear();
(void)D;
vector<int> ord(N);
for(int i = 0; i < N; i++) ord[i] = i;
shuffle(ord.begin(), ord.end(), rng);
vector<vector<int>> paths{{ord[0]}, {ord[1]}};
for(int k = 2; k < N; k++){
int i = ord[k];
int j = rng() % 2;
if(rng() % 2){
if(qry(paths[0].back(), paths[1].back())){
paths[0].insert(paths[0].end(), paths[1].rbegin(), paths[1].rend());
paths[1] = {i};
}
else if(qry(paths[j].back(), i)) paths[j].push_back(i);
else paths[j^1].push_back(i);
}
else{
if(qry(paths[j].back(), i)) paths[j].push_back(i);
else if(qry(paths[0].back(), paths[1].back())){
paths[0].insert(paths[0].end(), paths[1].rbegin(), paths[1].rend());
paths[1] = {i};
}
else paths[j^1].push_back(i);
}
}
if(!are_connected(paths[0], paths[1])){
return sz(paths[0]) > sz(paths[1]) ? paths[0] : paths[1];
}
for(int i = 0; i < 2; i++){
if(sz(paths[i]) < 3) continue;
if(!qry(paths[i][0], paths[i].back())){
if(are_connected({paths[i][0]}, {paths[i^1][0]})){
reverse(paths[i].begin(), paths[i].end());
}
paths[i].insert(paths[i].end(), paths[i^1].begin(), paths[i^1].end());
return paths[i];
}
}
auto prefix = [&](vector<int> v, int len){
vector<int> w;
for(int i = 0; i <= len; i++) w.push_back(v[i]);
return w;
};
int lo = 0, hi = sz(paths[0])-1;
while(lo < hi){
int m = (lo+hi)/2;
if(are_connected(prefix(paths[0], m), paths[1])) hi = m;
else lo = m+1;
}
int p1 = lo;
lo = 0, hi = sz(paths[1])-1;
while(lo < hi){
int m = (lo+hi)/2;
if(are_connected(prefix(paths[0], p1), prefix(paths[1], m))) hi = m;
else lo = m+1;
}
vector<int> res;
for(int i = 1; i <= sz(paths[0]); i++){
res.push_back(paths[0][(p1+i)%sz(paths[0])]);
}
for(int i = 0; i < sz(paths[1]); i++){
res.push_back(paths[1][(lo+i)%sz(paths[1])]);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
328 KB |
Output is correct |
5 |
Correct |
9 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
14 ms |
208 KB |
Output is correct |
3 |
Correct |
5 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
13 ms |
256 KB |
Output is correct |
6 |
Correct |
16 ms |
208 KB |
Output is correct |
7 |
Correct |
13 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
11 ms |
312 KB |
Output is correct |
10 |
Correct |
11 ms |
260 KB |
Output is correct |
11 |
Correct |
9 ms |
208 KB |
Output is correct |
12 |
Correct |
11 ms |
208 KB |
Output is correct |
13 |
Correct |
13 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
208 KB |
Output is correct |
2 |
Correct |
15 ms |
208 KB |
Output is correct |
3 |
Correct |
12 ms |
208 KB |
Output is correct |
4 |
Correct |
10 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
300 KB |
Output is correct |
6 |
Correct |
15 ms |
208 KB |
Output is correct |
7 |
Correct |
16 ms |
256 KB |
Output is correct |
8 |
Correct |
6 ms |
208 KB |
Output is correct |
9 |
Correct |
9 ms |
316 KB |
Output is correct |
10 |
Correct |
10 ms |
208 KB |
Output is correct |
11 |
Correct |
11 ms |
208 KB |
Output is correct |
12 |
Correct |
12 ms |
208 KB |
Output is correct |
13 |
Correct |
13 ms |
256 KB |
Output is correct |
14 |
Correct |
13 ms |
208 KB |
Output is correct |
15 |
Correct |
13 ms |
208 KB |
Output is correct |
16 |
Correct |
14 ms |
208 KB |
Output is correct |
17 |
Correct |
12 ms |
208 KB |
Output is correct |
18 |
Correct |
6 ms |
208 KB |
Output is correct |
19 |
Correct |
12 ms |
316 KB |
Output is correct |
20 |
Correct |
9 ms |
208 KB |
Output is correct |
21 |
Correct |
9 ms |
324 KB |
Output is correct |
22 |
Correct |
12 ms |
208 KB |
Output is correct |
23 |
Correct |
10 ms |
208 KB |
Output is correct |
24 |
Correct |
9 ms |
300 KB |
Output is correct |
25 |
Correct |
13 ms |
208 KB |
Output is correct |
26 |
Correct |
11 ms |
208 KB |
Output is correct |
27 |
Correct |
10 ms |
208 KB |
Output is correct |
28 |
Correct |
10 ms |
208 KB |
Output is correct |
29 |
Correct |
11 ms |
236 KB |
Output is correct |
30 |
Correct |
9 ms |
256 KB |
Output is correct |
31 |
Correct |
13 ms |
208 KB |
Output is correct |
32 |
Correct |
6 ms |
304 KB |
Output is correct |
33 |
Correct |
16 ms |
208 KB |
Output is correct |
34 |
Correct |
9 ms |
208 KB |
Output is correct |
35 |
Correct |
9 ms |
208 KB |
Output is correct |
36 |
Correct |
12 ms |
360 KB |
Output is correct |
37 |
Correct |
11 ms |
336 KB |
Output is correct |
38 |
Correct |
9 ms |
208 KB |
Output is correct |
39 |
Correct |
6 ms |
304 KB |
Output is correct |
40 |
Correct |
13 ms |
384 KB |
Output is correct |
41 |
Correct |
11 ms |
208 KB |
Output is correct |
42 |
Correct |
12 ms |
208 KB |
Output is correct |
43 |
Correct |
13 ms |
208 KB |
Output is correct |
44 |
Correct |
12 ms |
208 KB |
Output is correct |
45 |
Correct |
11 ms |
208 KB |
Output is correct |
46 |
Correct |
16 ms |
208 KB |
Output is correct |
47 |
Correct |
15 ms |
208 KB |
Output is correct |
48 |
Correct |
14 ms |
208 KB |
Output is correct |
49 |
Correct |
9 ms |
208 KB |
Output is correct |
50 |
Correct |
12 ms |
208 KB |
Output is correct |
51 |
Correct |
9 ms |
208 KB |
Output is correct |
52 |
Correct |
10 ms |
208 KB |
Output is correct |
53 |
Correct |
11 ms |
208 KB |
Output is correct |
54 |
Correct |
13 ms |
320 KB |
Output is correct |
55 |
Correct |
16 ms |
256 KB |
Output is correct |
56 |
Correct |
15 ms |
340 KB |
Output is correct |
57 |
Correct |
18 ms |
208 KB |
Output is correct |
58 |
Correct |
16 ms |
340 KB |
Output is correct |
59 |
Correct |
14 ms |
208 KB |
Output is correct |
60 |
Correct |
15 ms |
240 KB |
Output is correct |
61 |
Correct |
9 ms |
332 KB |
Output is correct |
62 |
Correct |
16 ms |
312 KB |
Output is correct |
63 |
Correct |
14 ms |
296 KB |
Output is correct |
64 |
Correct |
15 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
208 KB |
Output is correct |
2 |
Correct |
14 ms |
208 KB |
Output is correct |
3 |
Correct |
12 ms |
256 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
10 ms |
304 KB |
Output is correct |
6 |
Correct |
9 ms |
208 KB |
Output is correct |
7 |
Correct |
11 ms |
208 KB |
Output is correct |
8 |
Correct |
12 ms |
308 KB |
Output is correct |
9 |
Correct |
6 ms |
316 KB |
Output is correct |
10 |
Correct |
8 ms |
316 KB |
Output is correct |
11 |
Correct |
9 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
324 KB |
Output is correct |
14 |
Correct |
8 ms |
256 KB |
Output is correct |
15 |
Correct |
15 ms |
236 KB |
Output is correct |
16 |
Correct |
16 ms |
360 KB |
Output is correct |
17 |
Correct |
16 ms |
208 KB |
Output is correct |
18 |
Correct |
11 ms |
300 KB |
Output is correct |
19 |
Correct |
13 ms |
208 KB |
Output is correct |
20 |
Correct |
13 ms |
320 KB |
Output is correct |
21 |
Correct |
12 ms |
208 KB |
Output is correct |
22 |
Correct |
13 ms |
208 KB |
Output is correct |
23 |
Correct |
16 ms |
208 KB |
Output is correct |
24 |
Correct |
13 ms |
256 KB |
Output is correct |
25 |
Correct |
9 ms |
208 KB |
Output is correct |
26 |
Correct |
10 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
300 KB |
Output is correct |
28 |
Correct |
12 ms |
208 KB |
Output is correct |
29 |
Correct |
12 ms |
364 KB |
Output is correct |
30 |
Correct |
10 ms |
248 KB |
Output is correct |
31 |
Correct |
13 ms |
208 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Correct |
17 ms |
208 KB |
Output is correct |
34 |
Correct |
12 ms |
208 KB |
Output is correct |
35 |
Correct |
16 ms |
256 KB |
Output is correct |
36 |
Correct |
7 ms |
236 KB |
Output is correct |
37 |
Correct |
8 ms |
208 KB |
Output is correct |
38 |
Correct |
12 ms |
256 KB |
Output is correct |
39 |
Correct |
9 ms |
308 KB |
Output is correct |
40 |
Correct |
12 ms |
312 KB |
Output is correct |
41 |
Correct |
11 ms |
320 KB |
Output is correct |
42 |
Correct |
16 ms |
368 KB |
Output is correct |
43 |
Correct |
14 ms |
376 KB |
Output is correct |
44 |
Correct |
7 ms |
384 KB |
Output is correct |
45 |
Correct |
8 ms |
384 KB |
Output is correct |
46 |
Correct |
10 ms |
208 KB |
Output is correct |
47 |
Correct |
12 ms |
324 KB |
Output is correct |
48 |
Correct |
7 ms |
332 KB |
Output is correct |
49 |
Correct |
6 ms |
308 KB |
Output is correct |
50 |
Correct |
13 ms |
432 KB |
Output is correct |
51 |
Correct |
13 ms |
208 KB |
Output is correct |
52 |
Correct |
9 ms |
312 KB |
Output is correct |
53 |
Correct |
9 ms |
260 KB |
Output is correct |
54 |
Correct |
13 ms |
208 KB |
Output is correct |
55 |
Correct |
14 ms |
260 KB |
Output is correct |
56 |
Correct |
10 ms |
304 KB |
Output is correct |
57 |
Correct |
10 ms |
328 KB |
Output is correct |
58 |
Correct |
6 ms |
208 KB |
Output is correct |
59 |
Correct |
11 ms |
208 KB |
Output is correct |
60 |
Correct |
10 ms |
208 KB |
Output is correct |
61 |
Correct |
8 ms |
208 KB |
Output is correct |
62 |
Correct |
12 ms |
208 KB |
Output is correct |
63 |
Correct |
12 ms |
208 KB |
Output is correct |
64 |
Correct |
16 ms |
208 KB |
Output is correct |
65 |
Correct |
11 ms |
208 KB |
Output is correct |
66 |
Correct |
13 ms |
208 KB |
Output is correct |
67 |
Correct |
11 ms |
336 KB |
Output is correct |
68 |
Correct |
20 ms |
208 KB |
Output is correct |
69 |
Correct |
14 ms |
208 KB |
Output is correct |
70 |
Correct |
13 ms |
208 KB |
Output is correct |
71 |
Correct |
17 ms |
208 KB |
Output is correct |
72 |
Correct |
14 ms |
324 KB |
Output is correct |
73 |
Correct |
15 ms |
208 KB |
Output is correct |
74 |
Correct |
17 ms |
208 KB |
Output is correct |
75 |
Correct |
17 ms |
208 KB |
Output is correct |
76 |
Correct |
15 ms |
208 KB |
Output is correct |
77 |
Correct |
15 ms |
324 KB |
Output is correct |
78 |
Correct |
12 ms |
324 KB |
Output is correct |
79 |
Correct |
19 ms |
208 KB |
Output is correct |
80 |
Correct |
10 ms |
208 KB |
Output is correct |
81 |
Correct |
13 ms |
336 KB |
Output is correct |
82 |
Correct |
15 ms |
320 KB |
Output is correct |