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++ (Clang 10.0.0) (judge verdict: Time Limit Exceeded)

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;

const int MAX_V = 200001;

struct edge {
  int to, cost;
};
typedef pair<int, int> P; // firstは最短距離, secondは頂点の番号

int V;
vector<int> a;

vector<edge> G[MAX_V];
int d[MAX_V];
int prev_path[MAX_V]; // prev_path[i]の流入元の点

// 負辺のない単一始点全点間最短路を求めるアルゴリズム。
// 蟻本参照
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  fill(prev_path, prev_path + V, -1);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) {
      continue;
    }

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        prev_path[e.to] = v;
      }
    }
  }
}

// 経路復元
vector<int> get_path_vals(int e) {
  vector<int> path;
  for (; e != -1; e = prev_path[e]) {
    path.push_back(a[e]);
  }
  reverse(path.begin(), path.end());
  return path;
}

int dp[MAX_V];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> V;
  a.resize(V);
  for (int i = 0; i < V; i++) {
    int tmp;
    cin >> tmp;
    a[i] = tmp;
  }
  for (int i = 0; i < V - 1; i++) {
    int ui, vi;
    cin >> ui >> vi;
    ui--;
    vi--;
    G[ui].push_back(edge{vi, 1});
    G[vi].push_back(edge{ui, 1});
  }

  dijkstra(0);
  for (int i = 0; i < V; i++) {
    vector<int> vals = get_path_vals(i);

    ll len = vals.size();
    fill(dp, dp + MAX_V, INF);
    for (int i = 0; i < len; i++) {
      *lower_bound(dp, dp + MAX_V, vals[i]) = vals[i];
    }

    cout << lower_bound(dp, dp + MAX_V, INF) - dp << endl;
  }
  return 0;
}