#pragma GCC optimize("O2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair; using pll = pair; //#define double ldb template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(const T &X : arr) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(const T &X : vec) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const set &s) { for(const T &x : s) os << x << ' '; return os; } struct fastJump { vector p, jp, dep; fastJump(vector> g, int root = 0) : p(ssize(g)), jp(ssize(g)), dep(ssize(g)) { auto dfs = [&](int v, auto self) -> void { if (dep[p[v]] + dep[jp[jp[p[v]]]] == 2 * dep[jp[p[v]]]) jp[v] = jp[jp[p[v]]]; else jp[v] = p[v]; for(int x : g[v]) { if (x == p[v]) continue; p[x] = v, dep[x] = dep[v] + 1; self(x, self); } }; p[root] = jp[root] = root; dfs(root, dfs); } int jump(int v, int k) { k = min(k, dep[v]); while(k) { if (int d = dep[v] - dep[jp[v]]; d <= k) v = jp[v], k -= d; else v = p[v], k -= 1; } return v; } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); u = jump(u, dep[u] - dep[v]); if (u == v) return u; while(p[u] != p[v]) { if (jp[u] != jp[v]) u = jp[u], v = jp[v]; else u = p[u], v = p[v]; } return p[u]; } int kth(int s, int t, int k) { int m = lca(s, t); if (dep[s] + dep[t] - 2 * dep[m] < k) return -1; else if (dep[s] - dep[m] >= k) return jump(s, k); else return jump(t, dep[s] + dep[t] - 2 * dep[m] - k); } int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n; cin >> n; vector a(n); for(int &x : a) cin >> x; vector> g(n); for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; g[u].emplace_back(v); g[v].emplace_back(u); } fastJump fj(g); vector ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int i, int j) { return a[i] > a[j]; }); int l = ord[0], r = ord[0], d = 0; vector ans(n); for(int i = 1, p = 0; i <= n; i++) { if (i == n or a[ord[i]] != a[ord[i - 1]]) { for(int j = p; j < i; j++) { if (int x = fj.dis(ord[j], l); x > d) r = ord[j], d = x; if (int x = fj.dis(ord[j], r); x > d) l = ord[j], d = x; } for(int j = p; j < i; j++) ans[ord[j]] = max(fj.dis(l, ord[j]), fj.dis(r, ord[j])); p = i; } } cout << ans << '\n'; return 0; }