#include <bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define SIZE(x) (int)x.size()
#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define pb push_back
typedef vector<int> vi;
bool are_connected(vi A, vi B);
int n;
const int MAXN = 256;
int c[MAXN][MAXN];
bool query(int x, int y) {
if(c[x][y]==1) return true;
if(c[x][y]==0) return false;
forn(z,n) {
if(z==x||z==y) continue;
if(c[x][z] == 0 && c[y][z] == 0) {
c[x][y]=c[y][x]=1;
return true;
}
}
c[x][y]=c[y][x]=are_connected({x},{y});
return c[x][y];
}
vi longest_trip(int N, int D) {
n=N;
memset(c,-1,sizeof c);
(void)D;
vi P;
forn(i,n) P.pb(i);
srand(time(0));
random_shuffle(ALL(P));
vi a={P[0]}, b;
forsn(i,1,n) {
if(query(a.back(),P[i])) {
a.pb(P[i]);
} else if(!b.empty() && query(a.back(),b.back())) {
reverse(ALL(b));
forn(j,SIZE(b)) a.pb(b[j]);
b={P[i]};
} else if(b.empty() || query(b.back(),P[i])) {
b.pb(P[i]);
}
}
if(SIZE(b)>SIZE(a)) swap(a,b);
if(b.empty()||!are_connected(a,b)) return a;
vi qa={a.front()}, qb={b.front()};
if(a.back()!=qa.back()) qa.pb(a.back());
if(b.back()!=qb.back()) qb.pb(b.back());
if(are_connected(qa, qb)) {
if(query(a.back(),b.front())) {
forn(i,SIZE(b)) a.pb(b[i]);
return a;
} else if(query(b.back(),a.front())) {
forn(i,SIZE(a)) b.pb(a[i]);
return b;
} else if(query(a.front(),b.front())) {
reverse(ALL(a));
forn(i,SIZE(b)) a.pb(b[i]);
return a;
} else {
reverse(ALL(b));
forn(i,SIZE(b)) a.pb(b[i]);
return a;
}
}
int l=0, r=SIZE(a);
while(l+1<r) {
int m=(l+r)/2;
if(are_connected(b,vi(a.begin()+m,a.begin()+r))) l=m;
else r=m;
}
int i=l;
l=0, r=SIZE(b);
while(l+1<r) {
int m=(l+r)/2;
if(are_connected({a[i]},vi(b.begin()+m,b.begin()+r))) l=m;
else r=m;
}
int j=l;
vi ans;
forsn(p,j+1,SIZE(b)) ans.pb(b[p]);
forn(p,j+1) ans.pb(b[p]);
forsn(p,i,SIZE(a)) ans.pb(a[p]);
forn(p,i) ans.pb(a[p]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
600 KB |
Output is correct |
2 |
Correct |
5 ms |
852 KB |
Output is correct |
3 |
Correct |
6 ms |
600 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
600 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
12 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
600 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
6 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
10 ms |
600 KB |
Output is correct |
7 |
Correct |
7 ms |
600 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
856 KB |
Output is correct |
10 |
Correct |
6 ms |
668 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
6 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
600 KB |
Output is correct |
15 |
Correct |
8 ms |
600 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
7 ms |
600 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 |
600 KB |
Output is correct |
21 |
Correct |
5 ms |
600 KB |
Output is correct |
22 |
Correct |
6 ms |
856 KB |
Output is correct |
23 |
Correct |
7 ms |
852 KB |
Output is correct |
24 |
Correct |
8 ms |
600 KB |
Output is correct |
25 |
Correct |
8 ms |
600 KB |
Output is correct |
26 |
Correct |
9 ms |
600 KB |
Output is correct |
27 |
Correct |
6 ms |
600 KB |
Output is correct |
28 |
Correct |
7 ms |
600 KB |
Output is correct |
29 |
Correct |
6 ms |
600 KB |
Output is correct |
30 |
Correct |
5 ms |
600 KB |
Output is correct |
31 |
Correct |
6 ms |
600 KB |
Output is correct |
32 |
Correct |
6 ms |
600 KB |
Output is correct |
33 |
Correct |
6 ms |
856 KB |
Output is correct |
34 |
Correct |
5 ms |
856 KB |
Output is correct |
35 |
Correct |
5 ms |
600 KB |
Output is correct |
36 |
Correct |
7 ms |
600 KB |
Output is correct |
37 |
Correct |
5 ms |
600 KB |
Output is correct |
38 |
Correct |
6 ms |
600 KB |
Output is correct |
39 |
Correct |
5 ms |
600 KB |
Output is correct |
40 |
Correct |
6 ms |
856 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
6 ms |
600 KB |
Output is correct |
43 |
Correct |
7 ms |
600 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
10 ms |
600 KB |
Output is correct |
46 |
Correct |
10 ms |
600 KB |
Output is correct |
47 |
Correct |
9 ms |
600 KB |
Output is correct |
48 |
Correct |
8 ms |
600 KB |
Output is correct |
49 |
Correct |
8 ms |
600 KB |
Output is correct |
50 |
Correct |
5 ms |
600 KB |
Output is correct |
51 |
Correct |
6 ms |
600 KB |
Output is correct |
52 |
Correct |
7 ms |
600 KB |
Output is correct |
53 |
Correct |
5 ms |
600 KB |
Output is correct |
54 |
Correct |
5 ms |
600 KB |
Output is correct |
55 |
Correct |
6 ms |
864 KB |
Output is correct |
56 |
Correct |
5 ms |
600 KB |
Output is correct |
57 |
Correct |
6 ms |
856 KB |
Output is correct |
58 |
Correct |
7 ms |
856 KB |
Output is correct |
59 |
Correct |
8 ms |
856 KB |
Output is correct |
60 |
Correct |
9 ms |
856 KB |
Output is correct |
61 |
Correct |
6 ms |
600 KB |
Output is correct |
62 |
Correct |
6 ms |
856 KB |
Output is correct |
63 |
Correct |
10 ms |
700 KB |
Output is correct |
64 |
Correct |
8 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
600 KB |
Output is correct |
2 |
Correct |
6 ms |
612 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
13 ms |
852 KB |
Output is correct |
7 |
Correct |
8 ms |
600 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
852 KB |
Output is correct |
12 |
Correct |
6 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
636 KB |
Output is correct |
14 |
Correct |
9 ms |
852 KB |
Output is correct |
15 |
Correct |
10 ms |
600 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
600 KB |
Output is correct |
18 |
Correct |
7 ms |
852 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
7 ms |
600 KB |
Output is correct |
21 |
Correct |
9 ms |
600 KB |
Output is correct |
22 |
Correct |
11 ms |
600 KB |
Output is correct |
23 |
Correct |
6 ms |
600 KB |
Output is correct |
24 |
Correct |
6 ms |
600 KB |
Output is correct |
25 |
Correct |
6 ms |
600 KB |
Output is correct |
26 |
Correct |
5 ms |
600 KB |
Output is correct |
27 |
Correct |
6 ms |
688 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
6 ms |
600 KB |
Output is correct |
30 |
Correct |
5 ms |
600 KB |
Output is correct |
31 |
Correct |
5 ms |
600 KB |
Output is correct |
32 |
Correct |
9 ms |
600 KB |
Output is correct |
33 |
Correct |
13 ms |
600 KB |
Output is correct |
34 |
Correct |
11 ms |
600 KB |
Output is correct |
35 |
Correct |
10 ms |
600 KB |
Output is correct |
36 |
Correct |
8 ms |
600 KB |
Output is correct |
37 |
Correct |
5 ms |
600 KB |
Output is correct |
38 |
Correct |
6 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
600 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
5 ms |
600 KB |
Output is correct |
42 |
Correct |
5 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
856 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
7 ms |
600 KB |
Output is correct |
46 |
Correct |
7 ms |
600 KB |
Output is correct |
47 |
Correct |
5 ms |
600 KB |
Output is correct |
48 |
Correct |
5 ms |
600 KB |
Output is correct |
49 |
Correct |
7 ms |
600 KB |
Output is correct |
50 |
Correct |
6 ms |
600 KB |
Output is correct |
51 |
Correct |
8 ms |
600 KB |
Output is correct |
52 |
Correct |
6 ms |
600 KB |
Output is correct |
53 |
Correct |
6 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
600 KB |
Output is correct |
55 |
Correct |
7 ms |
600 KB |
Output is correct |
56 |
Correct |
5 ms |
600 KB |
Output is correct |
57 |
Correct |
7 ms |
600 KB |
Output is correct |
58 |
Correct |
6 ms |
600 KB |
Output is correct |
59 |
Correct |
6 ms |
600 KB |
Output is correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
8 ms |
600 KB |
Output is correct |
62 |
Correct |
6 ms |
600 KB |
Output is correct |
63 |
Correct |
5 ms |
700 KB |
Output is correct |
64 |
Correct |
6 ms |
700 KB |
Output is correct |
65 |
Correct |
7 ms |
856 KB |
Output is correct |
66 |
Correct |
8 ms |
700 KB |
Output is correct |
67 |
Correct |
8 ms |
856 KB |
Output is correct |
68 |
Correct |
6 ms |
600 KB |
Output is correct |
69 |
Correct |
6 ms |
864 KB |
Output is correct |
70 |
Correct |
7 ms |
696 KB |
Output is correct |
71 |
Correct |
6 ms |
856 KB |
Output is correct |
72 |
Correct |
6 ms |
700 KB |
Output is correct |
73 |
Correct |
6 ms |
856 KB |
Output is correct |
74 |
Correct |
6 ms |
856 KB |
Output is correct |
75 |
Correct |
6 ms |
600 KB |
Output is correct |
76 |
Correct |
7 ms |
600 KB |
Output is correct |
77 |
Correct |
6 ms |
700 KB |
Output is correct |
78 |
Correct |
6 ms |
856 KB |
Output is correct |
79 |
Correct |
7 ms |
856 KB |
Output is correct |
80 |
Correct |
7 ms |
864 KB |
Output is correct |
81 |
Correct |
7 ms |
600 KB |
Output is correct |
82 |
Correct |
7 ms |
600 KB |
Output is correct |