結果
| 問題 | No.2634 Tree Distance 3 |
| コンテスト | |
| ユーザー |
Yu_212
|
| 提出日時 | 2024-02-18 14:09:28 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,669 ms / 3,000 ms |
| コード長 | 4,168 bytes |
| 記録 | |
| コンパイル時間 | 3,421 ms |
| コンパイル使用メモリ | 264,996 KB |
| 実行使用メモリ | 334,920 KB |
| 最終ジャッジ日時 | 2024-09-29 00:27:10 |
| 合計ジャッジ時間 | 84,365 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 69 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define var auto
using graph = vector<vector<int>>;
template <class F>
void one_third_centroid_decomposition(const graph &g, F f) {
const int n = g.size();
assert(n >= 1);
vector<int> size(n), xy(n), count(n), sort(n);
using buf_node = pair<graph, graph>;
vector<unique_ptr<buf_node>> buf;
const auto get_buf = [&](const int depth) -> buf_node & {
while (depth >= buf.size())
buf.push_back(make_unique<buf_node>(graph(n), graph(n)));
return *buf[depth];
};
const auto dc = [&](const auto &dc, const int n, const int v, const graph &g, const int depth) -> void {
if (n <= 2)
return;
int s = -1;
const auto sz = [&](const auto &sz, const int v, const int p) -> void {
size[v] = 1;
int mx = 0;
for (int e : g[v]) {
if (e != p) {
sz(sz, e, v);
size[v] += size[e];
mx = max(mx, size[e]);
}
}
mx = max(mx, n - size[v]);
if (mx * 2 <= n)
s = v;
};
sz(sz, v, -1);
int maxsz = 0;
for (int e : g[s]) {
if (size[e] > size[s])
size[e] = n - size[s];
count[size[e]]++;
maxsz = max(maxsz, size[e] + 1);
}
for (int i = 1; i < maxsz; i++)
count[i] += count[i - 1];
for (int e : g[s])
sort[--count[size[e]]] = e;
fill(count.begin(), count.begin() + maxsz, 0);
int xs = 0, ys = 0;
for (int i = g[s].size(); i-- > 0;) {
if (xs < ys)
xy[sort[i]] = 0, xs += size[sort[i]];
else
xy[sort[i]] = 1, ys += size[sort[i]];
}
auto &[x, y] = get_buf(depth);
x[s].clear();
y[s].clear();
const auto build = [&](const auto &build, const int v, const int p, auto &t) -> void {
t[v] = g[v];
for (int e : g[v]) {
if (e != p) build(build, e, v, t);
}
};
for (int e : g[s]) {
auto &t = xy[e] ? y : x;
t[s].push_back(e);
build(build, e, s, t);
}
f(s, x, y);
dc(dc, xs + 1, s, x, depth + 1);
dc(dc, ys + 1, s, y, depth + 1);
};
dc(dc, n, 0, g, 0);
}
int main() {
cin.tie(0)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> edges(n);
for (int i = 0; i < n - 1; i ++) {
int u, v;
cin >> u >> v;
u--, v--;
edges[u].push_back(v);
edges[v].push_back(u);
}
vector<int> ans(n);
const auto f = [&](const int s, const auto &x, const auto &y) {
vector<int> table;
auto dfs1 = [&](auto self, int node, int parent, int depth) -> void {
while (table.size() <= depth) table.push_back(0);
table[depth] = min(table[depth], -a[node]);
for (int child : x[node]) {
if (child == parent) continue;
self(self, child, node, depth + 1);
}
};
dfs1(dfs1, s, s, 0);
int m = table.size();
for (int i = m - 2; i >= 0; i--) {
table[i] = min(table[i], table[i + 1]);
}
auto dfs2 = [&](auto self, int node, int parent, int depth) -> void {
int pos = upper_bound(table.begin(), table.end(), -a[node]) - table.begin();
if (pos >= 1) {
ans[node] = max(ans[node], depth + pos - 1);
}
for (int child : y[node]) {
if (child == parent) continue;
self(self, child, node, depth + 1);
}
};
dfs2(dfs2, s, s, 0);
};
one_third_centroid_decomposition(edges, [&](int s, const auto &x, const auto &y) {
f(s, x, y);
f(s, y, x);
});
for (int i = 0; i < n; i++) {
cout << ans[i] << " \n"[i+1==n];
}
}
Yu_212