// In the name of Allah
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define pb push_back
#define Mp make_pair
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
const int maxn = 600 + 7;
int n, d;
bool mark[maxn];
vector<int> res, resx;
vector<int> ls[maxn], vc;
void merge(int i, int j) {
for (int x : ls[j]) ls[i].pb(x);
ls[j].clear(); ls[j].shrink_to_fit();
}
pii get_res(int l, int r) {
if (r - l <= 1) return Mp(l, l);
int mid = (l + r) / 2;
pii f1 = get_res(l, mid), f2 = get_res(mid, r);
vc.clear();
vc.pb(f1.F); vc.pb(f1.S); vc.pb(f2.F); vc.pb(f2.S);
sort(all(vc)); vc.resize(unique(all(vc)) - vc.begin());
while (len(vc) > 2) {
int j1 = vc.back(); vc.pop_back();
int j2 = vc.back(); vc.pop_back();
int j3 = vc.back(); vc.pop_back();
if (are_connected({ls[j1].back()}, {ls[j3][0]})) {
merge(j1, j3);
vc.pb(j1); vc.pb(j2);
}
else if (are_connected({ls[j2].back()}, {ls[j3][0]})) {
merge(j2, j3);
vc.pb(j1); vc.pb(j2);
}
else {
reverse(all(ls[j2]));
merge(j1, j2);
vc.pb(j1); vc.pb(j3);
}
}
return Mp(vc[0], vc[1]);
}
vector<int> longest_trip(int N, int D) {
n = N; d = D;
fill(mark, mark + n, 0);
res.clear(); resx.clear();
for (int i = 0; i < n; i++) {
ls[i].clear(); ls[i].pb(i);
}
if (d >= 2) {
if (are_connected({0}, {1})) {
res.pb(0); mark[0] = 1;
res.pb(1); mark[1] = 1;
}
else {
res.pb(0); mark[0] = 1;
res.pb(2); mark[2] = 1;
}
for (int i = 0; i < n; i++) {
if (mark[i]) continue;
if (are_connected({i}, {res.back()})) res.pb(i);
else {
resx.clear(); swap(res, resx);
res.pb(i);
for (int x : resx) res.pb(x);
}
mark[i] = 1;
}
}
else {
pii f = get_res(0, n);
int j1 = f.F, j2 = f.S;
if (!are_connected(ls[j1], ls[j2])) {
if (len(ls[j1]) >= len(ls[j2])) res = ls[j1];
else res = ls[j2];
}
else {
if (are_connected({ls[j1].back()}, {ls[j2][0]})) {
merge(j1, j2);
res = ls[j1];
}
else if (are_connected({ls[j1].back()}, {ls[j2].back()})) {
reverse(all(ls[j2]));
merge(j1, j2);
res = ls[j1];
}
else if (are_connected({ls[j1][0]}, {ls[j2][0]})) {
reverse(all(ls[j1]));
merge(j1, j2);
res = ls[j1];
}
else if (are_connected({ls[j1][0]}, {ls[j2].back()})) {
reverse(all(ls[j1])); reverse(all(ls[j2]));
merge(j1, j2);
res = ls[j1];
}
else {
int lx = 0, rx = len(ls[j1]);
while (rx - lx > 1) {
int mid = (lx + rx) / 2;
vc.clear();
for (int i = lx; i < mid; i++) vc.pb(ls[j1][i]);
if (are_connected(vc, ls[j2])) rx = mid;
else lx = mid;
}
int i1 = lx, v1 = ls[j1][i1];
lx = 0, rx = len(ls[j2]);
while (rx - lx > 1) {
int mid = (lx + rx) / 2;
vc.clear();
for (int i = lx; i < mid; i++) vc.pb(ls[j2][i]);
if (are_connected({v1}, vc)) rx = mid;
else lx = mid;
}
int i2 = lx, v2 = ls[j2][i2];
for (int i = (i1 + 1); i < (i1 + 1 + len(ls[j1])); i++) {
res.pb(ls[j1][i % len(ls[j1])]);
}
for (int i = i2; i < (i2 + len(ls[j2])); i++) {
res.pb(ls[j2][i % len(ls[j2])]);
}
}
}
}
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:137:18: warning: unused variable 'v2' [-Wunused-variable]
137 | int i2 = lx, v2 = ls[j2][i2];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
456 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 |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 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 |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 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 |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
460 KB |
Output is correct |
6 |
Correct |
12 ms |
352 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
464 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 |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
9 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
472 KB |
Output is correct |
22 |
Correct |
8 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
464 KB |
Output is correct |
24 |
Correct |
7 ms |
600 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
344 KB |
Output is correct |
34 |
Correct |
9 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
460 KB |
Output is correct |
37 |
Correct |
6 ms |
456 KB |
Output is correct |
38 |
Correct |
8 ms |
460 KB |
Output is correct |
39 |
Correct |
9 ms |
464 KB |
Output is correct |
40 |
Correct |
8 ms |
460 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
9 ms |
460 KB |
Output is correct |
43 |
Correct |
9 ms |
460 KB |
Output is correct |
44 |
Correct |
8 ms |
460 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
11 ms |
344 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
10 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
352 KB |
Output is correct |
51 |
Correct |
9 ms |
348 KB |
Output is correct |
52 |
Correct |
11 ms |
348 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
8 ms |
348 KB |
Output is correct |
55 |
Correct |
8 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
348 KB |
Output is correct |
57 |
Correct |
9 ms |
468 KB |
Output is correct |
58 |
Correct |
8 ms |
352 KB |
Output is correct |
59 |
Correct |
9 ms |
344 KB |
Output is correct |
60 |
Correct |
8 ms |
356 KB |
Output is correct |
61 |
Correct |
8 ms |
472 KB |
Output is correct |
62 |
Correct |
12 ms |
356 KB |
Output is correct |
63 |
Correct |
8 ms |
344 KB |
Output is correct |
64 |
Correct |
8 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
416 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 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 |
4 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 |
7 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
676 KB |
Output is correct |
13 |
Correct |
8 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
7 ms |
456 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
452 KB |
Output is correct |
31 |
Correct |
8 ms |
344 KB |
Output is correct |
32 |
Correct |
12 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
8 ms |
344 KB |
Output is correct |
39 |
Correct |
13 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
464 KB |
Output is correct |
44 |
Correct |
7 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
600 KB |
Output is correct |
47 |
Correct |
5 ms |
600 KB |
Output is correct |
48 |
Correct |
6 ms |
464 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
460 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
10 ms |
600 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
9 ms |
344 KB |
Output is correct |
55 |
Correct |
11 ms |
344 KB |
Output is correct |
56 |
Correct |
7 ms |
344 KB |
Output is correct |
57 |
Correct |
9 ms |
344 KB |
Output is correct |
58 |
Correct |
5 ms |
340 KB |
Output is correct |
59 |
Correct |
7 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
5 ms |
340 KB |
Output is correct |
63 |
Correct |
7 ms |
344 KB |
Output is correct |
64 |
Correct |
8 ms |
464 KB |
Output is correct |
65 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
66 |
Partially correct |
11 ms |
464 KB |
Output is partially correct |
67 |
Correct |
7 ms |
344 KB |
Output is correct |
68 |
Partially correct |
11 ms |
344 KB |
Output is partially correct |
69 |
Partially correct |
8 ms |
460 KB |
Output is partially correct |
70 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
71 |
Correct |
6 ms |
344 KB |
Output is correct |
72 |
Correct |
8 ms |
344 KB |
Output is correct |
73 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
74 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
75 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
76 |
Partially correct |
8 ms |
460 KB |
Output is partially correct |
77 |
Correct |
8 ms |
344 KB |
Output is correct |
78 |
Correct |
7 ms |
344 KB |
Output is correct |
79 |
Correct |
8 ms |
456 KB |
Output is correct |
80 |
Partially correct |
10 ms |
344 KB |
Output is partially correct |
81 |
Partially correct |
12 ms |
344 KB |
Output is partially correct |
82 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |