#include "longesttrip.h"
#include "bits/stdc++.h"
using g_t = std::vector<std::vector<int>>;
std::vector<int> longest_trip(int N, int D) {
using namespace std;
vector<int> se(N);
std::iota(se.begin(), se.end(), 0);
std::random_device rd;
std::mt19937 g(rd());
shuffle(se.begin(), se.end(), g);
int st = se.back();
se.pop_back();
int oSt = se.back();
se.pop_back();
int loopSt = st, loopEn = st, oEnSt = oSt, oEnEn = oSt, oNC = -1;
g_t gr(N);
while (!se.empty() || oNC != -1) {
if (oNC == -1) {
oNC = se.back();
se.pop_back();
} else {
if (are_connected({oEnEn}, {oNC})) {
gr[oEnEn].push_back(oNC);
gr[oNC].push_back(oEnEn);
oEnEn = oNC;
oNC = -1;
} else {
if (are_connected({oNC}, {loopEn})) {
gr[loopEn].push_back(oNC);
gr[oNC].push_back(loopEn);
loopEn = oNC;
oNC = -1;
} else {
gr[loopEn].push_back(oEnEn);
gr[oEnEn].push_back(loopEn);
loopEn = oEnSt;
oEnSt = oNC;
oEnEn = oNC;
oNC = -1;
}
}
}
}
if (loopSt != loopEn && !are_connected({loopSt}, {loopEn})) {
if (are_connected({loopEn}, {oEnEn})) {
gr[loopEn].push_back(oEnEn);
gr[oEnEn].push_back(loopEn);
loopEn = oEnSt;
} else {
gr[loopSt].push_back(oEnEn);
gr[oEnEn].push_back(loopSt);
loopSt = oEnSt;
}
} else if (oEnEn != oEnSt && !are_connected({oEnEn}, {oEnSt})) {
if (are_connected({loopEn}, {oEnEn})) {
gr[loopEn].push_back(oEnEn);
gr[oEnEn].push_back(loopEn);
loopEn = oEnSt;
} else {
gr[loopEn].push_back(oEnSt);
gr[oEnSt].push_back(loopEn);
loopEn = oEnEn;
}
} else {
int act = loopSt;
vector<int> reLo{{act}};
if (!gr[act].empty()) {
act = gr[act][0];
while (true) {
int ne = gr[act][0];
if (ne == reLo.back() && gr[act].size() > 1) ne = gr[act][1];
reLo.push_back(act);
if (gr[act].size() == 1) {
break;
}
act = ne;
}
}
act = oEnSt;
vector<int> reoEn{{act}};
if (!gr[act].empty()) {
act = gr[act][0];
while (true) {
int ne = gr[act][0];
if (ne == reoEn.back() && gr[act].size() > 1) ne = gr[act][1];
reoEn.push_back(act);
if (gr[act].size() == 1) {
break;
}
act = ne;
}
}
if (!are_connected(reLo, reoEn)) {
if (reLo.size() > reoEn.size()) return reLo;
return reoEn;
}
int l = 0, r = reoEn.size();
while (l + 1 < r) {
int m = (l + r) / 2;
vector<int> relv;
for (int i = l; i < m; ++i) {
relv.push_back(reoEn[i]);
}
if (are_connected(relv, reLo)) r = m;
else l = m;
}
int ll = 0, rr = reLo.size();
while (ll + 1 < rr) {
int m = (ll + rr) / 2;
vector<int> relv;
for (int i = ll; i < m; ++i) {
relv.push_back(reLo[i]);
}
if (are_connected({reoEn[l]}, relv)) rr = m;
else ll = m;
}
vector<int> res;
for (int i = 0; i < reLo.size(); ++i) {
res.push_back(reLo[(ll + 1 + i) % reLo.size()]);
}
for (int i = 0; i < reoEn.size(); ++i) {
res.push_back(reoEn[(l + i) % reoEn.size()]);
}
return res;
}
int act = loopSt;
vector<int> res{{act}};
if (!gr[act].empty()) {
act = gr[act][0];
while (true) {
int ne = gr[act][0];
if (ne == res.back() && gr[act].size() > 1) ne = gr[act][1];
res.push_back(act);
if (gr[act].size() == 1) {
break;
}
act = ne;
}
}
return res;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < reLo.size(); ++i) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:123:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i = 0; i < reoEn.size(); ++i) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 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 |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
5 ms |
352 KB |
Output is correct |
4 |
Correct |
6 ms |
356 KB |
Output is correct |
5 |
Correct |
5 ms |
608 KB |
Output is correct |
6 |
Correct |
11 ms |
348 KB |
Output is correct |
7 |
Correct |
12 ms |
352 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
6 ms |
456 KB |
Output is correct |
10 |
Correct |
5 ms |
352 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
608 KB |
Output is correct |
13 |
Correct |
5 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
352 KB |
Output is correct |
2 |
Correct |
12 ms |
352 KB |
Output is correct |
3 |
Correct |
5 ms |
624 KB |
Output is correct |
4 |
Correct |
5 ms |
356 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
12 ms |
348 KB |
Output is correct |
7 |
Correct |
8 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 |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
7 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
6 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
10 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 |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
608 KB |
Output is correct |
22 |
Correct |
6 ms |
464 KB |
Output is correct |
23 |
Correct |
7 ms |
716 KB |
Output is correct |
24 |
Correct |
5 ms |
456 KB |
Output is correct |
25 |
Correct |
10 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 |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
10 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
600 KB |
Output is correct |
31 |
Correct |
7 ms |
444 KB |
Output is correct |
32 |
Correct |
7 ms |
440 KB |
Output is correct |
33 |
Correct |
7 ms |
620 KB |
Output is correct |
34 |
Correct |
8 ms |
340 KB |
Output is correct |
35 |
Correct |
8 ms |
600 KB |
Output is correct |
36 |
Correct |
8 ms |
600 KB |
Output is correct |
37 |
Correct |
8 ms |
348 KB |
Output is correct |
38 |
Correct |
8 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
600 KB |
Output is correct |
40 |
Correct |
10 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
604 KB |
Output is correct |
42 |
Correct |
7 ms |
608 KB |
Output is correct |
43 |
Correct |
6 ms |
604 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
13 ms |
344 KB |
Output is correct |
47 |
Correct |
9 ms |
344 KB |
Output is correct |
48 |
Correct |
10 ms |
348 KB |
Output is correct |
49 |
Correct |
9 ms |
348 KB |
Output is correct |
50 |
Correct |
6 ms |
456 KB |
Output is correct |
51 |
Correct |
8 ms |
440 KB |
Output is correct |
52 |
Correct |
8 ms |
440 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
11 ms |
600 KB |
Output is correct |
55 |
Correct |
8 ms |
600 KB |
Output is correct |
56 |
Correct |
7 ms |
600 KB |
Output is correct |
57 |
Correct |
10 ms |
856 KB |
Output is correct |
58 |
Correct |
8 ms |
600 KB |
Output is correct |
59 |
Correct |
7 ms |
628 KB |
Output is correct |
60 |
Correct |
9 ms |
604 KB |
Output is correct |
61 |
Correct |
8 ms |
612 KB |
Output is correct |
62 |
Correct |
7 ms |
468 KB |
Output is correct |
63 |
Correct |
8 ms |
612 KB |
Output is correct |
64 |
Correct |
9 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
356 KB |
Output is correct |
3 |
Correct |
5 ms |
356 KB |
Output is correct |
4 |
Correct |
5 ms |
356 KB |
Output is correct |
5 |
Correct |
6 ms |
612 KB |
Output is correct |
6 |
Correct |
13 ms |
356 KB |
Output is correct |
7 |
Correct |
10 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
348 KB |
Output is correct |
9 |
Correct |
7 ms |
500 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
612 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
5 ms |
352 KB |
Output is correct |
14 |
Correct |
11 ms |
352 KB |
Output is correct |
15 |
Correct |
12 ms |
348 KB |
Output is correct |
16 |
Correct |
8 ms |
348 KB |
Output is correct |
17 |
Correct |
6 ms |
348 KB |
Output is correct |
18 |
Correct |
7 ms |
624 KB |
Output is correct |
19 |
Correct |
6 ms |
864 KB |
Output is correct |
20 |
Correct |
6 ms |
608 KB |
Output is correct |
21 |
Correct |
12 ms |
348 KB |
Output is correct |
22 |
Correct |
10 ms |
348 KB |
Output is correct |
23 |
Correct |
8 ms |
348 KB |
Output is correct |
24 |
Correct |
9 ms |
348 KB |
Output is correct |
25 |
Correct |
9 ms |
600 KB |
Output is correct |
26 |
Correct |
6 ms |
448 KB |
Output is correct |
27 |
Correct |
9 ms |
444 KB |
Output is correct |
28 |
Correct |
7 ms |
444 KB |
Output is correct |
29 |
Correct |
8 ms |
348 KB |
Output is correct |
30 |
Correct |
8 ms |
600 KB |
Output is correct |
31 |
Correct |
8 ms |
352 KB |
Output is correct |
32 |
Correct |
11 ms |
348 KB |
Output is correct |
33 |
Correct |
12 ms |
352 KB |
Output is correct |
34 |
Correct |
9 ms |
344 KB |
Output is correct |
35 |
Correct |
10 ms |
352 KB |
Output is correct |
36 |
Correct |
9 ms |
348 KB |
Output is correct |
37 |
Correct |
6 ms |
456 KB |
Output is correct |
38 |
Correct |
8 ms |
448 KB |
Output is correct |
39 |
Correct |
8 ms |
444 KB |
Output is correct |
40 |
Correct |
7 ms |
348 KB |
Output is correct |
41 |
Correct |
8 ms |
456 KB |
Output is correct |
42 |
Correct |
9 ms |
608 KB |
Output is correct |
43 |
Correct |
7 ms |
712 KB |
Output is correct |
44 |
Correct |
5 ms |
464 KB |
Output is correct |
45 |
Correct |
6 ms |
476 KB |
Output is correct |
46 |
Correct |
6 ms |
468 KB |
Output is correct |
47 |
Partially correct |
7 ms |
864 KB |
Output is partially correct |
48 |
Partially correct |
8 ms |
1116 KB |
Output is partially correct |
49 |
Partially correct |
9 ms |
612 KB |
Output is partially correct |
50 |
Partially correct |
6 ms |
604 KB |
Output is partially correct |
51 |
Partially correct |
7 ms |
456 KB |
Output is partially correct |
52 |
Correct |
7 ms |
452 KB |
Output is correct |
53 |
Correct |
6 ms |
608 KB |
Output is correct |
54 |
Correct |
7 ms |
612 KB |
Output is correct |
55 |
Correct |
6 ms |
608 KB |
Output is correct |
56 |
Partially correct |
10 ms |
612 KB |
Output is partially correct |
57 |
Partially correct |
8 ms |
608 KB |
Output is partially correct |
58 |
Partially correct |
9 ms |
608 KB |
Output is partially correct |
59 |
Partially correct |
8 ms |
608 KB |
Output is partially correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
7 ms |
604 KB |
Output is correct |
62 |
Correct |
5 ms |
604 KB |
Output is correct |
63 |
Partially correct |
9 ms |
604 KB |
Output is partially correct |
64 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
65 |
Partially correct |
8 ms |
504 KB |
Output is partially correct |
66 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
67 |
Correct |
7 ms |
460 KB |
Output is correct |
68 |
Correct |
7 ms |
604 KB |
Output is correct |
69 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
70 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
71 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
72 |
Partially correct |
9 ms |
452 KB |
Output is partially correct |
73 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
74 |
Partially correct |
8 ms |
856 KB |
Output is partially correct |
75 |
Correct |
9 ms |
852 KB |
Output is correct |
76 |
Correct |
8 ms |
880 KB |
Output is correct |
77 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
78 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
79 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
80 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
81 |
Correct |
7 ms |
604 KB |
Output is correct |
82 |
Correct |
8 ms |
604 KB |
Output is correct |