Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
G=[[] for _ in range(N)]
ab = [tuple(map(int,input().split())) for _ in range(N-1)]
for a,b in ab:
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def bfs():
    from collections import deque
    dp = [[] for _ in range(N)]
    dp[0] = [A[0]]
    seen = [0]*N
    todo = deque()
    seen[0]=1
    todo.append(0)
    while todo:
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          idx = bisect_left(dp[a],A[b])
          if idx == len(dp[a]): dp[b] = dp[a]+[A[b]]
          else: dp[b] = [dp[a][i] if i != idx else A[b] for i in range(len(dp[a]))]
          seen[b] = 1
          todo.append(b)
    return dp
  
dp = bfs()
  
print(*[len(dp[i]) for i in range(N)],sep="\n")

Fixed submission:
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
INF=10**20
G=[[] for _ in range(N)]
ab = [tuple(map(int,input().split())) for _ in range(N-1)]
for a,b in ab:
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def dfs(a0):
    seen =[0]*len(G)
    todo = [~a0, a0]
    while todo:
        a = todo.pop()
        if a >= 0:
            seen[a] = 1
            idx = bisect_left(lis,A[a])
            his[a] = (idx,lis[idx])
            lis[idx] = A[a]
            dp[a] = bisect_left(lis,INF)
            for b in G[a]:
                if seen[b]: continue
                todo.append(~b)
                todo.append(b)
        else:
          idx,val = his[~a]
          lis[idx] = val
    return
  
lis = [INF]*N
dp = [0]*N
his = {}
dfs(0)
print(*dp,sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
G=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def bfs(s):
    from collections import deque
    seen = [0]*N
    d = [0]*N
    prev = [-1]*N
    todo = deque()
    seen[s]=1
    todo.append(s)
    while todo:
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          d[b] += d[a] + 1
          prev[b] = a
    return d, prev
  
def argsort(arr, reverse=False, shift=20):
    tmp = sorted([arr[i]<<shift | i for i in range(len(arr))],reverse=reverse)
    mask = (1<<shift) - 1
    return [tmp[i] & mask for i in range(len(arr))]
  
d,prev = bfs(0)
order = argsort(d)
dp = [[] for _ in range(N)]
dp[0] = [A[0]]
for v in order:
  if v == 0: continue
  p = prev[v]
  idx = bisect_left(dp[p],A[v])
  if idx == len(dp[p]): dp[v] = dp[p]+[A[v]]
  else: dp[v] = [dp[p][i] if i != idx else A[v] for i in range(len(dp[p]))]
  
print(*[len(dp[i]) for i in range(N)],sep="\n")

Fixed submission:
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
INF=10**20
G=[[] for _ in range(N)]
ab = [tuple(map(int,input().split())) for _ in range(N-1)]
for a,b in ab:
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def dfs(a0):
    seen =[0]*len(G)
    todo = [~a0, a0]
    while todo:
        a = todo.pop()
        if a >= 0:
            seen[a] = 1
            idx = bisect_left(lis,A[a])
            his[a] = (idx,lis[idx])
            lis[idx] = A[a]
            dp[a] = bisect_left(lis,INF)
            for b in G[a]:
                if seen[b]: continue
                todo.append(~b)
                todo.append(b)
        else:
          idx,val = his[~a]
          lis[idx] = val
    return
  
lis = [INF]*N
dp = [0]*N
his = {}
dfs(0)
print(*dp,sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
G=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def bfs(s):
    from collections import deque
    seen = [0]*N
    d = [0]*N
    prev = [-1]*N
    todo = deque()
    seen[s]=1
    todo.append(s)
    while todo:
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          d[b] += d[a] + 1
          prev[b] = a
    return d, prev
  
def argsort(arr, reverse=False, shift=20):
    tmp = sorted([arr[i]<<shift | i for i in range(len(arr))],reverse=reverse)
    mask = (1<<shift) - 1
    return [tmp[i] & mask for i in range(len(arr))]
  
d,prev = bfs(0)
order = argsort(d)
dp = [[] for _ in range(N)]
dp[0] = [A[0]]
for v in order:
  if v == 0: continue
  p = prev[v]
  idx = bisect_left(dp[p],A[v])
  if idx == len(dp[p]): dp[v] = dp[p]+[A[v]]
  else: dp[v] = [dp[p][i] if i != idx else A[v] for i in range(len(dp[p]))]
  
print(*[len(dp[i]) for i in range(N)],sep="\n")

Fixed submission:
from bisect import bisect_left

N=int(input())
*A,=map(int,input().split())
INF=10**20
G=[[] for _ in range(N)]
ab = [tuple(map(int,input().split())) for _ in range(N-1)]
for a,b in ab:
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def dfs(a0):
    seen =[0]*len(G)
    todo = [~a0, a0]
    while todo:
        a = todo.pop()
        if a >= 0:
            seen[a] = 1
            idx = bisect_left(lis,A[a])
            his[a] = (idx,lis[idx])
            lis[idx] = A[a]
            dp[a] = bisect_left(lis,INF)
            for b in G[a]:
                if seen[b]: continue
                todo.append(~b)
                todo.append(b)
        else:
          idx,val = his[~a]
          lis[idx] = val
    return
  
lis = [INF]*N
dp = [0]*N
his = {}
dfs(0)
print(*dp,sep="\n")