#include "longesttrip.h"
// 123
#include <bits/stdc++.h>
using namespace std;
std::vector<int> longest_trip(int N, int D) {
if (D == 3) {
vector<int> ans(N);
iota(ans.begin(), ans.end(), 0);
return ans;
} else {
vector<vector<int>> d(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
d[i][j] = d[j][i] = are_connected({i}, {j});
}
}
if (D == 2) {
vector<int> ans(1, 0);
vector<int> vis(N, 0);
vis[0] = 1;
while (ans.size() + 2 <= N) {
int i = 0, j = 0;
for (; i < N; i++) {
if (vis[i] == 0) break;
}
for (j = i + 1; j < N; j++) {
if (vis[j] == 0) break;
}
assert(i < N && j < N);
int x = ans.back();
if (d[x][i] && d[i][j]) {
ans.emplace_back(i), ans.emplace_back(j);
vis[i] = vis[j] = 1;
} else if (d[x][j] && d[j][i]) {
ans.emplace_back(j), ans.emplace_back(i);
vis[i] = vis[j] = 1;
} else {
assert(d[x][i] && d[x][j]);
ans.emplace_back(i);
vis[i] = 1;
}
}
int i = 0;
for (; i < N; i++) {
if (vis[i] == 0) break;
}
if (ans.size() == N - 1) {
if (d[ans.back()][i]) {
ans.emplace_back(i);
} else if (d[ans.front()][i]) {
ans.insert(ans.begin(), i);
} else {
assert(0);
}
}
return ans;
} else {
return {};
}
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:23:47: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
23 | while (ans.size() + 2 <= N) {
| ~~~~~~~~~~~~~~~^~~~
longesttrip.cpp:49:40: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | if (ans.size() == N - 1) {
| ~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Correct |
125 ms |
344 KB |
Output is correct |
4 |
Correct |
343 ms |
476 KB |
Output is correct |
5 |
Correct |
723 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
29 ms |
344 KB |
Output is correct |
8 |
Correct |
114 ms |
344 KB |
Output is correct |
9 |
Correct |
286 ms |
344 KB |
Output is correct |
10 |
Correct |
727 ms |
600 KB |
Output is correct |
11 |
Correct |
739 ms |
600 KB |
Output is correct |
12 |
Correct |
751 ms |
600 KB |
Output is correct |
13 |
Correct |
737 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |