#include #include #define SELECTER(_1,_2,_3,SELECT,...) SELECT #define rep1(i,n) for(int i=0;i<(int)n;++i) #define rep2(i,a,n) for(int i=(int)a;i<(int)n;++i) #define rep(...) SELECTER(__VA_ARGS__,rep2,rep1)(__VA_ARGS__) #define RSELECTER(_1, _2, _3, RSELECT, ...) RSELECT #define rrep1(i,n) for(int i=(int)(n)-1;i>=0;--i) #define rrep2(i,a,n) for(int i=(int)(n)-1;i>=(int)a;--i) #define rrep(...) RSELECTER(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define fi first #define se second #define PrintR LogOutput #ifdef _DEBUG #define Log(...) LogOutput(__VA_ARGS__) #else #define Log(...) #endif #define M_PI 3.14159265358979323846 using namespace std; using namespace atcoder; using ll=long long; using ld=long double; using pii=pair; using pll=pair; using pdd=pair; using tp=tuple; using tpll=tuple; using veci=vector; using vecpii=vector>; using vecll=vector; using vecpll=vector>; using vecpdd=vector>; using vecs=vector; using vecb=vector; using vecd=vector; using vectp=vector; using vectpll=vector; using mint=modint998244353; using mint10=modint1000000007; template istream& operator>>(istream& in, pair& a){return in >> a.first >> a.second;} template ostream& operator<<(ostream& out, const pair& a){return out << a.first << ' ' << a.second;} ostream& operator<<(ostream& out, const mint& a){return out << a.val();} ostream& operator<<(ostream& out, const mint10& a){return out << a.val();} ostream& operator<<(ostream& out, const modint& a){return out << a.val();} template ostream& operator<<(ostream& out, const vector& d){for(int i = 0 ; i < d.size() ; ++i) out << d[i] << (i == d.size() - 1 ? "" : " "); return out;} template pair operator+(const pair& a, const pair& b){return {a.fi + b.fi, a.se + b.se};} template pair operator-(const pair& a, const pair& b){return {a.fi - b.fi, a.se - b.se};} template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(a>b) {a=b;return true;} return false;} bool Judge(int i, int j, int h, int w){return i < 0 || j < 0 || i >= h || j >= w;} bool PrintA(int i){cout<<(i ? "Yes" : "No")<::max() >> 2; constexpr int inf=numeric_limits::max() >> 1; constexpr ll MOD=998244353; const int vi[] = {0, 1, 0, -1}, vj[] = {1, 0, -1, 0}; template void LogOutput(Args&&... args){ stringstream ss; ((ss << args << ' '), ...); cout << ss.str().substr(0, ss.str().length() - 1) << endl; } template void LogOutput(vector>& data){for(auto d : data) LogOutput(d);} int op(int a, int b){return max(a, b);} int e(){return -inf;} int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n;cin>>n; veci a(n); rep(i, n) cin>>a[i]; vector g(n); rep(i, n - 1){ int b, c;cin>>b>>c; b--, c--; g[b].push_back(c); g[c].push_back(b); } veci dp(n), used(n); auto dfs = [&](auto f, int v, int p) -> void{ dp[v] = 1; for(int nv : g[v]) if(nv != p && used[nv] == 0){ f(f, nv, v); dp[v] += dp[nv]; } }; auto dfs2 = [&](auto f, int v, int p, int c) -> int{ int u = 1; for(int nv : g[v]) if(used[nv] == 0){ if(nv == p) u &= c >= 2 * (c - dp[v]); else u &= c >= 2 * dp[nv]; } if(u) return v; for(int nv : g[v]) if(nv != p && used[nv] == 0){ int r = f(f, nv, v, c); if(r != -1) return r; } return -1; }; veci pr(n), sz(n); veci ls; vector g2(n); auto rec = [&](auto f, int v, int p) -> int{ dfs(dfs, v, -1); int cent = dfs2(dfs2, v, -1, dp[v]); pr[cent] = p; sz[cent] = dp[v]; used[cent] = 1; for(int nv : g[cent]) if(used[nv] == 0){ g2[cent].push_back(f(f, nv, cent)); } ls.push_back(cent); return cent; }; rec(rec, 0, -1); veci dist(n); vector b(n); vecpii lr(n); veci ans(n); veci idx(n); auto rec2 = [&](auto f, int v) -> void{ map mp; int cnt = 0; for(int nv : g2[v]){ int l = cnt, r = cnt + b[nv].size(); while(b[nv].size()){ mp[a[b[nv].back()]].push_back(b[nv].back()); b[v].push_back(b[nv].back()); lr[b[nv].back()] = {l, r}; idx[b[nv].back()] = cnt++; b[nv].pop_back(); } } lr[v] = {cnt, cnt + 1}; idx[v] = cnt++; b[v].push_back(v); mp[a[v]].push_back(v); segtree sg(sz[v]); for(auto it = mp.rbegin(); it != mp.rend(); ++it){ for(int nv : it->se) sg.set(idx[nv], dist[nv]); for(int nv : it->se){ chmax(ans[nv], dist[nv] + max(sg.prod(0, lr[nv].fi), sg.prod(lr[nv].se, sz[v]))); dist[nv]++; } } }; rep(i, ls.size()) rec2(rec2, ls[i]); PrintR(ans); }