#include "longesttrip.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
int g[257][257] = {{0}};
bool isg(int a, int b){
if(g[a][b]) {
return g[a][b] == 1;
}
if(are_connected({a},{b})){
g[a][b]=g[b][a]=1;
return true;
}
g[a][b]=g[b][a]=-1;
return false;
}
bool arc(const vector<int>& a, const vector<int>& b){
if(a.empty()||b.empty()) return false;
bool at0=false;
for(int i : a) for(int j : b) {
if(g[i][j]==1) return true;
else if(!g[i][j]) at0=true;
}
if(!at0) return false;
return are_connected(a, b);
}
pair<vector<int>,vector<int>> fl(const vector<int>& V){
if(V.size()<=1) return {V, {}};
int i, j;
vector<int> a;a.push_back(V[0]);
for(i = 1;i < V.size();i++){
if(isg(a.back(), V[i])){
a.push_back(V[i]);
} else {
i = V[i];
j = a.back();
break;
}
}
if(a.size() == V.size()) return {a, {}};
vector<int> b, c;
a.clear();
for(int k : V){
if(k==i||k==j) continue;
if(!isg(k,j)) a.push_back(k);
else if(!isg(k,i)) c.push_back(k);
else {
b.push_back(k);
}
}
if(b.empty()){
if(!arc(a,c)) {
a.push_back(i);
c.push_back(j);
return {a,c};
}
int l=0,r=c.size()-1, ll=0,rr=a.size()-1;
while(l<r||ll<rr){
if(r-l > rr-ll){
int m = r+l>>1;
if(arc(vector<int>{c.begin()+l,c.begin()+m+1}, vector<int>{a.begin()+ll,a.begin()+rr+1})){
r=m;
} else l = m+1;
} else {
int m = ll+rr>>1;
if(arc(vector<int>{c.begin()+l,c.begin()+r+1}, vector<int>{a.begin()+ll,a.begin()+m+1})){
rr=m;
} else ll = m+1;
}
}
a.push_back(i);swap(a[ll], a.back());
c.push_back(j);swap(c[l], c[0]);
for(int k : c) a.push_back(k);
return {a, {}};
}
auto L = fl(b);
if(L.second.empty()){
a.push_back(i);
for(int k : L.first) a.push_back(k);
a.push_back(j);
for(int k : c) a.push_back(k);
return {a, {}};
} else {
if(a.empty()){
L.first.push_back(i);
for(int k : L.second) L.first.push_back(k);
L.first.push_back(j);
for(int k : c) L.first.push_back(k);
return {L.first, {}};
}
if(isg(a[0], L.first[0])){
a.push_back(i);
swap(a.back(), a[0]);
for(int k : L.first) a.push_back(k);
for(int k : a) L.second.push_back(k);
L.second.push_back(j);
for(int k : c) L.second.push_back(k);
return {L.second, {}};
} else {
a.push_back(i);
swap(a.back(), a[0]);
for(int k : L.second) a.push_back(k);
for(int k : a) L.first.push_back(k);
L.first.push_back(j);
for(int k : c) L.first.push_back(k);
return {L.first, {}};
}
}
}
std::vector<int> longest_trip(int N, int D)
{
vector<int> V;
for(int i = 0;i < N;i++) {
V.push_back(i);
for(int j = 0;j < N;j++) g[i][j] = 0;
}
auto L = fl(V);
if(L.first.size()>=L.second.size()) return L.first;
return L.second;
}
Compilation message
longesttrip.cpp: In function 'std::pair<std::vector<int>, std::vector<int> > fl(const std::vector<int>&)':
longesttrip.cpp:32:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(i = 1;i < V.size();i++){
| ~~^~~~~~~~~~
longesttrip.cpp:61:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
61 | int m = r+l>>1;
| ~^~
longesttrip.cpp:66:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int m = ll+rr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
7 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 |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
13 ms |
344 KB |
Output is correct |
9 |
Correct |
13 ms |
344 KB |
Output is correct |
10 |
Correct |
10 ms |
600 KB |
Output is correct |
11 |
Correct |
11 ms |
856 KB |
Output is correct |
12 |
Correct |
19 ms |
600 KB |
Output is correct |
13 |
Correct |
14 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
14 ms |
344 KB |
Output is correct |
8 |
Correct |
13 ms |
344 KB |
Output is correct |
9 |
Correct |
13 ms |
344 KB |
Output is correct |
10 |
Correct |
10 ms |
600 KB |
Output is correct |
11 |
Correct |
12 ms |
600 KB |
Output is correct |
12 |
Correct |
19 ms |
704 KB |
Output is correct |
13 |
Correct |
26 ms |
708 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
19 ms |
344 KB |
Output is correct |
18 |
Correct |
16 ms |
344 KB |
Output is correct |
19 |
Correct |
22 ms |
344 KB |
Output is correct |
20 |
Correct |
14 ms |
344 KB |
Output is correct |
21 |
Correct |
51 ms |
704 KB |
Output is correct |
22 |
Correct |
37 ms |
704 KB |
Output is correct |
23 |
Correct |
30 ms |
856 KB |
Output is correct |
24 |
Correct |
34 ms |
860 KB |
Output is correct |
25 |
Correct |
7 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
15 ms |
344 KB |
Output is correct |
29 |
Correct |
11 ms |
344 KB |
Output is correct |
30 |
Correct |
11 ms |
340 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
344 KB |
Output is correct |
36 |
Correct |
12 ms |
600 KB |
Output is correct |
37 |
Correct |
9 ms |
600 KB |
Output is correct |
38 |
Correct |
11 ms |
600 KB |
Output is correct |
39 |
Correct |
7 ms |
600 KB |
Output is correct |
40 |
Correct |
7 ms |
600 KB |
Output is correct |
41 |
Correct |
8 ms |
600 KB |
Output is correct |
42 |
Correct |
11 ms |
600 KB |
Output is correct |
43 |
Correct |
8 ms |
600 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
12 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 |
12 ms |
344 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
6 ms |
344 KB |
Output is correct |
53 |
Correct |
11 ms |
344 KB |
Output is correct |
54 |
Correct |
15 ms |
344 KB |
Output is correct |
55 |
Correct |
12 ms |
344 KB |
Output is correct |
56 |
Correct |
8 ms |
600 KB |
Output is correct |
57 |
Correct |
11 ms |
600 KB |
Output is correct |
58 |
Correct |
5 ms |
600 KB |
Output is correct |
59 |
Correct |
8 ms |
600 KB |
Output is correct |
60 |
Correct |
8 ms |
708 KB |
Output is correct |
61 |
Correct |
8 ms |
600 KB |
Output is correct |
62 |
Correct |
7 ms |
600 KB |
Output is correct |
63 |
Correct |
6 ms |
600 KB |
Output is correct |
64 |
Correct |
7 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
13 ms |
344 KB |
Output is correct |
8 |
Correct |
15 ms |
344 KB |
Output is correct |
9 |
Partially correct |
13 ms |
344 KB |
Output is partially correct |
10 |
Partially correct |
12 ms |
704 KB |
Output is partially correct |
11 |
Partially correct |
13 ms |
600 KB |
Output is partially correct |
12 |
Partially correct |
15 ms |
856 KB |
Output is partially correct |
13 |
Partially correct |
23 ms |
700 KB |
Output is partially correct |
14 |
Correct |
4 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 |
13 ms |
344 KB |
Output is correct |
18 |
Correct |
14 ms |
596 KB |
Output is correct |
19 |
Partially correct |
20 ms |
344 KB |
Output is partially correct |
20 |
Partially correct |
20 ms |
544 KB |
Output is partially correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
12 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
14 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 |
10 ms |
344 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
9 ms |
344 KB |
Output is correct |
37 |
Correct |
10 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
8 ms |
344 KB |
Output is correct |
40 |
Correct |
13 ms |
344 KB |
Output is correct |
41 |
Correct |
11 ms |
344 KB |
Output is correct |
42 |
Correct |
9 ms |
344 KB |
Output is correct |
43 |
Partially correct |
36 ms |
704 KB |
Output is partially correct |
44 |
Partially correct |
22 ms |
852 KB |
Output is partially correct |
45 |
Partially correct |
28 ms |
684 KB |
Output is partially correct |
46 |
Partially correct |
38 ms |
856 KB |
Output is partially correct |
47 |
Partially correct |
12 ms |
600 KB |
Output is partially correct |
48 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
49 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
50 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
51 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
52 |
Correct |
7 ms |
600 KB |
Output is correct |
53 |
Correct |
5 ms |
600 KB |
Output is correct |
54 |
Correct |
7 ms |
600 KB |
Output is correct |
55 |
Correct |
7 ms |
600 KB |
Output is correct |
56 |
Partially correct |
11 ms |
600 KB |
Output is partially correct |
57 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
58 |
Partially correct |
6 ms |
600 KB |
Output is partially correct |
59 |
Partially correct |
6 ms |
600 KB |
Output is partially correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
7 ms |
600 KB |
Output is correct |
62 |
Partially correct |
11 ms |
600 KB |
Output is partially correct |
63 |
Partially correct |
9 ms |
600 KB |
Output is partially correct |
64 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
65 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
66 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
67 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
68 |
Correct |
8 ms |
856 KB |
Output is correct |
69 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
70 |
Partially correct |
6 ms |
600 KB |
Output is partially correct |
71 |
Partially correct |
9 ms |
704 KB |
Output is partially correct |
72 |
Partially correct |
6 ms |
600 KB |
Output is partially correct |
73 |
Partially correct |
5 ms |
600 KB |
Output is partially correct |
74 |
Partially correct |
8 ms |
600 KB |
Output is partially correct |
75 |
Correct |
7 ms |
600 KB |
Output is correct |
76 |
Correct |
7 ms |
600 KB |
Output is correct |
77 |
Partially correct |
8 ms |
704 KB |
Output is partially correct |
78 |
Partially correct |
10 ms |
600 KB |
Output is partially correct |
79 |
Partially correct |
11 ms |
600 KB |
Output is partially correct |
80 |
Partially correct |
7 ms |
600 KB |
Output is partially correct |
81 |
Correct |
10 ms |
600 KB |
Output is correct |
82 |
Correct |
7 ms |
600 KB |
Output is correct |