#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({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;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 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 |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
7 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
7 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
456 KB |
Output is correct |
11 |
Correct |
5 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
452 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
8 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 |
5 ms |
460 KB |
Output is correct |
11 |
Correct |
5 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
456 KB |
Output is correct |
13 |
Correct |
6 ms |
448 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
444 KB |
Output is correct |
19 |
Correct |
6 ms |
544 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
712 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
23 |
Correct |
6 ms |
860 KB |
Output is correct |
24 |
Correct |
6 ms |
608 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
7 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
440 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
344 KB |
Output is correct |
34 |
Correct |
6 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
600 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
600 KB |
Output is correct |
42 |
Correct |
8 ms |
600 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
8 ms |
600 KB |
Output is correct |
45 |
Incorrect |
1 ms |
496 KB |
non-disjoint arrays |
46 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 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 |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
448 KB |
Output is correct |
11 |
Correct |
6 ms |
456 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
6 ms |
456 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
500 KB |
Output is correct |
18 |
Correct |
9 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
552 KB |
Output is correct |
22 |
Correct |
8 ms |
596 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
7 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 |
6 ms |
436 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 |
6 ms |
344 KB |
Output is correct |
32 |
Incorrect |
1 ms |
344 KB |
non-disjoint arrays |
33 |
Halted |
0 ms |
0 KB |
- |