#include <vector>
#include<bits/stdc++.h>
#define f first
#define s second
#define ent '\n'
using namespace std;
typedef long long ll;
const int mx=5e2+12;
vector<int> g[mx];
int was[mx][mx];
bool are_connected(std::vector<int> A, std::vector<int> B);
int ask(int a,int b){
if(!was[a][b]){
was[a][b]=was[b][a]=(int)are_connected({a},{b})+1;
}
return was[a][b]-1;
}
std::vector<int> longest_trip(int n, int d){
vector<int> ans;
if(d==3){
for(int i=0;i<n;i++){
ans.push_back(i);
}
return ans;
}
bool used[n+12]={};
if(d==2){
deque<int> ans;
ans.push_back(0);
for(int i=1;i<n;i++){
if(are_connected({0},{i})){
ans.push_back(i);
used[0]=1;
used[i]=1;
break;
}
}
for(int i=1;i<n;i++){
if(used[i]){
continue;
}
if(are_connected({ans.back()},{i})){
ans.push_back(i);
}
else{
ans.push_front(i);
}
}
vector<int> v;
for(int x:ans){
v.push_back(x);
}
return v;
}
for(int i=0;i<n;i++){
g[i].clear();
for(int j=0;j<n;j++){
was[i][j]=0;
}
}
srand(time(0));
vector<int> t;
for(int i=0;i<n;i++){
g[i].push_back(i);
t.push_back(i);
}
while(t.size()>2){
int a=t[0],b=t[1],c=t[2];
vector<pair<int,int>> d;
d.push_back({a,b});
d.push_back({a,c});
d.push_back({b,c});
random_shuffle(d.begin(),d.end());
int v,u;
if(ask(d[0].f,d[0].s)){
v=d[0].f,u=d[0].s;
}
else if(ask(d[1].f,d[1].s)){
v=d[1].f,u=d[1].s;
}
else{
v=d[2].f,u=d[2].s;
}
reverse(g[v].begin(),g[v].end());
for(int x:g[u]){
g[v].push_back(x);
}
g[u].clear();
int x=g[v][0];
if(x!=v){
for(int to:g[v]){
g[x].push_back(to);
}
g[v].clear();
}
t.clear();
for(int i=0;i<n;i++){
if((int)g[i].size()>0){
t.push_back(i);
}
}
}
int a=t[0],b=t[1];
if(are_connected(g[a],g[b])){
vector<int> ans;
vector<int> v=g[a],u=g[b];
if(ask(v[0],u.back())){
ans=u;
for(int x:v){
ans.push_back(x);
}
return ans;
}
if(ask(v[0],u[0])){
reverse(u.begin(),u.end());
ans=u;
for(int x:v){
ans.push_back(x);
}
return ans;
}
if(ask(v.back(),u[0])){
ans=v;
for(int x:u){
ans.push_back(x);
}
return ans;
}
if(ask(v.back(),u.back())){
ans=v;
reverse(u.begin(),u.end());
for(int x:u){
ans.push_back(x);
}
return ans;
}
int pos=-1,pos1=-1;
for(int l=1,r=v.size();l<=r;){
int mid=l+r>>1;
vector<int> t;
for(int i=0;i<mid;i++){
t.push_back(v[i]);
}
if(are_connected(t,u)){
pos=mid-1;
r=mid-1;
}
else{
l=mid+1;
}
}
for(int l=1,r=u.size();l<=r;){
int mid=l+r>>1;
vector<int> t;
for(int i=0;i<mid;i++){
t.push_back(u[i]);
}
if(are_connected({v[pos]},t)){
pos1=mid-1;
r=mid-1;
}
else{
l=mid+1;
}
}
vector<int> x,y;
for(int i=pos;i<v.size();i++){
x.push_back(v[i]);
}
for(int i=0;i<pos;i++){
x.push_back(v[i]);
}
for(int i=pos1;i<u.size();i++){
y.push_back(u[i]);
}
for(int i=0;i<pos1;i++){
y.push_back(u[i]);
}
reverse(x.begin(),x.end());
for(int t:x){
ans.push_back(t);
}
for(int t:y){
ans.push_back(t);
}
return ans;
}
else{
if((int)g[a].size()>(int)g[b].size()){
return g[a];
}
else{
return g[b];
}
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:145:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
145 | int mid=l+r>>1;
| ~^~
longesttrip.cpp:159:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
159 | int mid=l+r>>1;
| ~^~
longesttrip.cpp:173:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for(int i=pos;i<v.size();i++){
| ~^~~~~~~~~
longesttrip.cpp:179:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
179 | for(int i=pos1;i<u.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
600 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
4 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
596 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
456 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1368 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
1112 KB |
Output is correct |
11 |
Correct |
7 ms |
1368 KB |
Output is correct |
12 |
Correct |
5 ms |
1220 KB |
Output is correct |
13 |
Correct |
6 ms |
856 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
1112 KB |
Output is correct |
20 |
Correct |
6 ms |
1112 KB |
Output is correct |
21 |
Correct |
7 ms |
1640 KB |
Output is correct |
22 |
Correct |
6 ms |
1356 KB |
Output is correct |
23 |
Correct |
7 ms |
1364 KB |
Output is correct |
24 |
Correct |
7 ms |
1628 KB |
Output is correct |
25 |
Correct |
8 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 |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
584 KB |
Output is correct |
31 |
Correct |
6 ms |
584 KB |
Output is correct |
32 |
Correct |
6 ms |
836 KB |
Output is correct |
33 |
Correct |
6 ms |
600 KB |
Output is correct |
34 |
Correct |
6 ms |
856 KB |
Output is correct |
35 |
Correct |
7 ms |
600 KB |
Output is correct |
36 |
Correct |
6 ms |
1240 KB |
Output is correct |
37 |
Correct |
7 ms |
1256 KB |
Output is correct |
38 |
Correct |
8 ms |
1852 KB |
Output is correct |
39 |
Correct |
7 ms |
1116 KB |
Output is correct |
40 |
Correct |
7 ms |
1404 KB |
Output is correct |
41 |
Correct |
8 ms |
1404 KB |
Output is correct |
42 |
Correct |
7 ms |
1112 KB |
Output is correct |
43 |
Correct |
8 ms |
1400 KB |
Output is correct |
44 |
Correct |
8 ms |
1572 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
9 ms |
344 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
844 KB |
Output is correct |
51 |
Correct |
8 ms |
584 KB |
Output is correct |
52 |
Correct |
10 ms |
776 KB |
Output is correct |
53 |
Correct |
8 ms |
860 KB |
Output is correct |
54 |
Correct |
7 ms |
856 KB |
Output is correct |
55 |
Correct |
7 ms |
856 KB |
Output is correct |
56 |
Correct |
6 ms |
1384 KB |
Output is correct |
57 |
Correct |
7 ms |
1660 KB |
Output is correct |
58 |
Correct |
9 ms |
1916 KB |
Output is correct |
59 |
Correct |
12 ms |
1712 KB |
Output is correct |
60 |
Correct |
10 ms |
1700 KB |
Output is correct |
61 |
Correct |
9 ms |
1136 KB |
Output is correct |
62 |
Correct |
8 ms |
1316 KB |
Output is correct |
63 |
Correct |
8 ms |
1808 KB |
Output is correct |
64 |
Correct |
10 ms |
1308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
856 KB |
Output is correct |
5 |
Correct |
5 ms |
856 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 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 |
856 KB |
Output is correct |
11 |
Correct |
6 ms |
1112 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
6 ms |
1096 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
5 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
1112 KB |
Output is correct |
20 |
Correct |
5 ms |
856 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
588 KB |
Output is correct |
27 |
Correct |
6 ms |
584 KB |
Output is correct |
28 |
Correct |
7 ms |
840 KB |
Output is correct |
29 |
Correct |
6 ms |
600 KB |
Output is correct |
30 |
Correct |
6 ms |
856 KB |
Output is correct |
31 |
Correct |
8 ms |
600 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
10 ms |
344 KB |
Output is correct |
37 |
Correct |
7 ms |
840 KB |
Output is correct |
38 |
Correct |
9 ms |
844 KB |
Output is correct |
39 |
Correct |
9 ms |
836 KB |
Output is correct |
40 |
Correct |
6 ms |
600 KB |
Output is correct |
41 |
Correct |
8 ms |
856 KB |
Output is correct |
42 |
Correct |
7 ms |
856 KB |
Output is correct |
43 |
Correct |
7 ms |
1612 KB |
Output is correct |
44 |
Correct |
7 ms |
1356 KB |
Output is correct |
45 |
Correct |
6 ms |
1144 KB |
Output is correct |
46 |
Correct |
6 ms |
1536 KB |
Output is correct |
47 |
Correct |
7 ms |
1112 KB |
Output is correct |
48 |
Correct |
7 ms |
1388 KB |
Output is correct |
49 |
Correct |
8 ms |
1488 KB |
Output is correct |
50 |
Correct |
8 ms |
1148 KB |
Output is correct |
51 |
Correct |
7 ms |
1412 KB |
Output is correct |
52 |
Correct |
7 ms |
1368 KB |
Output is correct |
53 |
Partially correct |
7 ms |
1112 KB |
Output is partially correct |
54 |
Correct |
8 ms |
1392 KB |
Output is correct |
55 |
Correct |
8 ms |
1640 KB |
Output is correct |
56 |
Correct |
7 ms |
1144 KB |
Output is correct |
57 |
Correct |
7 ms |
1144 KB |
Output is correct |
58 |
Correct |
9 ms |
1532 KB |
Output is correct |
59 |
Correct |
8 ms |
1148 KB |
Output is correct |
60 |
Partially correct |
8 ms |
1628 KB |
Output is partially correct |
61 |
Correct |
7 ms |
1112 KB |
Output is correct |
62 |
Correct |
6 ms |
1248 KB |
Output is correct |
63 |
Correct |
7 ms |
1404 KB |
Output is correct |
64 |
Correct |
7 ms |
1384 KB |
Output is correct |
65 |
Partially correct |
8 ms |
1180 KB |
Output is partially correct |
66 |
Partially correct |
11 ms |
1460 KB |
Output is partially correct |
67 |
Partially correct |
10 ms |
1328 KB |
Output is partially correct |
68 |
Partially correct |
9 ms |
1660 KB |
Output is partially correct |
69 |
Partially correct |
8 ms |
1564 KB |
Output is partially correct |
70 |
Partially correct |
10 ms |
1576 KB |
Output is partially correct |
71 |
Correct |
8 ms |
1148 KB |
Output is correct |
72 |
Partially correct |
9 ms |
1684 KB |
Output is partially correct |
73 |
Partially correct |
8 ms |
1628 KB |
Output is partially correct |
74 |
Partially correct |
9 ms |
1576 KB |
Output is partially correct |
75 |
Partially correct |
9 ms |
1368 KB |
Output is partially correct |
76 |
Partially correct |
8 ms |
1136 KB |
Output is partially correct |
77 |
Correct |
10 ms |
1148 KB |
Output is correct |
78 |
Correct |
9 ms |
1920 KB |
Output is correct |
79 |
Partially correct |
8 ms |
1948 KB |
Output is partially correct |
80 |
Partially correct |
9 ms |
1448 KB |
Output is partially correct |
81 |
Partially correct |
9 ms |
1148 KB |
Output is partially correct |
82 |
Partially correct |
8 ms |
1368 KB |
Output is partially correct |