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 all(i) (i).begin(), (i).end()
using namespace std;
typedef pair<int, int> pi;
std::vector<int> createFunTour(int N, int Q) {
if (N == 2) {
vector<int> ans(1, 0);
ans.push_back(1);
return ans;
}
vector<int> dep(N), sz(N);
dep[0] = 0, sz[0] = N;
int cen = 0;
for (int i = 1; i < N; ++i) {
dep[i] = hoursRequired(0, i);
sz[i] = attractionsBehind(0, i);
if (dep[i] > dep[cen] && sz[i] > N / 2) cen = i;
}
vector<vector<int>> v(3);
vector<int> ch;
if (cen != 0) {
vector<int> dep0(all(dep));
for (int i = 0; i < N; ++i) dep[i] = i == cen ? 0 : hoursRequired(cen, i);
for (int i = 0; i < N; ++i) if (dep[i] == 1) ch.push_back(i);
for (int i = 0; i < ch.size(); ++i) if (dep0[ch[i]] < dep0[cen]) swap(ch[i], ch[0]);
for (int j = 0; j < ch.size(); ++j) v[j].push_back(ch[j]);
for (int i = 0; i < N; ++i) if (dep[i] > 1) {
bool b = false;
if (dep0[i] != dep0[cen] + dep[i]) v[0].push_back(i), b = true;
else for (int j = 1; j < ch.size() - 1; ++j)
if (dep[i] == 1 + hoursRequired(ch[j], i)) {
v[j].push_back(i);
b = true;
break;
}
if (!b) v[ch.size() - 1].push_back(i);
}
}
else {
for (int i = 0; i < N; ++i) dep[i] = i == cen ? 0 : hoursRequired(cen, i);
for (int i = 0; i < N; ++i) if (dep[i] == 1) ch.push_back(i);
for (int j = 0; j < ch.size(); ++j) v[j].push_back(ch[j]);
for (int i = 0; i < N; ++i) if (dep[i] > 1) {
bool b = false;
for (int j = 0; j < ch.size() - 1; ++j)
if (dep[i] == 1 + hoursRequired(ch[j], i)) {
v[j].push_back(i);
b = true;
break;
}
if (!b) v[ch.size() - 1].push_back(i);
}
}
for (int i = 0; i < 3; ++i) sort(all(v[i]), [&](int x, int y) {
return dep[x] < dep[y];
});
vector<int> ans;
int pre = -1;
for (int i = 0; i < N - 1; ++i) {
int tmp = -1;
for (int j = 0; j < 3; ++j) if (j != pre && !v[j].empty() && (tmp == -1 || dep[v[j].back()] > dep[v[tmp].back()] || (dep[v[j].back()] == dep[v[tmp].back()] && v[j].size() > v[tmp].size())))
tmp = j;
if (ch.size() == 2 && i == 0) tmp = v[0].size() < v[1].size();
//assert(tmp != -1);
ans.push_back(v[tmp].back());
v[tmp].pop_back();
pre = tmp;
}
ans.push_back(cen);
return ans;
}
/*
* n depth
* n sz
* cen = max dep && sz > n/2
* rt != cen ? n dis cen + n two ch
* : n first ch + n second ch
*
*/
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:26:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 0; i < ch.size(); ++i) if (dep0[ch[i]] < dep0[cen]) swap(ch[i], ch[0]);
| ~~^~~~~~~~~~~
fun.cpp:27:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int j = 0; j < ch.size(); ++j) v[j].push_back(ch[j]);
| ~~^~~~~~~~~~~
fun.cpp:31:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | else for (int j = 1; j < ch.size() - 1; ++j)
| ~~^~~~~~~~~~~~~~~
fun.cpp:43:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int j = 0; j < ch.size(); ++j) v[j].push_back(ch[j]);
| ~~^~~~~~~~~~~
fun.cpp:46:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int j = 0; j < ch.size() - 1; ++j)
| ~~^~~~~~~~~~~~~~~
# | 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... |