Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

struct edge {
    ll to, cost;
};

// vector<edge> G[1000000];
VV<edge> G(1000000);
V<ll> d(1000000);

void addedge(VV<edge>& G, int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

//根付き木を作る。parentVectorに親nodeの番号が入る。
//parentVector[root] = -1
//treeRankVectorにはnodeのrankが入る。rootのrankは0
//subTreeSizeVectorには、そのnodeを根とする部分木のサイズが入る。
vector<ll> par;
vector<ll> treeRank;
vector<ll> subTreeSize;
void makeRootedTree(vector<ll>& parentVector, vector<ll>& treeRankVector, vector<ll>& subTreeSizeVector, ll vecSize, ll root) {
    parentVector = vector<ll>(vecSize);
    treeRankVector = vector<ll>(vecSize);
    assert(root < (ll)parentVector.size());
    parentVector[root] = -1;
    treeRankVector[root] = 0;
    queue<ll> que;
    que.push(root);
    while (!que.empty()) {
        ll node = que.front();
        que.pop();
        REP (i, 0, G[node].size()) {
            edge e = G[node][i];
            if (e.to != parentVector[node]) {
                parentVector[e.to] = node;
                treeRankVector[e.to] = treeRankVector[node] + 1;
                que.push(e.to);
            }
        }
    }

    //ここからはsubTreeSizeVectorの計算
    vector<ll> used(parentVector.size());
    subTreeSizeVector = vector<ll>(vecSize, 0);
    fill(used.begin(), used.end(), 0);
    REP(i, 0, parentVector.size()) {
        if (parentVector[i] != -1 && G[i].size() == 1) {
            que.push(i);
            used[i] = 1;
        }
    }
    while (!que.empty()) {
        ll node = que.front();
        que.pop();
        //rootも含めて子だけのときに何かやりたいときはここに書く
        subTreeSizeVector[node]++;
        if (parentVector[node] == -1) {
            continue;
        }
        ll parent = parentVector[node];
        used[parent]++;
        subTreeSizeVector[parent] += subTreeSizeVector[node];
        //ここに書く
        if ((parentVector[parent] == -1 && used[parent] == (ll)G[parent].size()) ||
            (parentVector[parent] != -1 && used[parent] == (ll)G[parent].size() - 1)) {
            que.push(parent);
        }
    }
}

ll N;
ll a[200010];
vll dpvec[200010];
ll dp[200010];
ll ans[200010];

void dfs(ll node) {
    ll pt = (lower_bound(dp, dp + N, a[node]) - dp);
    dp[pt] = a[node];
    dpvec[pt].push_back(a[node]);
    ans[node] = lower_bound(dp, dp + N, inf) - dp;
    REP(i, 0, G[node].size()) {
        edge e = G[node][i];
        if (e.to != par[node]) {
            dfs(e.to);
        }
    }
    dpvec[pt].pop_back();
    dp[pt] = dpvec[pt][dpvec[pt].size() - 1];    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    REP(i, 0, N) {
        cin >> a[i];
    }
    REP(i, 0, N - 1) {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        addedge(G, u, v, 1);
        addedge(G, v, u, 1);
    }
    makeRootedTree(par, treeRank, subTreeSize, N, 0);
    REP(i, 0, N) {
        dpvec[i].push_back(inf);
    }
    fill(dp, dp + N + 10, inf);
    // cout << "aaa" << endl;
    dfs(0);
    REP(i, 0, N) {
        cout << ans[i] << endl;
    }
}