#include <bits/stdc++.h>
#include "longesttrip.h"
using namespace std;
vector<int> longest_trip(int n, int D)
{
vector<int> used(n, 0);
vector<int> v1, v2, cur;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
if (are_connected({i}, {j}))
{
v1.push_back(i), v1.push_back(j);
used[i] = used[j] = 1;
break;
}
}
if (!v1.empty()) break;
}
for (int i = 0; i < n; i++)
{
if (used[i]) continue;
if (!v1.empty() && !v2.empty() && are_connected(v1, {i}) && are_connected(v2, {i}))
{
cur.clear();
for (int x : v1) cur.push_back(x);
cur.push_back(i);
for (int x : v2) cur.push_back(x);
v1 = cur;
}
else if (are_connected(v1, {i}))
{
vector<int> nw;
int idx = -1;
for (int j = 0; j < v1.size(); j++)
{
if (are_connected({i}, {v1[j]}))
{
idx = j;
break;
}
}
nw.push_back(i), nw.push_back(v1[idx]);
for (int j = (idx + 1) % v1.size(); j != idx; j = (j + 1) % v1.size())
{
nw.push_back(v1[j]);
}
v1 = nw;
}
else
{
if (v2.empty()) v2.push_back(i);
else
{
vector<int> nw;
int idx = -1;
for (int j = 0; j < v2.size(); j++)
{
if (are_connected({i}, {v2[j]}))
{
idx = j;
break;
}
}
nw.push_back(i), nw.push_back(v2[idx]);
for (int j = (idx + 1) % v2.size(); j != idx; j = (j + 1) % v2.size())
{
nw.push_back(v2[j]);
}
v2 = nw;
}
}
// cout << i << ":\n";
// for (int x : v1) cout << x << ' ';
// cout << '\n';
// for (int x : v2) cout << x << ' ';
// cout << '\n';
// for (int x : cur) cout << x << ' ';
// cout << '\n';
}
if (max({v1.size(), v2.size(), cur.size()}) == v1.size()) return v1;
if (max({v1.size(), v2.size(), cur.size()}) == v2.size()) return v2;
return cur;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:38:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int j = 0; j < v1.size(); j++)
| ~~^~~~~~~~~~~
longesttrip.cpp:60:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int j = 0; j < v2.size(); j++)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
5 ms |
760 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
9 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
14 ms |
444 KB |
Output is correct |
5 |
Correct |
18 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
13 ms |
696 KB |
Output is correct |
5 |
Correct |
20 ms |
1004 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
344 KB |
Output is correct |
9 |
Correct |
13 ms |
856 KB |
Output is correct |
10 |
Correct |
20 ms |
504 KB |
Output is correct |
11 |
Correct |
18 ms |
504 KB |
Output is correct |
12 |
Correct |
19 ms |
756 KB |
Output is correct |
13 |
Correct |
22 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
600 KB |
Output is correct |
4 |
Correct |
14 ms |
704 KB |
Output is correct |
5 |
Correct |
18 ms |
620 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
12 ms |
600 KB |
Output is correct |
9 |
Correct |
13 ms |
604 KB |
Output is correct |
10 |
Correct |
19 ms |
508 KB |
Output is correct |
11 |
Correct |
20 ms |
744 KB |
Output is correct |
12 |
Correct |
22 ms |
964 KB |
Output is correct |
13 |
Correct |
18 ms |
500 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
1 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 |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
11 ms |
600 KB |
Output is correct |
4 |
Correct |
17 ms |
696 KB |
Output is correct |
5 |
Partially correct |
18 ms |
748 KB |
Output is partially correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
11 ms |
344 KB |
Output is correct |
8 |
Correct |
11 ms |
344 KB |
Output is correct |
9 |
Correct |
13 ms |
444 KB |
Output is correct |
10 |
Partially correct |
19 ms |
752 KB |
Output is partially correct |
11 |
Partially correct |
21 ms |
752 KB |
Output is partially correct |
12 |
Partially correct |
18 ms |
1256 KB |
Output is partially correct |
13 |
Partially correct |
19 ms |
504 KB |
Output is partially correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Incorrect |
2 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |