이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "fun.h"
#include <bits/stdc++.h>
#ifdef LOCAL
#define clog std::cerr
#else
#define clog if (0) std::cerr
#endif
std::vector<int> createFunTour(int N, int Q) {
std::vector<std::pair<int, int>> subtree_size(N);
subtree_size[0] = {N, 0};
for (int i = 1; i < N; i++) {
subtree_size[i] = {attractionsBehind(0, i), i};
}
std::sort(subtree_size.begin(), subtree_size.end());
int centroid = -1;
for (centroid = 0; centroid < N; centroid++) {
if (subtree_size[centroid].first >= (N + 1) / 2) {
centroid = subtree_size[centroid].second;
break;
}
}
assert(centroid != -1);
std::vector<int> adj;
std::vector<int> d(N);
for (int i = 0; i < N; i++) {
if (i == centroid) {
d[i] = 0;
} else {
d[i] = hoursRequired(centroid, i);
}
if (d[i] == 1) {
adj.push_back(i);
}
}
std::vector<int> g(N, -1);
int sz = adj.size();
std::vector<std::vector<int>> f(sz);
for (int i = 0; i < N; i++) {
if (i == centroid) continue;
g[i] = 0;
while (g[i] + 1 < (int) adj.size() && hoursRequired(adj[g[i]], i) > d[i]) {
g[i]++;
}
f[g[i]].push_back(i);
}
std::vector<int> p(sz);
std::iota(p.begin(), p.end(), 0);
int last = -1;
std::vector<int> tour;
if (sz == 3) {
while (true) {
std::sort(p.begin(), p.end(),
[&](int i, int j) { return f[i].size() > f[j].size(); });
if (f[p[0]].size() == f[p[1]].size() + f[p[2]].size()) {
break;
}
// we can still do that
std::sort(p.begin(), p.end(),
[&](int i, int j) { return d[f[i].back()] > d[f[j].back()]; });
int now = p[p[0] == last];
tour.push_back(f[now].back());
f[now].pop_back();
last = now;
}
for (int i : f[p[2]]) {
g[i] = p[1];
f[p[1]].emplace_back(i);
}
f[p[2]].clear();
std::sort(f[p[1]].begin(), f[p[1]].end(),
[&](int i, int j) { return d[i] < d[j]; });
p.pop_back();
}
if (last != -1) {
last = p[1];
if (d[f[1].back()] > d[tour.back()]) last = p[0];
} else {
last = p[1];
}
while (f[p[0]].size() || f[p[1]].size()) {
std::sort(p.begin(), p.end(),
[&](int i, int j) { return d[f[i].back()] > d[f[j].back()]; });
int now = p[p[0] == last];
tour.push_back(f[now].back());
f[now].pop_back();
last = now;
}
tour.push_back(centroid);
return tour;
}
# | 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... |