#include <bits/stdc++.h>
using namespace std;
vector<int> path;
vector<int> mxpath;
int n;
set<int> have;
const int maxN = 256;
bool connected[maxN][maxN]={};
bool ans[maxN][maxN]={};
bool are_connected(vector<int> A, vector<int> B);
void dfs(int v) {
path.push_back(v);
have.insert(v);
if (path.size() == n) {
return;
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
random_shuffle(ord.begin(), ord.end());
for (int i : ord) {
if (!have.count(i)) {
bool c = false;
if (ans[v][i]) {
c = connected[v][i];
}
else {
ans[v][i] = ans[i][v] = true;
bool x = are_connected(vector<int>{i}, vector<int>{v});
connected[v][i] = connected[i][v] = x;
c = x;
}
if (c)
dfs(i);
}
if (path.size() == n) return;
}
if (path.size() == n) return;
if (path.size() > mxpath.size()) {
mxpath = path;
}
path.pop_back();
have.erase(v);
}
vector<int> longest_trip(int N, int D) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
connected[i][j] = false;
ans[i][j] = false;
}
}
n = N;
int start = 0;
path.clear();
have.clear();
vector<int> o(n);
iota(o.begin(), o.end(), 0);
random_shuffle(o.begin(), o.end());
for (int i = 0; i < min(n, 50); ++i) {
path.clear();
have.clear();
dfs(o[i]);
if (path.size() == n) return path;
}
return mxpath.size() > path.size() ? mxpath : path;
}
Compilation message
longesttrip.cpp: In function 'void dfs(int)':
longesttrip.cpp:18:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
18 | if (path.size() == n) {
| ~~~~~~~~~~~~^~~~
longesttrip.cpp:39:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
39 | if (path.size() == n) return;
| ~~~~~~~~~~~~^~~~
longesttrip.cpp:41:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
41 | if (path.size() == n) return;
| ~~~~~~~~~~~~^~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:67:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
67 | if (path.size() == n) return path;
| ~~~~~~~~~~~~^~~~
longesttrip.cpp:57:9: warning: unused variable 'start' [-Wunused-variable]
57 | int start = 0;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
7 ms |
856 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
856 KB |
Output is correct |
11 |
Correct |
8 ms |
856 KB |
Output is correct |
12 |
Correct |
7 ms |
856 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
8 ms |
940 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
856 KB |
Output is correct |
11 |
Correct |
7 ms |
856 KB |
Output is correct |
12 |
Correct |
7 ms |
856 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
14 |
Incorrect |
0 ms |
344 KB |
Incorrect |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
7 ms |
600 KB |
Output is correct |
5 |
Correct |
10 ms |
856 KB |
Output is correct |
6 |
Correct |
5 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 |
776 KB |
Output is correct |
11 |
Correct |
9 ms |
856 KB |
Output is correct |
12 |
Correct |
8 ms |
856 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
14 |
Incorrect |
1 ms |
344 KB |
Incorrect |
15 |
Halted |
0 ms |
0 KB |
- |