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>
#define f first
#define s second
using namespace std;
vector<int> createFunTour(int N, int Q) {
int n = N;
int cen = 0;
if (n == 2)
return {0, 1};
for (int i = 1;i < n;i++) {
if (attractionsBehind(cen, i) * 2 >= n)
cen = i;
}
vector<int> dis(n);
for (int i = 0;i < n;i++) {
if (i == cen)
continue;
dis[i] = hoursRequired(cen, i);
}
vector<int> p;
for (int i = 0;i < n;i++) {
if (dis[i] == 1) {
p.push_back(i);
}
}
int m = p.size();
vector<int> ch[m];
for (int i = 0;i < n;i++) {
if (i == cen)
continue;
for (int j = 0;j < m;j++) {
if (j == m - 1) {
ch[j].push_back(i);
}
else {
int x = hoursRequired(p[j], i);
if (x + 1 == dis[i]) {
ch[j].push_back(i);
break;
}
}
}
}
for (int i = 0;i < m;i++) {
sort(ch[i].begin(), ch[i].end(), [&](int x, int y) {
return dis[x] < dis[y];
});
}
vector<int> ans;
auto Resi2 = [&](vector<int> s1, vector<int> s2) {
if (s1.size() > s2.size())
swap(s1, s2);
while (s2.size() + s1.size()) {
ans.push_back(s2.back());
s2.pop_back();
swap(s1, s2);
}
};
if (m == 2) {
Resi2(ch[0], ch[1]);
}
else {
int lst = -1, z = 0;
while (1) {
int u = 0, mx = 0;
for (int i = 0;i < m;i++) {
mx = max(mx, (int)ch[i].size());
u += ch[i].size();
}
if (mx >= u - mx)
break;
mx = 0;
z++;
int node = -1;
for (int j = 0;j < m;j++) {
if (j == lst)
continue;
if (dis[ch[j].back()] >= mx) {
mx = dis[ch[j].back()];
node = j;
}
}
assert(node != -1);
ans.push_back(ch[node].back());
ch[node].pop_back();
lst = node;
}
int bst = 0;
for (int i = 1;i < m;i++) {
if (ch[i].size() > ch[bst].size())
bst = i;
}
vector<int> all;
int pos = 0;
int mx = 0;
for (int i = 0;i < m;i++) {
if (i == lst)
continue;
if (dis[ch[i].back()] > mx) {
pos = i;
mx = dis[ch[i].back()];
}
}
if (pos != bst && dis[ch[pos].back()] > dis[ch[bst].back()] && z > 1) {
ans.push_back(ch[pos].back());
ch[pos].pop_back();
}
for (int i = 0;i < m;i++) {
if (i == bst)
continue;
for (auto u : ch[i])
all.push_back(u);
}
sort(all.begin(), all.end(), [&](int i, int j) {
return dis[i] < dis[j];
});
//assert(all.size() + 1 == ch[bst].size());
if (lst != bst)
Resi2(all, ch[bst]);
else
Resi2(ch[bst], all);
}
ans.push_back(cen);
return ans;
}
# | 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... |