結果
| 問題 |
No.2634 Tree Distance 3
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-16 22:41:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 479 ms / 3,000 ms |
| コード長 | 3,957 bytes |
| コンパイル時間 | 1,569 ms |
| コンパイル使用メモリ | 126,972 KB |
| 実行使用メモリ | 76,720 KB |
| 最終ジャッジ日時 | 2024-09-28 21:17:36 |
| 合計ジャッジ時間 | 24,632 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 69 |
ソースコード
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
int N;
vector<int> A, B;
vector<vector<int>> G;
namespace lca {
vector<int> dep;
// vector<Int> cs;
vector<int> poss, seq;
vector<int> mn_;
vector<int *> mn;
void dfs(int u, int pi) {
poss[u] = seq.size();
seq.push_back(u);
for (const int i : G[u]) if (pi != i) {
const int v = A[i] ^ B[i] ^ u;
dep[v] = dep[u] + 1;
// cs[v] = cs[u] + C[i];
dfs(v, i);
seq.push_back(u);
}
}
inline int shallower(int u, int v) {
return (dep[u] <= dep[v]) ? u : v;
}
void build() {
poss.resize(N);
dep.assign(N, 0);
// cs.assign(N, 0);
seq.clear();
dfs(0, -1);
const int E = (31 - __builtin_clz(2 * N - 1)) + 1;
mn_.resize(E * (2 * N - 1));
mn.resize(E);
for (int e = 0; e < E; ++e) {
mn[e] = mn_.data() + e * (2 * N - 1);
}
for (int j = 0; j < 2 * N - 1; ++j) {
mn[0][j] = seq[j];
}
for (int e = 0; e < E - 1; ++e) {
for (int i = 0; i + (1 << (e + 1)) <= 2 * N - 1; ++i) {
mn[e + 1][i] = shallower(mn[e][i], mn[e][i + (1 << e)]);
}
}
}
int lca(int u, int v) {
int j0 = poss[u], j1 = poss[v];
if (j0 > j1) swap(j0, j1);
++j1;
const int e = 31 - __builtin_clz(j1 - j0);
return shallower(mn[e][j0], mn[e][j1 - (1 << e)]);
}
inline int dist(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
// inline Int cost(int u, int v) {
// return cs[u] + cs[v] - 2 * cs[lca(u, v)];
// }
} // lca
////////////////////////////////////////////////////////////////////////////////
vector<int> W;
int main() {
for (; ~scanf("%d", &N); ) {
W.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%d", &W[u]);
}
A.resize(N - 1);
B.resize(N - 1);
for (int i = 0; i < N - 1; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
G.assign(N, {});
for (int i = 0; i < N - 1; ++i) {
G[A[i]].push_back(i);
G[B[i]].push_back(i);
}
lca::build();
map<int, vector<int>> uss;
for (int u = 0; u < N; ++u) {
uss[-W[u]].push_back(u);
}
vector<int> ans(N, 0);
{
int d = 0;
int a = -1, b = -1;
for (const auto &kv : uss) {
for (const int u : kv.second) {
if (!~a) {
a = b = u;
} else {
const int dau = lca::dist(a, u);
const int dbu = lca::dist(b, u);
if (dau >= dbu) {
if (chmax(d, dau)) b = u;
} else {
if (chmax(d, dbu)) a = u;
}
}
}
for (const int u : kv.second) {
ans[u] = max(lca::dist(a, u), lca::dist(b, u));
}
}
}
for (int u = 0; u < N; ++u) {
if (u) printf(" ");
printf("%d", ans[u]);
}
puts("");
}
return 0;
}