template <classT> inlinevoidread(T &x) { x = 0; int f = 0; char ch = getchar(); while (!isdigit(ch)) { f |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } x = f ? -x : x; return ; }
typedefunsignedlonglong uLL; typedeflonglong LL;
std::vector<int> g[200010]; int a[200010], f[200010], b[200010]; int n, l;
voiddfs(int now, int pa){ int p, r; bool flag = false; if (a[now] > b[l]) { b[++l] = a[now]; f[now] = l; } else { flag = true; p = std::lower_bound(b + 1, b + l + 1, a[now]) - b; r = b[p]; b[p] = a[now]; f[now] = std::max(f[pa], p); } for (auto i : g[now]) { if (i != pa) { dfs(i, now); } } if (flag) b[p] = r; else b[l--] = 0; }
intmain(){ read(n); for (int i = 1; i <= n; ++i) read(a[i]); for (int i = 1, u, v; i < n; ++i) { read(u), read(v); g[u].push_back(v), g[v].push_back(u); } dfs(1, 0); for (int i = 1; i <= n; ++i) printf("%d\n", f[i]); return0; }