#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> lines;
vector<int> chk;
vector<int> head;
bool ask(int u, int v) {
if (u == v) return true;
return are_connected({u}, {v});
}
int Get() {
for (int i=0; i<n; i++) {
if (!chk[i]) return i;
}
return -1;
}
int Get2() {
for (int i=0; i<n; i++) {
if (head[i]) return i;
}
return -1;
}
void make_groups() {
chk[0] = 1;
while (Get() != -1) {
int a = Get();
chk[a] = 1;
int b = Get();
if (b == -1) break;
chk[b] = 1;
if (ask(a, b)) {
lines[a].push_back(b);
head[b] = 0;
continue;
}
if (ask(a, lines[0].back())) swap(a, b);
lines[0].push_back(b);
head[b] = 0;
int c = Get();
if (c == -1) break;
chk[c] = 1;
if (ask(b, c)) {
lines[0].push_back(c);
head[c] = 0;
chk[a] = 0;
continue;
}
lines[c].push_back(a);
head[a] = 0;
int d = Get();
if (d == -1) break;
chk[d] = 1;
if (ask(b, d)) {
lines[0].push_back(d);
head[d] = 0;
continue;
}
lines[c].push_back(d);
head[d] = 0;
}
}
vector<int> longest_trip(int N, int D) {
n = N;
lines.resize(n);
for (int i=0; i<n; i++) {
lines[i].clear();
lines[i].push_back(i);
}
chk.resize(n);
fill(chk.begin(), chk.end(), 0);
head.resize(n);
fill(head.begin(), head.end(), 1);
make_groups();
int l1 = 0;
head[0] = 0;
int l2 = Get2();
if (l2 == -1) return lines[0];
head[l2] = 0;
while (Get2() != -1) {
int a = Get2();
head[a] = 0;
if (ask(lines[l1].back(), a)) {
for (auto &i : lines[a]) {
lines[l1].push_back(i);
}
continue;
}
if (ask(lines[l2].back(), a)) {
for (auto &i : lines[a]) {
lines[l2].push_back(i);
}
continue;
}
reverse(lines[l2].begin(), lines[l2].end());
for (auto &i : lines[l2]) {
lines[l1].push_back(i);
}
l2 = a;
}
if (!are_connected(lines[l1], lines[l2])) {
if (lines[l1].size() >= lines[l2].size()) return lines[l1];
return lines[l2];
}
if (!ask(lines[l1][0], lines[l1].back())) {
if (ask(lines[l1][0], lines[l2][0])) {
reverse(lines[l1].begin(), lines[l1].end());
}
for (auto &i : lines[l2]) {
lines[l1].push_back(i);
}
return lines[l1];
}
if (!ask(lines[l2][0], lines[l2].back())) {
if (ask(lines[l1][0], lines[l2][0])) {
reverse(lines[l2].begin(), lines[l2].end());
}
for (auto &i : lines[l1]) {
lines[l2].push_back(i);
}
return lines[l2];
}
int lo = 0, hi = (int)lines[l1].size() - 1;
while (lo < hi) {
int mid = lo + hi >> 1;
vector<int> t(lines[l1].begin(), lines[l1].begin() + mid + 1);
if (are_connected(t, lines[l2])) {
hi = mid;
}
else {
lo = mid + 1;
}
}
int s1 = lo;
lo = 0; hi = (int)lines[l2].size() - 1;
while (lo < hi) {
int mid = lo + hi >> 1;
vector<int> t(lines[l2].begin(), lines[l2].begin() + mid + 1);
if (are_connected({lines[l1][s1]}, t)) {
hi = mid;
}
else {
lo = mid + 1;
}
}
int s2 = lo;
vector<int> ans;
int sz = (int)lines[l1].size();
for (int i=0; i<sz; i++) {
ans.push_back(lines[l1][(s1+i+1)%sz]);
}
sz = (int)lines[l2].size();
for (int i=0; i<sz; i++) {
ans.push_back(lines[l2][(s2+i)%sz]);
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:132:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
132 | int mid = lo + hi >> 1;
| ~~~^~~~
longesttrip.cpp:144:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
144 | int mid = lo + hi >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
10 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
340 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
452 KB |
Output is correct |
11 |
Correct |
5 ms |
456 KB |
Output is correct |
12 |
Correct |
5 ms |
452 KB |
Output is correct |
13 |
Correct |
5 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
800 KB |
Output is correct |
11 |
Correct |
6 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
708 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
600 KB |
Output is correct |
19 |
Correct |
7 ms |
852 KB |
Output is correct |
20 |
Correct |
6 ms |
600 KB |
Output is correct |
21 |
Correct |
5 ms |
712 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
23 |
Correct |
5 ms |
604 KB |
Output is correct |
24 |
Correct |
5 ms |
1112 KB |
Output is correct |
25 |
Correct |
6 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
6 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 |
6 ms |
596 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
6 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
504 KB |
Output is correct |
36 |
Correct |
6 ms |
600 KB |
Output is correct |
37 |
Correct |
7 ms |
608 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
6 ms |
344 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
6 ms |
600 KB |
Output is correct |
43 |
Correct |
8 ms |
456 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
8 ms |
344 KB |
Output is correct |
46 |
Correct |
12 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
8 ms |
344 KB |
Output is correct |
49 |
Correct |
12 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
440 KB |
Output is correct |
51 |
Correct |
8 ms |
440 KB |
Output is correct |
52 |
Correct |
9 ms |
436 KB |
Output is correct |
53 |
Correct |
7 ms |
852 KB |
Output is correct |
54 |
Correct |
6 ms |
448 KB |
Output is correct |
55 |
Correct |
7 ms |
696 KB |
Output is correct |
56 |
Correct |
6 ms |
968 KB |
Output is correct |
57 |
Correct |
7 ms |
720 KB |
Output is correct |
58 |
Correct |
7 ms |
872 KB |
Output is correct |
59 |
Correct |
7 ms |
612 KB |
Output is correct |
60 |
Correct |
8 ms |
612 KB |
Output is correct |
61 |
Correct |
7 ms |
604 KB |
Output is correct |
62 |
Correct |
7 ms |
716 KB |
Output is correct |
63 |
Correct |
7 ms |
864 KB |
Output is correct |
64 |
Correct |
9 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
624 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
6 ms |
456 KB |
Output is correct |
12 |
Correct |
5 ms |
448 KB |
Output is correct |
13 |
Correct |
5 ms |
604 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
440 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
440 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
5 ms |
344 KB |
Output is correct |
32 |
Correct |
13 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 |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
440 KB |
Output is correct |
38 |
Correct |
7 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
436 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
6 ms |
448 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
456 KB |
Output is correct |
44 |
Correct |
5 ms |
708 KB |
Output is correct |
45 |
Correct |
6 ms |
456 KB |
Output is correct |
46 |
Correct |
5 ms |
608 KB |
Output is correct |
47 |
Correct |
6 ms |
540 KB |
Output is correct |
48 |
Correct |
6 ms |
600 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
452 KB |
Output is correct |
51 |
Correct |
6 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
600 KB |
Output is correct |
54 |
Correct |
7 ms |
600 KB |
Output is correct |
55 |
Correct |
8 ms |
600 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
600 KB |
Output is correct |
58 |
Correct |
6 ms |
340 KB |
Output is correct |
59 |
Correct |
7 ms |
600 KB |
Output is correct |
60 |
Correct |
6 ms |
600 KB |
Output is correct |
61 |
Correct |
6 ms |
600 KB |
Output is correct |
62 |
Correct |
5 ms |
724 KB |
Output is correct |
63 |
Correct |
6 ms |
720 KB |
Output is correct |
64 |
Correct |
7 ms |
604 KB |
Output is correct |
65 |
Correct |
9 ms |
724 KB |
Output is correct |
66 |
Correct |
7 ms |
860 KB |
Output is correct |
67 |
Correct |
8 ms |
600 KB |
Output is correct |
68 |
Correct |
7 ms |
716 KB |
Output is correct |
69 |
Correct |
7 ms |
888 KB |
Output is correct |
70 |
Correct |
7 ms |
600 KB |
Output is correct |
71 |
Correct |
6 ms |
712 KB |
Output is correct |
72 |
Correct |
7 ms |
724 KB |
Output is correct |
73 |
Correct |
8 ms |
856 KB |
Output is correct |
74 |
Correct |
9 ms |
852 KB |
Output is correct |
75 |
Correct |
8 ms |
864 KB |
Output is correct |
76 |
Correct |
7 ms |
708 KB |
Output is correct |
77 |
Correct |
7 ms |
724 KB |
Output is correct |
78 |
Correct |
6 ms |
856 KB |
Output is correct |
79 |
Correct |
8 ms |
608 KB |
Output is correct |
80 |
Correct |
7 ms |
712 KB |
Output is correct |
81 |
Correct |
8 ms |
456 KB |
Output is correct |
82 |
Correct |
8 ms |
712 KB |
Output is correct |