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 <bits/stdc++.h>
#include "fun.h"
#define get_dist hoursRequired
#define get_size attractionsBehind
#ifdef LOCAL
#define cerr cout
#else
#define cerr if (false) cout
#endif
using namespace std;
vector<int> createFunTour(int n, int q) {
vector<int> res;
int C;
vector<pair<int, int>> d[3];
vector<int> dist(n);
/*Step 1: Find Centroid */ {
// a) pick a random vertex
int v = 0;
// b) for all i, find i's subtree's size if tree is rooted by v
vector<int> sz(n);
for (int i = 0; i < n; i++)
sz[i] = get_size(v, i);
// c) centroid is i s.t, 2 * sz[i] > n and sz[i] - min
C = -1;
for (int i = 0; i < n; i++)
if (2 * sz[i] > n)
if (C == -1 || sz[i] < sz[C])
C = i;
}
/*Step 2: Find distance from C to any other vertices*/ {
for (int i = 0; i < n; i++)
dist[i] = get_dist(C, i);
}
/*Step 3: Divide all vertices to subrees of C*/ {
vector<int> c;
for (int i = 0; i < n; i++)
if (dist[i] == 1)
c.push_back(i);
for (int i = 0; i < n; i++) {
if (i == C) continue;
int j = 0;
while (j < (int)c.size() - 1) {
if (get_dist(i, c[j]) < dist[i]) break;
j++;
}
d[j].push_back({dist[i], i});
}
}
for (int i = 0; i < 3; i++) {
sort(d[i].rbegin(), d[i].rend());
}
/*Step 4: Solve problem :)*/ {
res.push_back(C);
int lst = -1;
while ((int)res.size() < n) {
cerr << "oper: " << (int)res.size() << '\n';
for (int i = 0; i < 3; i++) {
cerr << "{";
for (auto [x, y] : d[i]) cerr << '(' << x << ' ' << y << ") ";
cerr << "}\n";
}
cerr << '\n';
int s = 0;
for (int i = 0; i < 3; i++)
s += (int)d[i].size();
int nxt = -1;
for (int i = 0; i < 3; i++) {
if (d[i].empty() || i == lst)
continue;
if ((int)d[i].size() * 2 > s) {
nxt = i;
break;
}
if (nxt == -1 || d[i].back().first < d[nxt].back().first)
nxt = i;
}
assert(nxt != -1);
res.push_back(d[nxt].back().second);
d[nxt].pop_back();
lst = nxt;
}
}
for (int c : res)
cerr << c << ' ';
cerr << '\n';
reverse(res.begin(), res.end());
return res;
}
# | 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... |