이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |