#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int hoursRequired(int X, int Y);
int attractionsBehind(int X, int Y);
namespace k1r1t0 {
const int N = 110000;
bool used[N];
int n, d[N], ds[N][2];
vector<int> v[3];
vector<int> createFunTour(int N, int Q) {
n = N;
array<int, 2> mn = {n, 0};
for (int i = 1; i < n; i++) {
int sz = attractionsBehind(0, i);
if (sz >= n / 2) mn = min(mn, {sz, i});
}
int r = mn[1];
for (int i = 0; i < n; i++)
d[i] = hoursRequired(r, i);
vector<int> sub;
for (int i = 0; i < n; i++)
if (d[i] == 1)
sub.push_back(i);
int cnt = (int) sub.size();
for (int i = 0; i < cnt - 1; i++)
for (int j = 0; j < n; j++)
ds[j][i] = hoursRequired(sub[i], j);
for (int i = 0; i < n; i++) {
if (i == r) continue;
int t = cnt - 1;
for (int j = 0; j < cnt - 1; j++)
if (ds[i][j] + 1 == d[i])
t = j;
v[t].push_back(i);
}
if (cnt == 1) {
assert(n == 2);
return {0, 1};
}
int sm = 0;
for (int i = 1; i <= 2; i++)
if (v[sm].size() > v[i].size())
sm = i;
v[sm].push_back(r);
for (int k = 0; k < 3; k++)
sort(begin(v[k]), end(v[k]), [&](int i, int j) {return d[i] < d[j];});
vector<int> ans;
int t = 0;
for (int i = 1; i <= 2; i++)
if (d[v[i].back()] > d[v[t].back()])
t = i;
for (int i = 0; i < n; i++) {
int j = (t + 1) % 3;
int k = (t + 2) % 3;
int a = v[t].size();
int b = v[j].size();
int c = v[k].size();
if (b > c) {
swap(j, k);
swap(b, c);
}
if (a - 1 + b < c && b != 0) {
vector<int> na;
na = v[j];
na.insert(na.end(), v[t].begin(), v[t].end());
v[t] = na;
ans.push_back(v[t].back());
v[t].pop_back();
sort(begin(v[t]), end(v[t]), [&](int i, int j) {return d[i] < d[j];});
t = k;
} else {
ans.push_back(v[t].back());
v[t].pop_back();
if (v[j].empty()) t = k;
else if (v[k].empty()) t = j;
else {
if (d[v[j].back()] > d[v[k].back()]) t = j;
else t = k;
}
}
}
return ans;
}
};
vector<int> createFunTour(int N, int Q) {
return k1r1t0::createFunTour(N, Q);
}
/*
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
static void wrongAnswer(std::string message) {
printf("WA: %s\n", message.c_str());
exit(0);
}
namespace tree_helper {
static int N;
static int logN;
static std::vector<std::vector<int>> parent;
static std::vector<int> depth;
static std::vector<int> subtree_size;
static void dfs(
const std::vector<std::vector<int>>& adj_list,
int current_node, int parent_node) {
parent[0][current_node] = parent_node;
subtree_size[current_node] = 1;
for (int i = 0; i < static_cast<int>(adj_list[current_node].size()); ++i) {
const int next_node = adj_list[current_node][i];
if (next_node != parent_node) {
depth[next_node] = depth[current_node] + 1;
dfs(adj_list, next_node, current_node);
subtree_size[current_node] += subtree_size[next_node];
}
}
}
static void initializeTree(const std::vector<std::vector<int>>& adj_list) {
N = static_cast<int>(adj_list.size());
depth = std::vector<int>(N, 0);
subtree_size = std::vector<int>(N, 0);
for (logN = 0; (1 << logN) < N; ++logN) {}
parent = std::vector<std::vector<int>>(logN, std::vector<int>(N, 0));
dfs(adj_list, 0, 0);
for (int i = 1; i < logN; ++i) {
for (int j = 0; j < N; ++j) {
parent[i][j] = parent[i - 1][parent[i - 1][j]];
}
}
}
static int getLowestCommonAncestor(int X, int Y) {
if (depth[X] < depth[Y]) {
std::swap(X, Y);
}
for (int i = logN - 1; i >= 0; --i) {
if (depth[parent[i][X]] >= depth[Y]) {
X = parent[i][X];
}
}
if (X == Y) {
return X;
}
for (int i = logN - 1; i >= 0; --i) {
if (parent[i][X] != parent[i][Y]) {
X = parent[i][X];
Y = parent[i][Y];
}
}
return parent[0][X];
}
static int getDistance(int X, int Y) {
return depth[X] + depth[Y] - 2 * depth[getLowestCommonAncestor(X, Y)];
}
static int attractionsBehind(int X, int Y) {
if (X == Y) {
return N;
}
for (int i = logN - 1; i >= 0; --i) {
if (depth[parent[i][X]] > depth[Y]) {
X = parent[i][X];
}
}
if (Y == parent[0][X]) {
return N - subtree_size[X];
}
return subtree_size[Y];
}
static void checkFunTour(const std::vector<int>& fun_tour) {
if (static_cast<int>(fun_tour.size()) != N) {
wrongAnswer("Invalid size");
}
std::vector<bool> visited_attractions(N, false);
for (int i = 0; i < N; ++i) {
if (fun_tour[i] < 0 || fun_tour[i] >= N) {
wrongAnswer("Invalid index");
}
if (visited_attractions[fun_tour[i]]) {
wrongAnswer("Repeated index");
}
visited_attractions[fun_tour[i]] = true;
}
int last_travel_time = getDistance(fun_tour[0], fun_tour[1]);
for (int i = 2; i < N; ++i) {
int travel_time = getDistance(fun_tour[i - 1], fun_tour[i]);
if (travel_time > last_travel_time) {
wrongAnswer("Tour is not fun");
}
last_travel_time = travel_time;
}
}
} // namespace tree_helper
static int N, Q;
int hoursRequired(int X, int Y) {
if (--Q < 0) {
wrongAnswer("Too many queries");
}
if (X < 0 || X >= N || Y < 0 || Y >= N) {
wrongAnswer("Invalid index");
}
return tree_helper::getDistance(X, Y);
}
int attractionsBehind(int X, int Y) {
if (--Q < 0) {
wrongAnswer("Too many queries");
}
if (X < 0 || X >= N || Y < 0 || Y >= N) {
wrongAnswer("Invalid index");
}
return tree_helper::attractionsBehind(X, Y);
}
int main() {
assert(2 == scanf("%d %d", &N, &Q));
std::vector<std::vector<int>> adj_list(N);
for (int i = 0; i < N - 1; ++i) {
int A, B;
assert(2 == scanf("%d %d", &A, &B));
adj_list[A].push_back(B);
adj_list[B].push_back(A);
}
tree_helper::initializeTree(adj_list);
std::vector<int> fun_tour = createFunTour(N, Q);
tree_helper::checkFunTour(fun_tour);
for (int i = 0; i < N; ++i) {
printf("%d%c", fun_tour[i], " \n"[i == N - 1]);
}
return 0;
}
//*/
# | 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... |