#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse4")
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
std::vector<int> longest_trip(int N, int D) {
vector<vector<int>> g(N);
if (D == 3) {
vector<int> v;
for (int i=0;i<N;i++) {
v.push_back(i);
}
return v;
}
vector<array<int,2>> pr((N*(N-1))/2);
int cnt=0;
for (int i=0;i<N;i++) {
for (int j=i+1;j<N;j++) {
pr[cnt++] = {i, j};
}
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(pr.begin(), pr.end(), rng);
for (auto [i, j] : pr) {
if (are_connected({i}, {j})) {
g[i].push_back(j);
g[j].push_back(i);
}
}
vector<bool> vv(N, false);
function<void(int)> df=[&](int node) {
vv[node]=true;
for (int &x:g[node]) {
if (!vv[x]) df(x);
}
};
df(0);
for (int i=0;i<N;i++) {
if (!vv[i]) {
vector<int> f1, f2;
for (int j=0;j<N;j++) {
if (vv[j])f1.push_back(j);
else f2.push_back(j);
}
if (f1.size() > f2.size()) return f1;
return f2;
}
}
vector<int> lng;
int hf = N/2;
if (N%2 != 0) hf++;
for (int i=0;i<N;i++) {
vector<int> tmp;
vector<bool> vis(N, false);
function<void(int)> dfs=[&](int node) {
vis[node] = true;
tmp.push_back(node);
if (lng.size() < tmp.size()) lng = tmp;
for (int x:g[node]) {
if (!vis[x]) {
dfs(x);
}
}
tmp.pop_back();
};
dfs(i);
if (lng.size() < tmp.size()) {
lng = tmp;
}
if ((D == 2 && (int)lng.size() == N) || (D == 1 && (int)lng.size() >= hf)) return lng;
}
return lng;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
203 ms |
1124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Correct |
147 ms |
592 KB |
Output is correct |
4 |
Correct |
416 ms |
592 KB |
Output is correct |
5 |
Correct |
853 ms |
1336 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Correct |
143 ms |
344 KB |
Output is correct |
9 |
Correct |
341 ms |
848 KB |
Output is correct |
10 |
Correct |
868 ms |
1280 KB |
Output is correct |
11 |
Correct |
855 ms |
1792 KB |
Output is correct |
12 |
Correct |
852 ms |
1324 KB |
Output is correct |
13 |
Correct |
870 ms |
1304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Correct |
153 ms |
452 KB |
Output is correct |
4 |
Correct |
374 ms |
664 KB |
Output is correct |
5 |
Correct |
894 ms |
1256 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
31 ms |
344 KB |
Output is correct |
8 |
Correct |
169 ms |
464 KB |
Output is correct |
9 |
Correct |
319 ms |
724 KB |
Output is correct |
10 |
Correct |
804 ms |
1140 KB |
Output is correct |
11 |
Correct |
861 ms |
2032 KB |
Output is correct |
12 |
Correct |
910 ms |
1392 KB |
Output is correct |
13 |
Correct |
870 ms |
1320 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
35 ms |
340 KB |
Output is correct |
17 |
Correct |
105 ms |
344 KB |
Output is correct |
18 |
Correct |
152 ms |
344 KB |
Output is correct |
19 |
Correct |
347 ms |
592 KB |
Output is correct |
20 |
Correct |
306 ms |
748 KB |
Output is correct |
21 |
Correct |
849 ms |
1204 KB |
Output is correct |
22 |
Correct |
862 ms |
1548 KB |
Output is correct |
23 |
Correct |
844 ms |
1784 KB |
Output is correct |
24 |
Correct |
889 ms |
1552 KB |
Output is correct |
25 |
Correct |
10 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
27 ms |
344 KB |
Output is correct |
28 |
Correct |
21 ms |
344 KB |
Output is correct |
29 |
Correct |
24 ms |
344 KB |
Output is correct |
30 |
Correct |
206 ms |
600 KB |
Output is correct |
31 |
Correct |
196 ms |
472 KB |
Output is correct |
32 |
Correct |
190 ms |
344 KB |
Output is correct |
33 |
Correct |
329 ms |
528 KB |
Output is correct |
34 |
Correct |
299 ms |
1016 KB |
Output is correct |
35 |
Correct |
323 ms |
344 KB |
Output is correct |
36 |
Correct |
868 ms |
1236 KB |
Output is correct |
37 |
Correct |
797 ms |
1700 KB |
Output is correct |
38 |
Correct |
834 ms |
1172 KB |
Output is correct |
39 |
Correct |
856 ms |
1496 KB |
Output is correct |
40 |
Correct |
840 ms |
1264 KB |
Output is correct |
41 |
Correct |
835 ms |
984 KB |
Output is correct |
42 |
Correct |
769 ms |
1048 KB |
Output is correct |
43 |
Correct |
871 ms |
1104 KB |
Output is correct |
44 |
Correct |
903 ms |
1356 KB |
Output is correct |
45 |
Correct |
13 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
22 ms |
344 KB |
Output is correct |
48 |
Correct |
21 ms |
340 KB |
Output is correct |
49 |
Correct |
22 ms |
344 KB |
Output is correct |
50 |
Correct |
182 ms |
456 KB |
Output is correct |
51 |
Correct |
229 ms |
456 KB |
Output is correct |
52 |
Correct |
215 ms |
692 KB |
Output is correct |
53 |
Correct |
310 ms |
768 KB |
Output is correct |
54 |
Correct |
317 ms |
344 KB |
Output is correct |
55 |
Correct |
322 ms |
592 KB |
Output is correct |
56 |
Correct |
811 ms |
1288 KB |
Output is correct |
57 |
Correct |
853 ms |
1264 KB |
Output is correct |
58 |
Correct |
810 ms |
1136 KB |
Output is correct |
59 |
Correct |
837 ms |
1336 KB |
Output is correct |
60 |
Correct |
899 ms |
1152 KB |
Output is correct |
61 |
Correct |
828 ms |
1312 KB |
Output is correct |
62 |
Correct |
885 ms |
1376 KB |
Output is correct |
63 |
Correct |
893 ms |
1344 KB |
Output is correct |
64 |
Correct |
879 ms |
1088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
596 KB |
Output is correct |
2 |
Correct |
29 ms |
344 KB |
Output is correct |
3 |
Partially correct |
137 ms |
448 KB |
Output is partially correct |
4 |
Partially correct |
397 ms |
592 KB |
Output is partially correct |
5 |
Partially correct |
869 ms |
1312 KB |
Output is partially correct |
6 |
Incorrect |
1 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |