#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;
if(ask(g[a][0],g[b][0])){
reverse(g[a].begin(),g[a].end());
for(int x:g[a]){
ans.push_back(x);
}
for(int x:g[b]){
ans.push_back(x);
}
}
else if(ask(g[a].back(),g[b][0])){
for(int x:g[a]){
ans.push_back(x);
}
for(int x:g[b]){
ans.push_back(x);
}
}
else if(ask(g[a][0],g[b].back())){
for(int x:g[b]){
ans.push_back(x);
}
for(int x:g[a]){
ans.push_back(x);
}
}
else{
if(ask(g[a].back(),g[b].back())==0){
assert(0);
}
reverse(g[b].begin(),g[b].end());
for(int x:g[a]){
ans.push_back(x);
}
for(int x:g[b]){
ans.push_back(x);
}
}
return ans;
}
else{
if((int)g[a].size()>(int)g[b].size()){
return g[a];
}
else{
return g[b];
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 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 |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
4 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 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 |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
596 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1128 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
856 KB |
Output is correct |
10 |
Correct |
6 ms |
1112 KB |
Output is correct |
11 |
Correct |
5 ms |
856 KB |
Output is correct |
12 |
Correct |
7 ms |
1816 KB |
Output is correct |
13 |
Correct |
7 ms |
1100 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Runtime error |
1 ms |
852 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
1012 KB |
Output is correct |
6 |
Correct |
14 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
1312 KB |
Output is correct |
11 |
Correct |
7 ms |
1624 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
5 ms |
1100 KB |
Output is correct |
14 |
Correct |
8 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Runtime error |
6 ms |
484 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |