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
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << x << '\n';
using namespace std;
vector<int> createFunTour(int n, int q) {
vector<int> res;
int C;
int k = 0;
vector<pair<int, int>> d[n];
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);
k = (int)c.size();
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 < k; i++) {
sort(d[i].rbegin(), d[i].rend());
}
vector<int> ds;
/*Step 4: Solve problem :)*/ {
res.push_back(C);
ds.push_back(0);
int lst = -1;
while ((int)res.size() < n) {
for (int i = 0; i < k; i++) {
debug((int)d[i].size());
}
cerr << '\n';
int s = 0;
for (int i = 0; i < k; i++)
s += (int)d[i].size();
int nxt = -1;
for (int i = 0; i < k; i++) {
if (d[i].empty() || i == lst)
continue;
if ((int)d[i].size() * 2 >= s) {
nxt = i;
cerr << "*\n";
break;
}
if (nxt == -1 || d[i].back().first < d[nxt].back().first)
nxt = i;
}
assert(nxt != -1);
if (ds.size() > 1 && ds[(int)ds.size() - 2] > d[nxt].back().first) {
cerr << "Caught the moron!\n";
return res;
}
ds.push_back(d[nxt].back().first);
res.push_back(d[nxt].back().second);
d[nxt].pop_back();
lst = nxt;
}
}
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... |