Buggy python submission (verdict: Time Limit Exceeded):
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N)]
for u, v in uv:
    link[u - 1].append(v - 1)
    link[v - 1].append(u - 1)


def dfs(l, visit, v, dp):
    visit[v] = True
    # LIS
    if dp[-1] < A[v]:
        dp = np.append(dp, A[v])
    else:
        i = np.searchsorted(dp, A[v])
        dp[i] = A[v]
    l[v] = len(dp)

    for x in link[v]:
        if not visit[x]:
            dfs(l, visit, x, dp.copy())


def main():
    visit = [False] * N
    l = [None] * N
    dfs(l, visit, 0, np.array([A[0]]))
    for ans in l:
        print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N + 1)]
for u, v in uv:
    link[u].append(v)
    link[v].append(u)


def dfs(l, dp, pre, v):
    # LIS
    idx = np.searchsorted(dp, A[v - 1])
    l[v] = max(idx, l[pre])
    bef = dp[idx]
    dp[idx] = A[v - 1]
    
    for x in link[v]:
        if x != pre:
            dfs(l, dp, v, x)

    dp[idx] = bef


def main():
    l = [0] * (N + 1)
    dp = np.full(N + 1, INF)
    dp[0] = 0
    dfs(l, dp, 0, 1)
    for ans in l[1:]:
        print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]


def dfs(l, visit, link, v, dp):
    visit[v] = True
    # LIS
    if dp[-1] < A[v]:
        dp = np.append(dp, A[v])
    else:
        i = np.searchsorted(dp, A[v])
        dp[i] = A[v]
    l[v] = len(dp)

    for x in link[v]:
        if not visit[x]:
            dfs(l, visit, link, x, dp.copy())


def main():
    link = [[] for _ in range(N)]
    for u, v in uv:
        link[u - 1].append(v - 1)
        link[v - 1].append(u - 1)
    
    visit = [False] * N
    l = [None] * N
    dfs(l, visit, link, 0, np.array([A[0]]))
    for ans in l:
        print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N + 1)]
for u, v in uv:
    link[u].append(v)
    link[v].append(u)


def dfs(l, dp, pre, v):
    # LIS
    idx = np.searchsorted(dp, A[v - 1])
    l[v] = max(idx, l[pre])
    bef = dp[idx]
    dp[idx] = A[v - 1]
    
    for x in link[v]:
        if x != pre:
            dfs(l, dp, v, x)

    dp[idx] = bef


def main():
    l = [0] * (N + 1)
    dp = np.full(N + 1, INF)
    dp[0] = 0
    dfs(l, dp, 0, 1)
    for ans in l[1:]:
        print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!python3

import heapq


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]


def solve(link, w):
    l = [None] * N
    visit = [False] * N
    visit_n = 0
    # cost, len, node
    hq = [(0, -1, 0)]
    while hq:
        if visit_n == N:
            break
        c, n, v = heapq.heappop(hq)
        if visit[v] :
            continue
        visit[v] = True
        visit_n += 1
        l[v] = abs(n)
        for x in link[v]:
            if not visit[x]:
                heapq.heappush(hq, (c + 1, n + w[v][x], x))

    return l


def main():
    link = [[] for _ in range(N)]
    w = [[0] * N for _ in range(N)]
    for u, v in uv:
        u -= 1
        v -= 1
        link[u].append(v)
        link[v].append(u)
        if A[u] < A[v]:
            w[u][v] = -1
        if A[v] < A[u]:
            w[v][u] = -1
    
    l = solve(link, w)
    for ans in l:
        print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N + 1)]
for u, v in uv:
    link[u].append(v)
    link[v].append(u)


def dfs(l, dp, pre, v):
    # LIS
    idx = np.searchsorted(dp, A[v - 1])
    l[v] = max(idx, l[pre])
    bef = dp[idx]
    dp[idx] = A[v - 1]
    
    for x in link[v]:
        if x != pre:
            dfs(l, dp, v, x)

    dp[idx] = bef


def main():
    l = [0] * (N + 1)
    dp = np.full(N + 1, INF)
    dp[0] = 0
    dfs(l, dp, 0, 1)
    for ans in l[1:]:
        print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!python3

import heapq


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]


def solve(link, w):
    l = [None] * N
    visit = [False] * N
    visit_n = 0
    # cost, len, node
    hq = [(0, -1, 0)]
    while hq:
        if visit_n == N:
            break
        c, n, v = hq.pop()
        if visit[v] :
            continue
        visit[v] = True
        visit_n += 1
        l[v] = abs(n)
        for x in link[v]:
            if not visit[x]:
                heapq.heappush(hq, (c + 1, n + w[v][x], x))

    return l


def main():
    link = [[] for _ in range(N)]
    w = [[0] * N for _ in range(N)]
    for u, v in uv:
        u -= 1
        v -= 1
        link[u].append(v)
        link[v].append(u)
        if A[u] < A[v]:
            w[u][v] = -1
        if A[v] < A[u]:
            w[v][u] = -1
    
    l = solve(link, w)
    for ans in l:
        print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N + 1)]
for u, v in uv:
    link[u].append(v)
    link[v].append(u)


def dfs(l, dp, pre, v):
    # LIS
    idx = np.searchsorted(dp, A[v - 1])
    l[v] = max(idx, l[pre])
    bef = dp[idx]
    dp[idx] = A[v - 1]
    
    for x in link[v]:
        if x != pre:
            dfs(l, dp, v, x)

    dp[idx] = bef


def main():
    l = [0] * (N + 1)
    dp = np.full(N + 1, INF)
    dp[0] = 0
    dfs(l, dp, 0, 1)
    for ans in l[1:]:
        print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!python3

import heapq


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]


def solve(link, w):
    l = [None] * N
    visit = [False] * N
    # cost, len, node
    hq = [(0, -1, 0)]
    while hq:
        c, n, v = hq.pop()
        if visit[v] :
            continue
        visit[v] = True
        l[v] = abs(n)
        for x in link[v]:
            if not visit[x]:
                heapq.heappush(hq, (c + 1, n + w[v][x], x))

    return l


def main():
    link = [[] for _ in range(N)]
    w = [[0] * N for _ in range(N)]
    for u, v in uv:
        u -= 1
        v -= 1
        link[u].append(v)
        link[v].append(u)
        if A[u] < A[v]:
            w[u][v] = -1
        if A[v] < A[u]:
            w[v][u] = -1
    
    l = solve(link, w)
    for ans in l:
        print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!python3

import sys
sys.setrecursionlimit(10 ** 6)

import numpy as np


INF = 10 ** 10

# input
N = int(input())
A = list(map(int, input().split()))
uv = [list(map(int, input().split())) for _ in range(N - 1)]

# link
link = [[] for _ in range(N + 1)]
for u, v in uv:
    link[u].append(v)
    link[v].append(u)


def dfs(l, dp, pre, v):
    # LIS
    idx = np.searchsorted(dp, A[v - 1])
    l[v] = max(idx, l[pre])
    bef = dp[idx]
    dp[idx] = A[v - 1]
    
    for x in link[v]:
        if x != pre:
            dfs(l, dp, v, x)

    dp[idx] = bef


def main():
    l = [0] * (N + 1)
    dp = np.full(N + 1, INF)
    dp[0] = 0
    dfs(l, dp, 0, 1)
    for ans in l[1:]:
        print(ans)


if __name__ == "__main__":
    main()