#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define sz size()
#define all(x) (x).begin(), (x).end()
vector<int> v1,v2;
vector<int> go(ll x, ll y)
{
if(x) reverse(all(v1));
if(y) reverse(all(v2));
for(int i = 0; i < v2.sz; i++) v1.push_back(v2[i]);
return v1;
}
std::vector<int> longest_trip(int N, int D)
{
vector<ll> sh;
for(int i = 0; i < N; i++) sh.push_back(i);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(all(sh), g);
mt19937 rnd(time(0));
v1.clear(); v2.clear();
v1.push_back(sh[0]);
for(int ii = 1; ii < N; ii++)
{
int i = sh[ii];
ll j = rnd()%2;
if(j) swap(v1,v2);
bool bo = 0;
if(v1.sz)
bo = are_connected({v1.back()},{i});
if(bo==1) v1.push_back(i);
else
{
if(v2.sz==0) v2.push_back(i);
else
{
bo = are_connected({v2.back()},{i});
if(bo==1) v2.push_back(i);
else
{
reverse(all(v2));
for(int j = 0; j < v2.sz; j++) v1.push_back(v2[j]); v2.clear();
v2.push_back(i);
}
}
}
}
if(v2.sz==0) return v1;
if(v1.sz==0) return v2;
//if(v1.sz>v2.sz) return v1; else return v2;
bool b1 = are_connected({v1[0]},{v2[0]});
bool b2 = are_connected({v1[0]},{v2.back()});
bool b3 = are_connected({v1.back()},{v2[0]});
bool b4 = are_connected({v1.back()},{v2.back()});
if(b1)
{
return go(1,0);
}
else if(b2)
{
return go(1,1);
}
else if(b3)
{
return go(0,0);
}
else if(b4)
{
return go(0,1);
}
// not connecteed
bool both = are_connected(v1,v2);
if(both==0)
{
if(v1.sz>v2.sz) return v1; else return v2;
}
// connected
ll l = 0, r = v1.sz-1;
while(l<r)
{
ll mid = (l+r)/2;
vector<int> vv;
for(int i = l; i <= mid; i++) vv.push_back(v1[i]);
bool m = are_connected(vv,v2);
if(m) r = mid; else l = mid+1;
}
ll node1 = l;
l = 0, r = v2.sz-1;
while(l<r)
{
ll mid = (l+r)/2;
vector<int> vv;
for(int i = l; i <= mid; i++) vv.push_back(v2[i]);
bool m = are_connected(vv,{v1[node1]});
if(m) r = mid; else l = mid+1;
}
ll node2 = l;
vector<int> ans;
for(int i = node1+1; i < v1.sz; i++) ans.push_back(v1[i]);
for(int i = 0; i <= node1; i++) ans.push_back(v1[i]);
for(int i = node2; i < v2.sz; i++) ans.push_back(v2[i]);
for(int i = 0; i < node2; i++) ans.push_back(v2[i]);
return ans;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> go(ll, ll)':
longesttrip.cpp:15:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i < v2.sz; i++) v1.push_back(v2[i]);
| ^
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:47:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int j = 0; j < v2.sz; j++) v1.push_back(v2[j]); v2.clear();
| ^
longesttrip.cpp:47:21: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
47 | for(int j = 0; j < v2.sz; j++) v1.push_back(v2[j]); v2.clear();
| ^~~
longesttrip.cpp:47:73: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
47 | for(int j = 0; j < v2.sz; j++) v1.push_back(v2[j]); v2.clear();
| ^~
longesttrip.cpp:107:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i = node1+1; i < v1.sz; i++) ans.push_back(v1[i]);
| ^
longesttrip.cpp:109:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = node2; i < v2.sz; i++) ans.push_back(v2[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 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 |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
3 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
452 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
3 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
448 KB |
Output is correct |
9 |
Correct |
7 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
16 ms |
344 KB |
Output is correct |
15 |
Correct |
12 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
344 KB |
Output is correct |
19 |
Correct |
7 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
5 ms |
452 KB |
Output is correct |
23 |
Correct |
4 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
456 KB |
Output is correct |
25 |
Correct |
12 ms |
344 KB |
Output is correct |
26 |
Correct |
20 ms |
344 KB |
Output is correct |
27 |
Correct |
10 ms |
344 KB |
Output is correct |
28 |
Correct |
11 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
8 ms |
444 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
344 KB |
Output is correct |
37 |
Correct |
11 ms |
344 KB |
Output is correct |
38 |
Correct |
9 ms |
344 KB |
Output is correct |
39 |
Correct |
9 ms |
344 KB |
Output is correct |
40 |
Correct |
11 ms |
344 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
8 ms |
344 KB |
Output is correct |
45 |
Correct |
11 ms |
344 KB |
Output is correct |
46 |
Correct |
14 ms |
344 KB |
Output is correct |
47 |
Correct |
12 ms |
344 KB |
Output is correct |
48 |
Correct |
10 ms |
344 KB |
Output is correct |
49 |
Correct |
10 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
6 ms |
344 KB |
Output is correct |
52 |
Correct |
13 ms |
600 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
340 KB |
Output is correct |
56 |
Correct |
9 ms |
344 KB |
Output is correct |
57 |
Correct |
8 ms |
344 KB |
Output is correct |
58 |
Correct |
8 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
344 KB |
Output is correct |
60 |
Correct |
9 ms |
340 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
8 ms |
344 KB |
Output is correct |
63 |
Correct |
9 ms |
600 KB |
Output is correct |
64 |
Correct |
9 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
408 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
16 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
452 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
21 ms |
344 KB |
Output is correct |
15 |
Correct |
13 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
344 KB |
Output is correct |
17 |
Correct |
9 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
9 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
11 ms |
344 KB |
Output is correct |
22 |
Correct |
14 ms |
344 KB |
Output is correct |
23 |
Correct |
9 ms |
344 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
12 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
344 KB |
Output is correct |
31 |
Correct |
7 ms |
344 KB |
Output is correct |
32 |
Correct |
13 ms |
344 KB |
Output is correct |
33 |
Correct |
12 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
344 KB |
Output is correct |
35 |
Correct |
10 ms |
340 KB |
Output is correct |
36 |
Correct |
12 ms |
344 KB |
Output is correct |
37 |
Correct |
8 ms |
344 KB |
Output is correct |
38 |
Correct |
9 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
4 ms |
344 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
8 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
4 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
7 ms |
344 KB |
Output is correct |
48 |
Correct |
10 ms |
456 KB |
Output is correct |
49 |
Correct |
5 ms |
344 KB |
Output is correct |
50 |
Correct |
7 ms |
344 KB |
Output is correct |
51 |
Partially correct |
7 ms |
344 KB |
Output is partially correct |
52 |
Correct |
11 ms |
344 KB |
Output is correct |
53 |
Correct |
8 ms |
600 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
8 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
344 KB |
Output is correct |
57 |
Correct |
8 ms |
344 KB |
Output is correct |
58 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
59 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
60 |
Correct |
10 ms |
344 KB |
Output is correct |
61 |
Partially correct |
6 ms |
456 KB |
Output is partially correct |
62 |
Correct |
5 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
344 KB |
Output is correct |
64 |
Partially correct |
11 ms |
344 KB |
Output is partially correct |
65 |
Correct |
9 ms |
344 KB |
Output is correct |
66 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
67 |
Partially correct |
6 ms |
344 KB |
Output is partially correct |
68 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
69 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
70 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
71 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
72 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |
73 |
Partially correct |
10 ms |
448 KB |
Output is partially correct |
74 |
Correct |
6 ms |
344 KB |
Output is correct |
75 |
Correct |
8 ms |
452 KB |
Output is correct |
76 |
Partially correct |
10 ms |
344 KB |
Output is partially correct |
77 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
78 |
Partially correct |
10 ms |
344 KB |
Output is partially correct |
79 |
Correct |
6 ms |
344 KB |
Output is correct |
80 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
81 |
Partially correct |
5 ms |
344 KB |
Output is partially correct |
82 |
Partially correct |
9 ms |
344 KB |
Output is partially correct |