#pragma GCC optimize("Ofast","unroll-all-loops")
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> longest_trip(int N,int D){
vector<vector<int>> adj(N);
vector areConnected(N,vector<bool>(N));
for(int i=0;i<N;i++) {
for(int j=i+1;j<N;j++) {
if(are_connected({i},{j})) {
adj[i].emplace_back(j);
adj[j].emplace_back(i);
areConnected[i][j]=areConnected[j][i]=true;
}
}
}
{
// Case 1 if two components
int isTwo = -1;
{
vector<bool> visited(N);
int curr = 0;
function<void(int)> dfs = [&](int x) {
if(visited[x])return;
visited[x]=true;
curr++;
for(int&i:adj[x])dfs(i);
};
dfs(0);
if(curr!=N) {
if(curr>=N-curr)isTwo=curr;
else {
for(int i=0;i<N;i++) {
if(!visited[i]){
isTwo=i;break;
}
}
}
}
}
if(isTwo!=-1) {
vector<bool> visited(N);
vector<int> ans;
function<void(int)> dfs = [&](int x) {
visited[x]=true;
ans.emplace_back(x);
for(int&i:adj[x])if(!visited[i]) {
dfs(i);
return;
}
};
dfs(isTwo);
return ans;
}
}
vector<vector<int>> path(3);
vector<bool> visited(N);
function<int(int,int)> dfs = [&](int x,int def) {
visited[x]=true;
int state = def;
int children = 0;
for(int&i:adj[x])if(!visited[i]) {
children++;
if(children==2) {
assert(dfs(i,2)==2);
state=0;
break;
}
auto ans = dfs(i,def);
if(ans==0) {
state = 0;
break;
}
}
path[state].emplace_back(x);
return state;
};
auto curr = dfs(0,1);
if(curr==1) {
return path[1];
}
assert(curr==0);
if(path[0].size()==1) {
vector<int> ans = path[1];
ans.emplace_back(path[0][0]);
reverse(path[2].begin(),path[2].end());
for(int&i:path[2])ans.emplace_back(i);
return ans;
}
if(!areConnected[path[0][0]][path[1][0]])swap(path[1],path[2]);
path[1].emplace_back(path[0][0]);
path[0].erase(path[0].begin());
reverse(path[0].begin(),path[0].end());
reverse(path[2].begin(),path[2].end());
auto ans = path[0];
for(int&i:path[1])ans.emplace_back(i);
for(int&i:path[2])ans.emplace_back(i);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
212 ms |
1028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
19 ms |
344 KB |
Output is correct |
3 |
Correct |
154 ms |
592 KB |
Output is correct |
4 |
Correct |
436 ms |
612 KB |
Output is correct |
5 |
Correct |
880 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Correct |
141 ms |
444 KB |
Output is correct |
4 |
Correct |
410 ms |
736 KB |
Output is correct |
5 |
Correct |
843 ms |
1360 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
26 ms |
432 KB |
Output is correct |
8 |
Correct |
142 ms |
440 KB |
Output is correct |
9 |
Correct |
277 ms |
732 KB |
Output is correct |
10 |
Correct |
839 ms |
944 KB |
Output is correct |
11 |
Correct |
864 ms |
764 KB |
Output is correct |
12 |
Correct |
933 ms |
1312 KB |
Output is correct |
13 |
Correct |
890 ms |
996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
24 ms |
344 KB |
Output is correct |
3 |
Correct |
136 ms |
596 KB |
Output is correct |
4 |
Correct |
429 ms |
344 KB |
Output is correct |
5 |
Correct |
785 ms |
1148 KB |
Output is correct |
6 |
Correct |
7 ms |
340 KB |
Output is correct |
7 |
Correct |
24 ms |
344 KB |
Output is correct |
8 |
Correct |
129 ms |
856 KB |
Output is correct |
9 |
Correct |
268 ms |
756 KB |
Output is correct |
10 |
Correct |
773 ms |
972 KB |
Output is correct |
11 |
Correct |
799 ms |
1008 KB |
Output is correct |
12 |
Correct |
856 ms |
1360 KB |
Output is correct |
13 |
Correct |
819 ms |
1316 KB |
Output is correct |
14 |
Correct |
5 ms |
344 KB |
Output is correct |
15 |
Incorrect |
13 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Partially correct |
145 ms |
440 KB |
Output is partially correct |
4 |
Partially correct |
402 ms |
600 KB |
Output is partially correct |
5 |
Partially correct |
841 ms |
880 KB |
Output is partially correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
27 ms |
344 KB |
Output is correct |
8 |
Partially correct |
140 ms |
460 KB |
Output is partially correct |
9 |
Partially correct |
335 ms |
744 KB |
Output is partially correct |
10 |
Partially correct |
895 ms |
968 KB |
Output is partially correct |
11 |
Partially correct |
900 ms |
1092 KB |
Output is partially correct |
12 |
Partially correct |
819 ms |
1016 KB |
Output is partially correct |
13 |
Partially correct |
882 ms |
884 KB |
Output is partially correct |
14 |
Incorrect |
1 ms |
344 KB |
Incorrect |
15 |
Halted |
0 ms |
0 KB |
- |