Buggy python submission (verdict: Runtime Error):
import bisect

N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, prev_dp):
    dp = [*prev_dp]
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
    else:
        pos = bisect.bisect_left(dp, a[v])
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)


dfs(0, [])


for an in ans:
    print(an)


Fixed submission:
import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)

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

N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    to[u - 1].append(v - 1)
    to[v - 1].append(u - 1)


dp = [None] * N

dp[0] = [a[0]]
Q = [0]

while Q:
    u = Q.pop()
    for v in to[u]:
        if dp[v] is not None:
            continue
        dp[v] = [*dp[u]]
        if a[v] > dp[u][-1]:
            dp[v].append(a[v])
        else:
            dp[v][bisect.bisect_left(dp[v], a[v])] = a[v]
        Q.append(v)

for d in dp:
    print(len(d))


Fixed submission:
import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)

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

N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    to[u - 1].append(v - 1)
    to[v - 1].append(u - 1)


dp = [None] * N

dp[0] = [a[0]]
Q = [0]

while Q:
    u = Q.pop()
    for v in to[u]:
        if dp[v] is not None:
            continue
        dp[v] = [*dp[u]]
        if a[v] > dp[u][-1]:
            dp[v].append(a[v])
        else:
            dp[v][bisect.bisect_left(dp[v], a[v])] = a[v]
        Q.append(v)

for d in dp:
    print(len(d))


Fixed submission:
import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)

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

N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    to[u - 1].append(v - 1)
    to[v - 1].append(u - 1)


dp = [None] * N

dp[0] = [a[0]]
Q = [0]

while Q:
    u = Q.pop()
    for v in to[u]:
        if dp[v] is not None:
            continue
        dp[v] = [*dp[u]]
        if a[v] > dp[u][-1]:
            dp[v].append(a[v])
        else:
            dp[v][bisect.bisect_left(dp[v], a[v])] = a[v]
        Q.append(v)

for d in dp:
    print(len(d))


Fixed submission:
import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)

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


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    to[u - 1].append(v - 1)
    to[v - 1].append(u - 1)


dp = [None for _ in range(N)]


dp[0] = [a[0]]
Q = [0]

while Q:
    u = Q.pop()
    for v in to[u]:
        if dp[v] is not None:
            continue
        dp[v] = dp[u][::]
        if a[v] > dp[u][-1]:
            dp[v].append(a[v])
        else:
            dp[v][bisect.bisect_left(dp[v], a[v])] = a[v]
        Q.append(v)

for d in dp:
    print(len(d))


Fixed submission:
import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)