#include "longesttrip.h"
using namespace std;
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l); i < (r); ++i)
#define A(a) (a).begin(), (a).end()
int edge[300][300];
int parent[300];
int find(int i) {
return i == parent[i] ? i : parent[i] = find(parent[i]);
}
int GN;
bool query(int A, int B) {
assert(min(A, B) >= 0 and max(A, B) < GN);
return edge[A][B] | edge[B][A];
}
vector<int> longest_trip(int N, int ______________)
{
GN = N;
// okay we're just going to build the spanning tree straight up, fuck this shit.
iota(parent, parent + N, 0ll);
F(i, 0, N) F(j, i + 1, N) {
if (are_connected({i}, {j})) {
parent[find(i)] = find(j);
edge[i][j] = edge[j][i] = 1;
}
}
F(i, 0, N) if (find(i) != find(0)) {
vector<int> v[2];
F(j, 0, N) v[find(j) == find(0)].push_back(j);
if (v[0].size() < v[1].size()) swap(v[0], v[1]);
auto hamil = v[0];
F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
return v[0];
}
vector<int> hamil;
auto gen = [&]() {
set<int> v;
F(i, 0, N) if (find(A(hamil), i) == hamil.end()) v.insert(i);
return v;
};
if (query({0}, {1})) {
hamil = {0, 1};
} else if (query({1}, {2})) {
hamil = {1, 2};
} else {
hamil = {0, 2};
}
while (hamil.size() != N) { // this will reach N, with the exception of one case
// cout << "HOW " << endl;
F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
if (query({hamil[0]}, {hamil.back()})) {
// its true that we have a hamiltonian CYCLE, so any rotation is valid.
} else {
// Pick any node not in hamil, this must be connected to either the front or the back.
auto i = *gen().begin();
if (query({hamil[0]}, {i})) hamil.insert(hamil.begin(), i);
else hamil.push_back(i);
continue;
}
// Okay, we want to find *any* connection between hamil and others, if exists already.
auto bad = gen();
bool seen_edge = false;
F(___, 0, hamil.size()) {
auto v = hamil.back(); hamil.pop_back(); hamil.insert(hamil.begin(), v);
for (auto y: bad) if (edge[v][y]) {
hamil.insert(hamil.begin(), y);
seen_edge = true;
goto end;
}
}
end:
assert(seen_edge); // we already handle the CC = 2 case up there in this brute force approach
}
F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
return hamil;
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
| ^
longesttrip.cpp:41:9: note: in expansion of macro 'F'
41 | F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
| ^
longesttrip.cpp:62:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
62 | while (hamil.size() != N) { // this will reach N, with the exception of one case
| ~~~~~~~~~~~~~^~~~
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
| ^
longesttrip.cpp:65:9: note: in expansion of macro 'F'
65 | F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
| ^
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
| ^
longesttrip.cpp:82:9: note: in expansion of macro 'F'
82 | F(___, 0, hamil.size()) {
| ^
longesttrip.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define F(i, l, r) for (int i = (l); i < (r); ++i)
| ^
longesttrip.cpp:94:9: note: in expansion of macro 'F'
94 | F(i, 1, hamil.size()) assert(query(hamil[i-1], hamil[i]));
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
200 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
344 KB |
Output is correct |
3 |
Correct |
172 ms |
344 KB |
Output is correct |
4 |
Correct |
427 ms |
572 KB |
Output is correct |
5 |
Correct |
921 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
344 KB |
Output is correct |
3 |
Correct |
155 ms |
500 KB |
Output is correct |
4 |
Correct |
421 ms |
344 KB |
Output is correct |
5 |
Correct |
906 ms |
716 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
22 ms |
340 KB |
Output is correct |
3 |
Correct |
157 ms |
344 KB |
Output is correct |
4 |
Correct |
400 ms |
344 KB |
Output is correct |
5 |
Correct |
860 ms |
724 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
344 KB |
Output is correct |
3 |
Partially correct |
173 ms |
600 KB |
Output is partially correct |
4 |
Partially correct |
414 ms |
568 KB |
Output is partially correct |
5 |
Partially correct |
911 ms |
728 KB |
Output is partially correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |