// CF template, version 3.0
#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}
//#define int long long int
template <typename T, typename I>
struct segtree {
int n;
vector<T> tree;
vector<I> initial;
T id;
segtree(int i_n, vector<I> i_initial, T i_id): n(i_n), initial(i_initial), id(i_id) {
tree.resize(4 * n);
}
T conquer(T left, T right) {
// write your conquer function
}
T value(I inp) {
// write your value function
}
void build(int v, int l, int r) {
if (l == r) tree[v] = value(initial[l]);
else {
int middle = (l + r) / 2;
build(2 * v, l, middle);
build(2 * v + 1, middle + 1, r);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
void upd(int v, int l, int r, int i, I x) {
if (l == r) tree[v] = value(x);
else {
int middle = (l + r) / 2;
if (middle >= i) upd(2 * v, l, middle, i, x);
else upd(2 * v + 1, middle + 1, r, i, x);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
T query(int v, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return tree[v];
else if (r < ql || qr < l) return id;
int middle = (l + r) / 2;
T left = query(2 * v, l, middle, ql, qr);
T right = query(2 * v + 1, middle + 1, r, ql, qr);
return conquer(left, right);
}
};
// vector<int>
// Find the first node in Y (if any) connected to at least one node in X.
int bin_search(vector<int> A, vector<int> Y) {
int M = Y.size();
int l = 0;
int r = M;
while (r - l >= 1) {
int middle = (l + r) / 2;
vector<int> B;
forto(middle + 1, i) B.push_back(Y[i]);
if (are_connected(A, B)) r = middle;
else l = middle + 1;
}
if (l == M) return -1;
return Y[l];
}
vector<int> longest_trip(int N, int _D) {
vector<int> path;
vector<bool> other(N, true); // think of this as "free".
vector<int> A1;
A1.push_back(0);
vector<int> B1;
forto(N, i) if (i) B1.push_back(i);
int initial = bin_search(A1, B1);
if (initial == -1) {
return B1;
}
path.push_back(0);
path.push_back(initial);
other[0] = other[initial] = false;
while (path.size() < N) {
vector<int> A2;
vector<int> B2;
A2.push_back(path[0]);
B2.push_back(path.back());
if (are_connected(A2, B2)) {
vector<int> remain;
forto(N, i) if (other[i]) remain.push_back(i);
int first = bin_search(B2, remain);
if (first == -1) {
int firsthere = bin_search(remain, path);
if (firsthere == -1) {
if (path.size() >= (N + 1) / 2) return path;
return remain;
}
vector<int> A3;
A3.push_back(firsthere);
int firstout = bin_search(A3, remain);
vector<int> newpath;
for (int r: remain) if (r != firstout) newpath.push_back(r);
newpath.push_back(firstout);
int ind;
int s = path.size();
forto(s, i) if (path[i] == firsthere) ind = i;
for (int i = ind; i < s; i++) newpath.push_back(path[i]);
forto(ind, i) newpath.push_back(path[i]);
return newpath;
} else {
path.push_back(first);
other[first] = false;
}
} else {
int last;
forto(N, i) if (other[i]) last = i;
vector<int> A3;
vector<int> B3;
A3.push_back(path[0]);
B3.push_back(last);
if (are_connected(A3, B3)) {
rev(path);
path.push_back(last);
} else {
path.push_back(last);
}
other[last] = false;
}
}
return path;
}
Compilation message
longesttrip.cpp: In function 'int bin_search(std::vector<int>, std::vector<int>)':
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:80:9: note: in expansion of macro 'forto'
80 | forto(middle + 1, i) B.push_back(Y[i]);
| ^~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:94:5: note: in expansion of macro 'forto'
94 | forto(N, i) if (i) B1.push_back(i);
| ^~~~~
longesttrip.cpp:102:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | while (path.size() < N) {
| ~~~~~~~~~~~~^~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:109:13: note: in expansion of macro 'forto'
109 | forto(N, i) if (other[i]) remain.push_back(i);
| ^~~~~
longesttrip.cpp:114:37: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if (path.size() >= (N + 1) / 2) return path;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:125:17: note: in expansion of macro 'forto'
125 | forto(s, i) if (path[i] == firsthere) ind = i;
| ^~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:127:17: note: in expansion of macro 'forto'
127 | forto(ind, i) newpath.push_back(path[i]);
| ^~~~~
longesttrip.cpp:16:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
16 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
longesttrip.cpp:135:13: note: in expansion of macro 'forto'
135 | forto(N, i) if (other[i]) last = i;
| ^~~~~
longesttrip.cpp:123:21: warning: 'ind' may be used uninitialized in this function [-Wmaybe-uninitialized]
123 | int ind;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
344 KB |
Output is correct |
3 |
Correct |
26 ms |
344 KB |
Output is correct |
4 |
Correct |
41 ms |
436 KB |
Output is correct |
5 |
Correct |
54 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
15 ms |
344 KB |
Output is correct |
3 |
Correct |
24 ms |
344 KB |
Output is correct |
4 |
Correct |
40 ms |
344 KB |
Output is correct |
5 |
Correct |
56 ms |
464 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
19 ms |
344 KB |
Output is correct |
8 |
Correct |
32 ms |
600 KB |
Output is correct |
9 |
Correct |
41 ms |
344 KB |
Output is correct |
10 |
Correct |
43 ms |
344 KB |
Output is correct |
11 |
Correct |
44 ms |
344 KB |
Output is correct |
12 |
Correct |
54 ms |
344 KB |
Output is correct |
13 |
Correct |
53 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
344 KB |
Output is correct |
3 |
Correct |
30 ms |
344 KB |
Output is correct |
4 |
Correct |
35 ms |
344 KB |
Output is correct |
5 |
Correct |
40 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
14 ms |
344 KB |
Output is correct |
8 |
Correct |
23 ms |
344 KB |
Output is correct |
9 |
Correct |
30 ms |
344 KB |
Output is correct |
10 |
Correct |
43 ms |
344 KB |
Output is correct |
11 |
Correct |
44 ms |
344 KB |
Output is correct |
12 |
Correct |
51 ms |
452 KB |
Output is correct |
13 |
Correct |
45 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
20 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
344 KB |
Output is correct |
18 |
Correct |
19 ms |
344 KB |
Output is correct |
19 |
Correct |
32 ms |
344 KB |
Output is correct |
20 |
Correct |
22 ms |
344 KB |
Output is correct |
21 |
Correct |
36 ms |
344 KB |
Output is correct |
22 |
Correct |
33 ms |
596 KB |
Output is correct |
23 |
Correct |
45 ms |
464 KB |
Output is correct |
24 |
Correct |
32 ms |
468 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
15 ms |
344 KB |
Output is correct |
28 |
Correct |
16 ms |
344 KB |
Output is correct |
29 |
Correct |
13 ms |
344 KB |
Output is correct |
30 |
Correct |
35 ms |
344 KB |
Output is correct |
31 |
Correct |
26 ms |
344 KB |
Output is correct |
32 |
Correct |
20 ms |
344 KB |
Output is correct |
33 |
Correct |
29 ms |
344 KB |
Output is correct |
34 |
Correct |
27 ms |
344 KB |
Output is correct |
35 |
Correct |
23 ms |
344 KB |
Output is correct |
36 |
Correct |
44 ms |
344 KB |
Output is correct |
37 |
Correct |
40 ms |
344 KB |
Output is correct |
38 |
Correct |
52 ms |
600 KB |
Output is correct |
39 |
Correct |
49 ms |
596 KB |
Output is correct |
40 |
Correct |
35 ms |
464 KB |
Output is correct |
41 |
Correct |
32 ms |
344 KB |
Output is correct |
42 |
Correct |
31 ms |
344 KB |
Output is correct |
43 |
Correct |
29 ms |
344 KB |
Output is correct |
44 |
Correct |
31 ms |
600 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
21 ms |
344 KB |
Output is correct |
48 |
Correct |
13 ms |
344 KB |
Output is correct |
49 |
Correct |
14 ms |
344 KB |
Output is correct |
50 |
Correct |
25 ms |
344 KB |
Output is correct |
51 |
Correct |
16 ms |
344 KB |
Output is correct |
52 |
Correct |
20 ms |
344 KB |
Output is correct |
53 |
Correct |
24 ms |
344 KB |
Output is correct |
54 |
Correct |
26 ms |
344 KB |
Output is correct |
55 |
Correct |
22 ms |
344 KB |
Output is correct |
56 |
Correct |
45 ms |
600 KB |
Output is correct |
57 |
Correct |
35 ms |
592 KB |
Output is correct |
58 |
Correct |
38 ms |
468 KB |
Output is correct |
59 |
Correct |
41 ms |
700 KB |
Output is correct |
60 |
Correct |
44 ms |
596 KB |
Output is correct |
61 |
Correct |
32 ms |
344 KB |
Output is correct |
62 |
Correct |
45 ms |
344 KB |
Output is correct |
63 |
Correct |
56 ms |
344 KB |
Output is correct |
64 |
Correct |
37 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
344 KB |
Output is correct |
3 |
Correct |
22 ms |
344 KB |
Output is correct |
4 |
Partially correct |
44 ms |
344 KB |
Output is partially correct |
5 |
Partially correct |
58 ms |
708 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
15 ms |
344 KB |
Output is correct |
8 |
Correct |
26 ms |
344 KB |
Output is correct |
9 |
Partially correct |
33 ms |
344 KB |
Output is partially correct |
10 |
Partially correct |
47 ms |
468 KB |
Output is partially correct |
11 |
Partially correct |
44 ms |
344 KB |
Output is partially correct |
12 |
Partially correct |
42 ms |
604 KB |
Output is partially correct |
13 |
Partially correct |
49 ms |
700 KB |
Output is partially correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
344 KB |
Output is correct |
18 |
Correct |
26 ms |
344 KB |
Output is correct |
19 |
Partially correct |
35 ms |
344 KB |
Output is partially correct |
20 |
Partially correct |
36 ms |
344 KB |
Output is partially correct |
21 |
Correct |
8 ms |
344 KB |
Output is correct |
22 |
Correct |
10 ms |
344 KB |
Output is correct |
23 |
Correct |
17 ms |
344 KB |
Output is correct |
24 |
Correct |
20 ms |
344 KB |
Output is correct |
25 |
Correct |
16 ms |
344 KB |
Output is correct |
26 |
Correct |
37 ms |
344 KB |
Output is correct |
27 |
Correct |
26 ms |
344 KB |
Output is correct |
28 |
Correct |
17 ms |
344 KB |
Output is correct |
29 |
Partially correct |
30 ms |
344 KB |
Output is partially correct |
30 |
Partially correct |
41 ms |
344 KB |
Output is partially correct |
31 |
Partially correct |
26 ms |
344 KB |
Output is partially correct |
32 |
Correct |
13 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
23 ms |
344 KB |
Output is correct |
35 |
Correct |
19 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
340 KB |
Output is correct |
37 |
Correct |
23 ms |
344 KB |
Output is correct |
38 |
Correct |
26 ms |
344 KB |
Output is correct |
39 |
Correct |
17 ms |
344 KB |
Output is correct |
40 |
Partially correct |
33 ms |
340 KB |
Output is partially correct |
41 |
Partially correct |
25 ms |
596 KB |
Output is partially correct |
42 |
Partially correct |
34 ms |
344 KB |
Output is partially correct |
43 |
Partially correct |
44 ms |
600 KB |
Output is partially correct |
44 |
Partially correct |
44 ms |
440 KB |
Output is partially correct |
45 |
Partially correct |
38 ms |
448 KB |
Output is partially correct |
46 |
Partially correct |
48 ms |
344 KB |
Output is partially correct |
47 |
Partially correct |
51 ms |
600 KB |
Output is partially correct |
48 |
Partially correct |
36 ms |
600 KB |
Output is partially correct |
49 |
Partially correct |
61 ms |
448 KB |
Output is partially correct |
50 |
Partially correct |
36 ms |
600 KB |
Output is partially correct |
51 |
Partially correct |
51 ms |
344 KB |
Output is partially correct |
52 |
Partially correct |
32 ms |
344 KB |
Output is partially correct |
53 |
Partially correct |
34 ms |
472 KB |
Output is partially correct |
54 |
Partially correct |
28 ms |
344 KB |
Output is partially correct |
55 |
Partially correct |
37 ms |
600 KB |
Output is partially correct |
56 |
Partially correct |
41 ms |
344 KB |
Output is partially correct |
57 |
Partially correct |
46 ms |
448 KB |
Output is partially correct |
58 |
Partially correct |
30 ms |
460 KB |
Output is partially correct |
59 |
Partially correct |
28 ms |
472 KB |
Output is partially correct |
60 |
Partially correct |
30 ms |
344 KB |
Output is partially correct |
61 |
Partially correct |
33 ms |
344 KB |
Output is partially correct |
62 |
Partially correct |
52 ms |
344 KB |
Output is partially correct |
63 |
Partially correct |
41 ms |
344 KB |
Output is partially correct |
64 |
Partially correct |
23 ms |
344 KB |
Output is partially correct |
65 |
Partially correct |
36 ms |
344 KB |
Output is partially correct |
66 |
Partially correct |
30 ms |
600 KB |
Output is partially correct |
67 |
Partially correct |
26 ms |
600 KB |
Output is partially correct |
68 |
Partially correct |
31 ms |
448 KB |
Output is partially correct |
69 |
Partially correct |
40 ms |
592 KB |
Output is partially correct |
70 |
Partially correct |
41 ms |
344 KB |
Output is partially correct |
71 |
Partially correct |
42 ms |
344 KB |
Output is partially correct |
72 |
Partially correct |
29 ms |
344 KB |
Output is partially correct |
73 |
Partially correct |
23 ms |
344 KB |
Output is partially correct |
74 |
Partially correct |
34 ms |
600 KB |
Output is partially correct |
75 |
Partially correct |
42 ms |
600 KB |
Output is partially correct |
76 |
Partially correct |
30 ms |
344 KB |
Output is partially correct |
77 |
Partially correct |
40 ms |
600 KB |
Output is partially correct |
78 |
Partially correct |
34 ms |
344 KB |
Output is partially correct |
79 |
Partially correct |
41 ms |
344 KB |
Output is partially correct |
80 |
Partially correct |
37 ms |
600 KB |
Output is partially correct |
81 |
Partially correct |
28 ms |
344 KB |
Output is partially correct |
82 |
Partially correct |
31 ms |
444 KB |
Output is partially correct |