#include<bits/stdc++.h>
#include "longesttrip.h"
#define MX 100100
using namespace std;
int n,now,x,y,z,xx,yy,gg,zz,to[MX];
vector<int>A,B,ans,ans1,ans2,a[MX];
// queue<int>d;
// bool are_connected(std::vector<int> A, std::vector<int> B)
bool check(int x,int y)
{
A.clear();B.clear();
A.push_back(x);
B.push_back(y);
return are_connected(A,B);
}
bool checkk(int x,vector<int> G)
{
A.clear();
A.push_back(x);
return are_connected(A,G);
}
bool checkkk(int x,vector<int> G)
{
A.clear();
for(int i=0;i<=x;++i)
A.push_back(ans1[i]);
return are_connected(A,G);
}
bool checkkkk(int x,int y)
{
A.clear();
A.push_back(x);
B.clear();
for(int i=0;i<=y;++i)
B.push_back(ans2[i]);
return are_connected(A,B);
}
void dfs(int x,int fa)
{
ans1.push_back(x);
for(int i=0;i<a[x].size();++i)
if(a[x][i]!=fa)
dfs(a[x][i],x);
return;
}
void dfss(int x,int fa)
{
ans2.push_back(x);
for(int i=0;i<a[x].size();++i)
if(a[x][i]!=fa)
dfss(a[x][i],x);
return;
}
mt19937 gy(2018729);
int rd(int l,int r)
{
return gy()%(r-l+1)+l;
}
std::vector<int> longest_trip(int N, int D)
{
n=N;
for(int i=0;i<n;++i)
a[i].clear();
x=0;
to[x]=x;
y=1;
to[y]=y;
gg=0;
for(int i=2;i<n;++i){
z=i;
to[z]=z;
if(check(x,z)){
a[x].push_back(z);
a[z].push_back(x);
xx=to[x];
zz=to[z];
to[xx]=zz;
to[zz]=xx;
x=z;
gg=0;
}
else if(!gg&&check(x,y)){
a[x].push_back(y);
a[y].push_back(x);
xx=to[x];
yy=to[y];
to[xx]=yy;
to[yy]=xx;
x=xx;y=z;
gg=0;
}
else {
a[y].push_back(z);
a[z].push_back(y);
yy=to[y];
zz=to[z];
to[yy]=zz;
to[zz]=yy;
y=z;
gg=1;
}
}
ans1.clear();
ans2.clear();
ans.clear();
dfs(x,x);
dfss(y,y);
if(!are_connected(ans1,ans2)){
if(ans1.size()>ans2.size())return ans1;
else return ans2;
}
xx=to[x];
yy=to[y];
A.clear();
B.clear();
A.push_back(x);
if(x!=xx)A.push_back(xx);
B.push_back(y);
if(y!=yy)B.push_back(yy);
if(are_connected(A,B)){
if(check(x,y)){
for(int i=ans1.size()-1;i>=0;--i)
ans.push_back(ans1[i]);
for(int i=0;i<ans2.size();++i)
ans.push_back(ans2[i]);
}
else if(check(x,yy)){
for(int i=ans1.size()-1;i>=0;--i)
ans.push_back(ans1[i]);
for(int i=ans2.size()-1;i>=0;--i)
ans.push_back(ans2[i]);
}
else if(check(xx,y)){
for(int i=0;i<ans1.size();++i)
ans.push_back(ans1[i]);
for(int i=0;i<ans2.size();++i)
ans.push_back(ans2[i]);
}
else if(check(xx,yy)){
for(int i=0;i<ans1.size();++i)
ans.push_back(ans1[i]);
for(int i=ans2.size()-1;i>=0;--i)
ans.push_back(ans2[i]);
}
}
else{
int l=0,r=ans1.size()-1;
while(l<r){
int mid=l+r>>1;
if(checkkk(mid,ans2))r=mid;
else l=mid+1;
}
int sav=l;
l=0;r=ans2.size()-1;
while(l<r){
int mid=l+r>>1;
if(checkkkk(ans1[sav],mid))r=mid;
else l=mid+1;
}
int i=sav,j=l;
for(int k=i+1;k<ans1.size();++k)
ans.push_back(ans1[k]);
for(int k=0;k<=i;++k)
ans.push_back(ans1[k]);
for(int k=j;k<ans2.size();++k)
ans.push_back(ans2[k]);
for(int k=0;k<j;++k)
ans.push_back(ans2[k]);
return ans;
}
return ans;
}
Compilation message
longesttrip.cpp: In function 'void dfs(int, int)':
longesttrip.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<a[x].size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp: In function 'void dfss(int, int)':
longesttrip.cpp:50:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=0;i<a[x].size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:126:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for(int i=0;i<ans2.size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp:136:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int i=0;i<ans1.size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp:138:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i=0;i<ans2.size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp:142:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for(int i=0;i<ans1.size();++i)
| ~^~~~~~~~~~~~
longesttrip.cpp:151:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
151 | int mid=l+r>>1;
| ~^~
longesttrip.cpp:158:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
158 | int mid=l+r>>1;
| ~^~
longesttrip.cpp:163:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | for(int k=i+1;k<ans1.size();++k)
| ~^~~~~~~~~~~~
longesttrip.cpp:167:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for(int k=j;k<ans2.size();++k)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
2640 KB |
Output is correct |
2 |
Correct |
10 ms |
2640 KB |
Output is correct |
3 |
Correct |
9 ms |
2640 KB |
Output is correct |
4 |
Correct |
11 ms |
2640 KB |
Output is correct |
5 |
Correct |
11 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2640 KB |
Output is correct |
2 |
Correct |
10 ms |
2640 KB |
Output is correct |
3 |
Correct |
11 ms |
2640 KB |
Output is correct |
4 |
Correct |
11 ms |
2664 KB |
Output is correct |
5 |
Correct |
12 ms |
2640 KB |
Output is correct |
6 |
Correct |
21 ms |
2640 KB |
Output is correct |
7 |
Correct |
13 ms |
2640 KB |
Output is correct |
8 |
Correct |
10 ms |
2640 KB |
Output is correct |
9 |
Correct |
10 ms |
2640 KB |
Output is correct |
10 |
Correct |
15 ms |
2640 KB |
Output is correct |
11 |
Correct |
10 ms |
2640 KB |
Output is correct |
12 |
Correct |
9 ms |
2640 KB |
Output is correct |
13 |
Correct |
8 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
2640 KB |
Output is correct |
2 |
Correct |
8 ms |
2640 KB |
Output is correct |
3 |
Correct |
8 ms |
2640 KB |
Output is correct |
4 |
Correct |
11 ms |
2592 KB |
Output is correct |
5 |
Correct |
10 ms |
2640 KB |
Output is correct |
6 |
Correct |
20 ms |
2640 KB |
Output is correct |
7 |
Correct |
13 ms |
2640 KB |
Output is correct |
8 |
Correct |
11 ms |
2640 KB |
Output is correct |
9 |
Correct |
9 ms |
2640 KB |
Output is correct |
10 |
Correct |
11 ms |
2768 KB |
Output is correct |
11 |
Correct |
7 ms |
2640 KB |
Output is correct |
12 |
Correct |
8 ms |
2640 KB |
Output is correct |
13 |
Correct |
11 ms |
2640 KB |
Output is correct |
14 |
Correct |
7 ms |
2640 KB |
Output is correct |
15 |
Correct |
11 ms |
2640 KB |
Output is correct |
16 |
Correct |
16 ms |
2640 KB |
Output is correct |
17 |
Correct |
12 ms |
2640 KB |
Output is correct |
18 |
Correct |
13 ms |
2640 KB |
Output is correct |
19 |
Correct |
13 ms |
2672 KB |
Output is correct |
20 |
Correct |
10 ms |
2668 KB |
Output is correct |
21 |
Correct |
11 ms |
2784 KB |
Output is correct |
22 |
Correct |
11 ms |
2640 KB |
Output is correct |
23 |
Correct |
8 ms |
2640 KB |
Output is correct |
24 |
Correct |
10 ms |
2640 KB |
Output is correct |
25 |
Correct |
10 ms |
2640 KB |
Output is correct |
26 |
Correct |
8 ms |
2640 KB |
Output is correct |
27 |
Correct |
11 ms |
2640 KB |
Output is correct |
28 |
Correct |
11 ms |
2640 KB |
Output is correct |
29 |
Correct |
11 ms |
2640 KB |
Output is correct |
30 |
Correct |
11 ms |
2640 KB |
Output is correct |
31 |
Correct |
11 ms |
2640 KB |
Output is correct |
32 |
Correct |
9 ms |
2640 KB |
Output is correct |
33 |
Correct |
8 ms |
2640 KB |
Output is correct |
34 |
Correct |
8 ms |
2640 KB |
Output is correct |
35 |
Correct |
10 ms |
2640 KB |
Output is correct |
36 |
Correct |
9 ms |
2640 KB |
Output is correct |
37 |
Correct |
7 ms |
2640 KB |
Output is correct |
38 |
Correct |
10 ms |
2660 KB |
Output is correct |
39 |
Correct |
10 ms |
2640 KB |
Output is correct |
40 |
Correct |
9 ms |
2640 KB |
Output is correct |
41 |
Correct |
12 ms |
2640 KB |
Output is correct |
42 |
Correct |
8 ms |
2764 KB |
Output is correct |
43 |
Correct |
8 ms |
2640 KB |
Output is correct |
44 |
Correct |
12 ms |
2640 KB |
Output is correct |
45 |
Correct |
16 ms |
2640 KB |
Output is correct |
46 |
Correct |
10 ms |
2764 KB |
Output is correct |
47 |
Correct |
14 ms |
2640 KB |
Output is correct |
48 |
Correct |
9 ms |
2640 KB |
Output is correct |
49 |
Correct |
13 ms |
2640 KB |
Output is correct |
50 |
Correct |
8 ms |
2672 KB |
Output is correct |
51 |
Correct |
10 ms |
2640 KB |
Output is correct |
52 |
Correct |
13 ms |
2640 KB |
Output is correct |
53 |
Correct |
7 ms |
2672 KB |
Output is correct |
54 |
Correct |
11 ms |
2640 KB |
Output is correct |
55 |
Correct |
8 ms |
2640 KB |
Output is correct |
56 |
Correct |
9 ms |
2640 KB |
Output is correct |
57 |
Correct |
6 ms |
2824 KB |
Output is correct |
58 |
Correct |
11 ms |
2760 KB |
Output is correct |
59 |
Correct |
10 ms |
2640 KB |
Output is correct |
60 |
Correct |
10 ms |
2640 KB |
Output is correct |
61 |
Correct |
14 ms |
2640 KB |
Output is correct |
62 |
Correct |
11 ms |
2684 KB |
Output is correct |
63 |
Correct |
14 ms |
2640 KB |
Output is correct |
64 |
Correct |
8 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2640 KB |
Output is correct |
2 |
Correct |
7 ms |
2640 KB |
Output is correct |
3 |
Correct |
7 ms |
2640 KB |
Output is correct |
4 |
Correct |
8 ms |
2640 KB |
Output is correct |
5 |
Correct |
8 ms |
2640 KB |
Output is correct |
6 |
Correct |
19 ms |
2640 KB |
Output is correct |
7 |
Correct |
12 ms |
2640 KB |
Output is correct |
8 |
Correct |
6 ms |
2640 KB |
Output is correct |
9 |
Correct |
12 ms |
2640 KB |
Output is correct |
10 |
Correct |
7 ms |
2640 KB |
Output is correct |
11 |
Correct |
7 ms |
2640 KB |
Output is correct |
12 |
Correct |
8 ms |
2660 KB |
Output is correct |
13 |
Correct |
5 ms |
2640 KB |
Output is correct |
14 |
Correct |
8 ms |
2640 KB |
Output is correct |
15 |
Correct |
13 ms |
2640 KB |
Output is correct |
16 |
Correct |
10 ms |
2640 KB |
Output is correct |
17 |
Correct |
12 ms |
2544 KB |
Output is correct |
18 |
Correct |
7 ms |
2640 KB |
Output is correct |
19 |
Correct |
11 ms |
2640 KB |
Output is correct |
20 |
Correct |
9 ms |
2640 KB |
Output is correct |
21 |
Correct |
11 ms |
2640 KB |
Output is correct |
22 |
Correct |
9 ms |
2640 KB |
Output is correct |
23 |
Correct |
8 ms |
2640 KB |
Output is correct |
24 |
Correct |
7 ms |
2640 KB |
Output is correct |
25 |
Correct |
12 ms |
2640 KB |
Output is correct |
26 |
Correct |
10 ms |
2688 KB |
Output is correct |
27 |
Correct |
10 ms |
2640 KB |
Output is correct |
28 |
Correct |
14 ms |
2676 KB |
Output is correct |
29 |
Correct |
10 ms |
2640 KB |
Output is correct |
30 |
Correct |
10 ms |
2660 KB |
Output is correct |
31 |
Correct |
9 ms |
2640 KB |
Output is correct |
32 |
Correct |
19 ms |
2640 KB |
Output is correct |
33 |
Correct |
12 ms |
2640 KB |
Output is correct |
34 |
Correct |
7 ms |
2640 KB |
Output is correct |
35 |
Correct |
17 ms |
2640 KB |
Output is correct |
36 |
Correct |
11 ms |
2640 KB |
Output is correct |
37 |
Correct |
11 ms |
2640 KB |
Output is correct |
38 |
Correct |
16 ms |
2600 KB |
Output is correct |
39 |
Correct |
13 ms |
2640 KB |
Output is correct |
40 |
Correct |
12 ms |
2640 KB |
Output is correct |
41 |
Correct |
11 ms |
2640 KB |
Output is correct |
42 |
Correct |
14 ms |
2640 KB |
Output is correct |
43 |
Correct |
11 ms |
2640 KB |
Output is correct |
44 |
Correct |
9 ms |
2688 KB |
Output is correct |
45 |
Correct |
12 ms |
2668 KB |
Output is correct |
46 |
Correct |
10 ms |
2640 KB |
Output is correct |
47 |
Correct |
11 ms |
2640 KB |
Output is correct |
48 |
Correct |
12 ms |
2664 KB |
Output is correct |
49 |
Correct |
11 ms |
2640 KB |
Output is correct |
50 |
Correct |
13 ms |
2668 KB |
Output is correct |
51 |
Correct |
9 ms |
2640 KB |
Output is correct |
52 |
Correct |
13 ms |
2672 KB |
Output is correct |
53 |
Correct |
6 ms |
2640 KB |
Output is correct |
54 |
Correct |
9 ms |
2640 KB |
Output is correct |
55 |
Correct |
10 ms |
2640 KB |
Output is correct |
56 |
Correct |
8 ms |
2640 KB |
Output is correct |
57 |
Correct |
9 ms |
2640 KB |
Output is correct |
58 |
Correct |
13 ms |
2640 KB |
Output is correct |
59 |
Correct |
10 ms |
2680 KB |
Output is correct |
60 |
Correct |
12 ms |
2640 KB |
Output is correct |
61 |
Correct |
11 ms |
2640 KB |
Output is correct |
62 |
Correct |
12 ms |
2640 KB |
Output is correct |
63 |
Correct |
9 ms |
2672 KB |
Output is correct |
64 |
Correct |
9 ms |
2688 KB |
Output is correct |
65 |
Correct |
10 ms |
2668 KB |
Output is correct |
66 |
Correct |
14 ms |
2676 KB |
Output is correct |
67 |
Correct |
13 ms |
2768 KB |
Output is correct |
68 |
Correct |
10 ms |
2680 KB |
Output is correct |
69 |
Correct |
15 ms |
2672 KB |
Output is correct |
70 |
Correct |
14 ms |
2668 KB |
Output is correct |
71 |
Correct |
8 ms |
2640 KB |
Output is correct |
72 |
Correct |
10 ms |
2680 KB |
Output is correct |
73 |
Correct |
9 ms |
2692 KB |
Output is correct |
74 |
Correct |
9 ms |
2680 KB |
Output is correct |
75 |
Correct |
11 ms |
2672 KB |
Output is correct |
76 |
Correct |
8 ms |
2668 KB |
Output is correct |
77 |
Correct |
11 ms |
2640 KB |
Output is correct |
78 |
Correct |
12 ms |
2688 KB |
Output is correct |
79 |
Correct |
9 ms |
2640 KB |
Output is correct |
80 |
Correct |
14 ms |
2640 KB |
Output is correct |
81 |
Correct |
11 ms |
2668 KB |
Output is correct |
82 |
Correct |
11 ms |
2684 KB |
Output is correct |