#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 ncSt = se.back();
se.pop_back();
int loopSt = st, loopEn = st, oEnSt = oSt, oEnEn = oSt, oNC = ncSt, ind1 = -1;
g_t gr(N);
while (!se.empty() || oNC != -1 || ind1 != -1) {
if (oNC == -1 && !se.empty()) {
oNC = se.back();
se.pop_back();
} else if (ind1 == -1 && !se.empty()) {
ind1 = se.back();
se.pop_back();
} else {
if (ind1 == -1) {
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;
}
}
} else {
if (!are_connected({oEnEn}, {oNC, ind1})) {
gr[oNC].push_back(ind1);
gr[ind1].push_back(oNC);
if (are_connected({oNC}, {loopEn})) {
gr[loopEn].push_back(oNC);
gr[oNC].push_back(loopEn);
loopEn = ind1;
oNC = ind1 = -1;
} else {
gr[loopEn].push_back(oEnEn);
gr[oEnEn].push_back(loopEn);
loopEn = oEnSt;
oEnSt = ind1;
oEnEn = oNC;
oNC = ind1 = -1;
}
} else {
if (are_connected({oEnEn}, {oNC})) {
gr[oEnEn].push_back(oNC);
gr[oNC].push_back(oEnEn);
oEnEn = oNC;
oNC = ind1;
ind1 = -1;
} else {
gr[oEnEn].push_back(ind1);
gr[ind1].push_back(oEnEn);
oEnEn = ind1;
ind1 = -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:159:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for (int i = 0; i < reLo.size(); ++i) {
| ~~^~~~~~~~~~~~~
longesttrip.cpp:162:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for (int i = 0; i < reoEn.size(); ++i) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
10 ms |
596 KB |
Output is correct |
5 |
Correct |
9 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
12 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
344 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
344 KB |
Output is correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
9 ms |
344 KB |
Output is correct |
9 |
Correct |
9 ms |
344 KB |
Output is correct |
10 |
Correct |
9 ms |
456 KB |
Output is correct |
11 |
Correct |
10 ms |
344 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
12 ms |
864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
9 ms |
344 KB |
Output is correct |
4 |
Correct |
11 ms |
344 KB |
Output is correct |
5 |
Correct |
10 ms |
856 KB |
Output is correct |
6 |
Correct |
13 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
9 ms |
344 KB |
Output is correct |
10 |
Correct |
9 ms |
600 KB |
Output is correct |
11 |
Correct |
10 ms |
600 KB |
Output is correct |
12 |
Correct |
9 ms |
600 KB |
Output is correct |
13 |
Correct |
10 ms |
600 KB |
Output is correct |
14 |
Correct |
12 ms |
344 KB |
Output is correct |
15 |
Correct |
14 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
344 KB |
Output is correct |
19 |
Correct |
9 ms |
600 KB |
Output is correct |
20 |
Correct |
9 ms |
344 KB |
Output is correct |
21 |
Correct |
9 ms |
608 KB |
Output is correct |
22 |
Correct |
9 ms |
600 KB |
Output is correct |
23 |
Correct |
10 ms |
856 KB |
Output is correct |
24 |
Correct |
10 ms |
604 KB |
Output is correct |
25 |
Correct |
13 ms |
344 KB |
Output is correct |
26 |
Correct |
14 ms |
596 KB |
Output is correct |
27 |
Correct |
11 ms |
344 KB |
Output is correct |
28 |
Correct |
11 ms |
344 KB |
Output is correct |
29 |
Correct |
12 ms |
344 KB |
Output is correct |
30 |
Correct |
9 ms |
444 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
444 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
600 KB |
Output is correct |
36 |
Correct |
9 ms |
856 KB |
Output is correct |
37 |
Correct |
7 ms |
600 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
452 KB |
Output is correct |
40 |
Correct |
7 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
9 ms |
600 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
8 ms |
452 KB |
Output is correct |
45 |
Correct |
15 ms |
344 KB |
Output is correct |
46 |
Correct |
13 ms |
344 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
12 ms |
344 KB |
Output is correct |
49 |
Correct |
11 ms |
344 KB |
Output is correct |
50 |
Correct |
9 ms |
444 KB |
Output is correct |
51 |
Correct |
8 ms |
448 KB |
Output is correct |
52 |
Correct |
8 ms |
440 KB |
Output is correct |
53 |
Correct |
8 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
12 ms |
600 KB |
Output is correct |
57 |
Correct |
7 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
600 KB |
Output is correct |
60 |
Correct |
8 ms |
600 KB |
Output is correct |
61 |
Correct |
8 ms |
604 KB |
Output is correct |
62 |
Correct |
7 ms |
604 KB |
Output is correct |
63 |
Correct |
8 ms |
600 KB |
Output is correct |
64 |
Correct |
8 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
10 ms |
344 KB |
Output is correct |
5 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
6 |
Correct |
13 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
9 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
344 KB |
Output is correct |
10 |
Partially correct |
13 ms |
600 KB |
Output is partially correct |
11 |
Partially correct |
12 ms |
856 KB |
Output is partially correct |
12 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
13 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
14 |
Correct |
12 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
9 ms |
344 KB |
Output is correct |
19 |
Correct |
10 ms |
856 KB |
Output is correct |
20 |
Correct |
11 ms |
600 KB |
Output is correct |
21 |
Correct |
10 ms |
344 KB |
Output is correct |
22 |
Correct |
12 ms |
344 KB |
Output is correct |
23 |
Correct |
9 ms |
344 KB |
Output is correct |
24 |
Correct |
10 ms |
344 KB |
Output is correct |
25 |
Correct |
10 ms |
532 KB |
Output is correct |
26 |
Correct |
8 ms |
444 KB |
Output is correct |
27 |
Correct |
6 ms |
444 KB |
Output is correct |
28 |
Correct |
8 ms |
444 KB |
Output is correct |
29 |
Correct |
9 ms |
444 KB |
Output is correct |
30 |
Correct |
8 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
600 KB |
Output is correct |
32 |
Correct |
13 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
15 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
344 KB |
Output is correct |
37 |
Correct |
9 ms |
440 KB |
Output is correct |
38 |
Correct |
8 ms |
444 KB |
Output is correct |
39 |
Correct |
8 ms |
448 KB |
Output is correct |
40 |
Correct |
9 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 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
44 |
Partially correct |
9 ms |
460 KB |
Output is partially correct |
45 |
Partially correct |
9 ms |
856 KB |
Output is partially correct |
46 |
Partially correct |
9 ms |
460 KB |
Output is partially correct |
47 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
48 |
Correct |
6 ms |
600 KB |
Output is correct |
49 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
50 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
51 |
Correct |
7 ms |
600 KB |
Output is correct |
52 |
Correct |
7 ms |
452 KB |
Output is correct |
53 |
Correct |
6 ms |
596 KB |
Output is correct |
54 |
Correct |
7 ms |
600 KB |
Output is correct |
55 |
Correct |
7 ms |
600 KB |
Output is correct |
56 |
Correct |
6 ms |
600 KB |
Output is correct |
57 |
Partially correct |
7 ms |
856 KB |
Output is partially correct |
58 |
Partially correct |
7 ms |
604 KB |
Output is partially correct |
59 |
Partially correct |
6 ms |
608 KB |
Output is partially correct |
60 |
Correct |
6 ms |
608 KB |
Output is correct |
61 |
Correct |
7 ms |
356 KB |
Output is correct |
62 |
Partially correct |
10 ms |
868 KB |
Output is partially correct |
63 |
Correct |
6 ms |
356 KB |
Output is correct |
64 |
Partially correct |
7 ms |
980 KB |
Output is partially correct |
65 |
Partially correct |
9 ms |
864 KB |
Output is partially correct |
66 |
Partially correct |
8 ms |
352 KB |
Output is partially correct |
67 |
Partially correct |
7 ms |
460 KB |
Output is partially correct |
68 |
Partially correct |
7 ms |
460 KB |
Output is partially correct |
69 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
70 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
71 |
Correct |
6 ms |
344 KB |
Output is correct |
72 |
Correct |
8 ms |
880 KB |
Output is correct |
73 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
74 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
75 |
Correct |
8 ms |
608 KB |
Output is correct |
76 |
Correct |
9 ms |
712 KB |
Output is correct |
77 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
78 |
Correct |
6 ms |
344 KB |
Output is correct |
79 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
80 |
Partially correct |
9 ms |
856 KB |
Output is partially correct |
81 |
Correct |
8 ms |
456 KB |
Output is correct |
82 |
Correct |
7 ms |
604 KB |
Output is correct |