#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
const int N = 200005;
std::vector<int> longest_trip(int n, int D)
{
if(D == 3){
vector<int> v;
for(int i = 0; i < n; ++i) v.pb(i);
return v;
}
if(D == 2){
deque<int> v;
v.pb(0);
int o = -1;
for(int i = 1; i < n; ++i){
bool x = are_connected(vi{0}, vi{i});
if(x){
o = i;
v.pb(i);
break;
}
}
if(o == -1){
vi res;
for(int i = 1; i < n; ++i){
res.pb(i);
}
return res;
}
for(int i = 1; i < n; ++i){
if(o == i) continue;
bool x = are_connected(vi{v[0]}, vi{i});
if(x){
v.push_front(i);
}else v.pb(i);
}
vector<int> res(all(v));
return res;
}
vector<vector<int>> g(n);
vector<vector<bool>> is(n, vector<bool>(n));
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
bool ok = are_connected(vi{i}, vi{j});
if(ok){
is[i][j] = is[j][i] = 1;
g[i].pb(j);
g[j].pb(i);
}
}
}
vector<vector<int>> S;
for(int i = 0; i < n; ++i){
if(g[i].empty()){
vi res;
for(int j = 0; j < n; ++j){
if(j != i) res.pb(j);
}
return res;
}
bool okk = 0;
for(auto &s: S){
bool ok = 1;
for(int x: s){
if(is[x][i] == 0) ok = 0;
}
if(ok == 1){
s.pb(i);
okk = 1;
break;
}
}
if(okk == 0){
S.pb(vi{i});
}
}
if(S.size() == 1) return S[0];
if(S.size() == 2){
for(int &x: S[0]){
for(int &y: S[1]){
if(is[x][y]){
swap(x, S[0].back());
swap(y, S[1][0]);
vi res;
for(int f: S[0]) res.pb(f);
for(int f: S[1]) res.pb(f);
return res;
}
}
}
if(S[0].size() > S[1].size()) return S[0];
return S[1];
}
deque<int> a, b;
a.pb(0);
for(int i = 1; i < n; ++i){
if(is[a[0]][i]){
a.push_front(i);
}else if(is[a.back()][i]){
a.pb(i);
}else{
if(b.empty()) b.pb(i);
else{
if(is[b[0]][i]){
b.push_front(i);
}else if(is[b.back()][i]){
b.pb(i);
}else{
if(is[a[0]][b[0]]){
for(int x: b) a.push_front(x);
b.clear();
b.pb(i);
}else{
assert(false);
}
}
}
}
}
if(a.size() < b.size()) swap(a, b);
vector<int> res(all(a));
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
217 ms |
736 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
5 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 |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
31 ms |
344 KB |
Output is correct |
3 |
Correct |
167 ms |
344 KB |
Output is correct |
4 |
Correct |
438 ms |
888 KB |
Output is correct |
5 |
Correct |
852 ms |
976 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Correct |
161 ms |
452 KB |
Output is correct |
9 |
Correct |
314 ms |
744 KB |
Output is correct |
10 |
Correct |
810 ms |
1040 KB |
Output is correct |
11 |
Correct |
849 ms |
976 KB |
Output is correct |
12 |
Correct |
842 ms |
988 KB |
Output is correct |
13 |
Correct |
891 ms |
892 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
15 ms |
344 KB |
Output is correct |
16 |
Correct |
42 ms |
344 KB |
Output is correct |
17 |
Correct |
99 ms |
440 KB |
Output is correct |
18 |
Correct |
136 ms |
444 KB |
Output is correct |
19 |
Correct |
292 ms |
756 KB |
Output is correct |
20 |
Correct |
283 ms |
616 KB |
Output is correct |
21 |
Correct |
844 ms |
1072 KB |
Output is correct |
22 |
Correct |
838 ms |
1424 KB |
Output is correct |
23 |
Correct |
850 ms |
1328 KB |
Output is correct |
24 |
Correct |
843 ms |
1132 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
25 ms |
408 KB |
Output is correct |
28 |
Correct |
23 ms |
588 KB |
Output is correct |
29 |
Correct |
22 ms |
344 KB |
Output is correct |
30 |
Correct |
178 ms |
600 KB |
Output is correct |
31 |
Correct |
215 ms |
344 KB |
Output is correct |
32 |
Correct |
180 ms |
436 KB |
Output is correct |
33 |
Correct |
293 ms |
740 KB |
Output is correct |
34 |
Correct |
294 ms |
476 KB |
Output is correct |
35 |
Correct |
324 ms |
732 KB |
Output is correct |
36 |
Correct |
787 ms |
1016 KB |
Output is correct |
37 |
Correct |
819 ms |
712 KB |
Output is correct |
38 |
Correct |
798 ms |
860 KB |
Output is correct |
39 |
Correct |
782 ms |
1044 KB |
Output is correct |
40 |
Correct |
825 ms |
1084 KB |
Output is correct |
41 |
Correct |
871 ms |
1236 KB |
Output is correct |
42 |
Correct |
837 ms |
1216 KB |
Output is correct |
43 |
Correct |
856 ms |
1012 KB |
Output is correct |
44 |
Correct |
849 ms |
1084 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
23 ms |
344 KB |
Output is correct |
48 |
Correct |
22 ms |
344 KB |
Output is correct |
49 |
Correct |
23 ms |
344 KB |
Output is correct |
50 |
Correct |
210 ms |
344 KB |
Output is correct |
51 |
Correct |
174 ms |
440 KB |
Output is correct |
52 |
Correct |
193 ms |
344 KB |
Output is correct |
53 |
Correct |
311 ms |
464 KB |
Output is correct |
54 |
Correct |
319 ms |
592 KB |
Output is correct |
55 |
Correct |
300 ms |
592 KB |
Output is correct |
56 |
Correct |
838 ms |
1564 KB |
Output is correct |
57 |
Correct |
840 ms |
1376 KB |
Output is correct |
58 |
Correct |
825 ms |
948 KB |
Output is correct |
59 |
Correct |
821 ms |
984 KB |
Output is correct |
60 |
Correct |
866 ms |
1136 KB |
Output is correct |
61 |
Correct |
813 ms |
816 KB |
Output is correct |
62 |
Correct |
842 ms |
592 KB |
Output is correct |
63 |
Correct |
836 ms |
1100 KB |
Output is correct |
64 |
Correct |
851 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Partially correct |
151 ms |
440 KB |
Output is partially correct |
4 |
Partially correct |
403 ms |
344 KB |
Output is partially correct |
5 |
Partially correct |
849 ms |
1036 KB |
Output is partially correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
22 ms |
344 KB |
Output is correct |
8 |
Partially correct |
131 ms |
436 KB |
Output is partially correct |
9 |
Partially correct |
318 ms |
948 KB |
Output is partially correct |
10 |
Partially correct |
821 ms |
1020 KB |
Output is partially correct |
11 |
Partially correct |
815 ms |
1088 KB |
Output is partially correct |
12 |
Partially correct |
835 ms |
984 KB |
Output is partially correct |
13 |
Partially correct |
833 ms |
900 KB |
Output is partially correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
1 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |