This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "fun.h"
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
mt19937 rng(101202);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r) {
if (l > r) { cerr << l << " " << r << "\n"; }
return l + rng() % (r - l + 1);
}
const int N = 100010;
set<pair<int, int>> children[N * 4];
vector<int> createFunTour(int n, int q) {
if (n <= 500) {
vector<vector<int>> distance(n, vector<int>(n));
vector<pair<int, int>> edges;
vector<bool> visited(n);
for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) {
distance[i][j] = distance[j][i] = hoursRequired(i, j);
edges.push_back({i, j});
edges.push_back({j, i});
}
sort(edges.begin(), edges.end(), [&](pair<int, int> x, pair<int, int> y) {
return distance[x.first][x.second] > distance[y.first][y.second];
});
int currentVertex = edges[0].first, fun = n;
visited[currentVertex] = true;
vector<int> answer = {currentVertex};
for (int i = 1; i < n; ++i) {
bool found = false;
for (const auto [x, y] : edges) {
if (x != currentVertex || !visited[x] || visited[x] && visited[y] || fun < distance[x][y]) continue;
answer.push_back(currentVertex = y);
visited[y] = true;
found = true;
break;
}
assert(found);
}
return answer;
}
function<void(int)> build = [&](int s) {
if (s >= n) return;
build(2 * s + 1); build(2 * s + 2);
children[s].insert({0, s});
for (const auto &[c, v] : children[2 * s + 1]) children[s].insert({c + 1, v});
for (const auto &[c, v] : children[2 * s + 2]) children[s].insert({c + 1, v});
};
build(0);
vector<bool> visited(n);
function getSubtree = [&](int u) -> pair<int, int> {
while (children[u].size() && visited[children[u].rbegin()->second]) children[u].erase(*children[u].rbegin());
return children[u].size() ? *children[u].rbegin() : make_pair(0, -1);
};
function getSubtree_Left = [&](int u) -> pair<int, int> {
return getSubtree(u * 2 + 1);
};
function getSubtree_Right = [&](int u) -> pair<int, int> {
return getSubtree(u * 2 + 2);
};
int currentVertex = 0; while (currentVertex * 2 + 1 < n) currentVertex = currentVertex * 2 + 1;
visited[currentVertex] = true;
vector<int> answer = {currentVertex};
for (int i = 1; i < n; ++i) {
pair<
int, // distance
int // vertex
> currentBest;
currentBest = getSubtree(currentVertex);
for (int u = currentVertex, level = 1; u > 0; u = (u - 1) / 2, level += 1) {
int p = (u - 1) / 2;
auto thisBest_ = u == p * 2 + 1 ? getSubtree_Right(p) : getSubtree_Left(p);
pair<int, int> thisBest = {0, -1};
if (!visited[p]) thisBest = {level, p};
if (thisBest_.second != -1) thisBest = {thisBest.first + thisBest_.first, thisBest_.second};
if (thisBest > currentBest) currentBest = thisBest;
}
visited[currentVertex = currentBest.second] = true;
answer.push_back(currentVertex);
}
return answer;
}
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:85:61: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
85 | if (x != currentVertex || !visited[x] || visited[x] && visited[y] || fun < distance[x][y]) continue;
| ~~~~~~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |