#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));
vector<vector<int>> adj(N);
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (are_connected({i}, {j})) {
adj[i].emplace_back(j);
adj[j].emplace_back(i);
d[i][j] = d[j][i] = 1;
}
}
}
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 {
vector<int> vis(N, 0);
int cc = 0;
vector<int> ls[2];
function<void(int, vector<int>&)> dfs = [&](int u, vector<int>& ls) {
vis[u] = 1;
ls.emplace_back(u);
for (int v : adj[u]) {
if (!vis[v]) dfs(v, ls);
}
};
for (int i = 0; i < N; i++) {
if (!vis[i]) dfs(i, ls[cc++]);
}
if (cc == 2) {
if (ls[0].size() < ls[1].size()) swap(ls[0], ls[1]);
return ls[0];
}
deque<int> ans;
auto add = [&](int x, int _) {
if (_) {
ans.emplace_back(x);
} else {
ans.emplace_front(x);
}
};
auto rotate = [&](int x) {
deque<int> tmp;
while (ans.back() != x) {
tmp.emplace_back(ans.back());
ans.pop_back();
}
ans.pop_back();
tmp.emplace_back(x);
while (ans.size()) {
tmp.emplace_front(ans.front());
ans.pop_front();
}
ans = tmp;
};
function<void(int, int)> solve = [&](int u, int _) {
vis[u] = 0;
add(u, _);
int fst = 1;
for (int v : adj[u]) {
if (vis[v] == 0) continue;
if (fst) {
add(v, _);
} else {
int x = ans.front();
int y = ans.back();
if (d[x][v]) {
solve(v, 0);
} else if (d[y][v]) {
solve(v, 1);
} else {
assert(d[x][y]);
rotate(u);
solve(v, 1);
}
}
}
};
solve(0, 0);
vector<int> res;
for (int i : ans) res.emplace_back(i);
return res;
}
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:28:47: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
28 | while (ans.size() + 2 <= N) {
| ~~~~~~~~~~~~~~~^~~~
longesttrip.cpp:54:40: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | if (ans.size() == N - 1) {
| ~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
127 ms |
856 KB |
Output is correct |
4 |
Correct |
340 ms |
1020 KB |
Output is correct |
5 |
Correct |
755 ms |
1856 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
20 ms |
344 KB |
Output is correct |
8 |
Correct |
112 ms |
600 KB |
Output is correct |
9 |
Correct |
260 ms |
1104 KB |
Output is correct |
10 |
Correct |
732 ms |
2160 KB |
Output is correct |
11 |
Correct |
745 ms |
1488 KB |
Output is correct |
12 |
Correct |
736 ms |
1660 KB |
Output is correct |
13 |
Correct |
729 ms |
1816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
25 ms |
344 KB |
Output is correct |
3 |
Correct |
140 ms |
856 KB |
Output is correct |
4 |
Correct |
335 ms |
840 KB |
Output is correct |
5 |
Correct |
706 ms |
1868 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
29 ms |
344 KB |
Output is correct |
3 |
Partially correct |
114 ms |
1104 KB |
Output is partially correct |
4 |
Partially correct |
340 ms |
1328 KB |
Output is partially correct |
5 |
Partially correct |
752 ms |
1348 KB |
Output is partially correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |