Buggy python submission (verdict: Time Limit Exceeded):
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
sys.setrecursionlimit(10**6)
N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)

import bisect
def dfs(x,DP):
    #print(x,DP)
    pos=bisect.bisect_left(DP,A[x])
    #print(pos)
    if pos<len(DP):
        DP=list(DP)
        DP[pos]=A[x]
        DP=tuple(DP)
    else:
        DP+=(A[x],)
    #print(DP)

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to,DP)

dfs(1,tuple())

print(*ANS[1:])
    


Fixed submission:
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**6)

N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)
DP=[]

def dfs(x):
    pos=bisect.bisect_left(DP,A[x])
    
    if pos<len(DP):
        changepos=pos
        old=DP[pos]
        
        DP[pos]=A[x]
    else:
        changepos=pos
        old=-1
        
        DP.append(A[x])

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to)

    if old==-1:
        DP.pop()
    else:
        DP[changepos]=old

dfs(1)

print(*ANS[1:])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
sys.setrecursionlimit(10**6)
N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)

import bisect
def dfs(x,DP):
    #print(x,DP)
    pos=bisect.bisect_left(DP,A[x])
    #print(pos)
    if pos<len(DP):
        DP=list(DP)
        DP[pos]=A[x]
        DP=tuple(DP)
    else:
        DP+=(A[x],)
    #print(DP)

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to,DP)

dfs(1,tuple())

print(*ANS[1:])
    


Fixed submission:
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**6)

N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)
DP=[]

def dfs(x):
    pos=bisect.bisect_left(DP,A[x])
    
    if pos<len(DP):
        changepos=pos
        old=DP[pos]
        
        DP[pos]=A[x]
    else:
        changepos=pos
        old=-1
        
        DP.append(A[x])

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to)

    if old==-1:
        DP.pop()
    else:
        DP[changepos]=old

dfs(1)

print(*ANS[1:])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
sys.setrecursionlimit(10**6)
N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)

import bisect
def dfs(x,DP):
    #print(x,DP)
    DP=list(DP)
    pos=bisect.bisect_left(DP,A[x])
    #print(pos)
    if pos<len(DP):
        DP[pos]=A[x]
    else:
        DP.append(A[x])
    #print(DP)

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to,tuple(DP))

dfs(1,tuple())

print(*ANS[1:])

Fixed submission:
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**6)

N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)
DP=[]

def dfs(x):
    pos=bisect.bisect_left(DP,A[x])
    
    if pos<len(DP):
        changepos=pos
        old=DP[pos]
        
        DP[pos]=A[x]
    else:
        changepos=pos
        old=-1
        
        DP.append(A[x])

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to)

    if old==-1:
        DP.pop()
    else:
        DP[changepos]=old

dfs(1)

print(*ANS[1:])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
sys.setrecursionlimit(10**6)
N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)

import bisect
def dfs(x,DP):
    #print(x,DP)
    DP=list(DP)
    pos=bisect.bisect_left(DP,A[x])
    #print(pos)
    if pos<len(DP):
        DP[pos]=A[x]
    else:
        DP.append(A[x])
    #print(DP)

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to,tuple(DP))

dfs(1,tuple())

print(*ANS[1:])
    


Fixed submission:
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**6)

N=int(input())
A=[0]+list(map(int,input().split()))

E=[[] for i in range(N+1)]
for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

USE=[0]*(N+1)
USE[1]=1
ANS=[0]*(N+1)
DP=[]

def dfs(x):
    pos=bisect.bisect_left(DP,A[x])
    
    if pos<len(DP):
        changepos=pos
        old=DP[pos]
        
        DP[pos]=A[x]
    else:
        changepos=pos
        old=-1
        
        DP.append(A[x])

    ANS[x]=len(DP)

    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            dfs(to)

    if old==-1:
        DP.pop()
    else:
        DP[changepos]=old

dfs(1)

print(*ANS[1:])