Problem p02698 (python)

Fixed submission:
import bisect
import sys
sys.setrecursionlimit(10**7)

n = int(input())
a = [0]+list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)

m = 10**18
dp = [m]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(x):
    ind = bisect.bisect_left(dp,a[x])
    changes.append((ind,dp[ind]))
    dp[ind] = a[x]
    ans[x] = bisect.bisect_left(dp,m)
    
    for i in t[x]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
for i in ans[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = [0]+list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)

m = 10**18
dp = [m]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(x):
    ind = bisect.bisect_left(dp,a[x])
    changes.append((ind,dp[ind]))
    dp[ind] = a[x]
    ans[x] = bisect.bisect_left(dp,m)
    
    for i in t[x]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
for i in ans[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)

INF = float('inf')
dp = [INF]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(x):
    ind = bisect.bisect_left(dp,a[x-1])
    changes.append((ind,dp[ind]))
    dp[ind] = a[x-1]
    ans[x] = bisect.bisect_left(dp,INF)
    
    for i in t[x]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
for i in ans[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)

INF = float('inf')
dp = [INF]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(x):
    ind = bisect.bisect_left(dp,a[x-1])
    changes.append([ind,dp[ind]])
    dp[ind] = a[x-1]
    ans[x] = bisect.bisect_left(dp,INF)
    
    for i in t[x]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
for i in a[1:]:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)

INF = float('inf')
dp = [INF]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(v):
    ind = bisect.bisect_left(dp,a[v-1])
    changes.append([ind,dp[ind]])
    dp[ind] = a[v-1]
    ans[v] = bisect.bisect_left(dp,INF)
    
    for i in t[v]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
print('\n'.join(map(str,ans[1:])))

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)
 
INF = float('inf')
dp = [INF]*(n+1)
ans = [1]*(n+1)
checked = [False]*(n+1)
checked[1] = True
changes = []

def search(v):
    ind = bisect.bisect_left(dp,a[v-1])
    changes.append([ind,dp[ind]])
    dp[ind] = a[v-1]
    ans[v] = bisect.bisect_left(dp,INF)
    
    for i in t[v]:
        if not checked[i]:
            checked[i] = True
            search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
print('\n'.join(map(str,ans[1:])))

Buggy submission (verdict: Runtime Error):
import bisect
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)
 
INF = float('inf')
dp = [INF]*(n+1)
ans = [1]*(n+1)
checked = [1]
changes = []

def search(v):
    ind = bisect.bisect_left(dp,a[v-1])
    changes.append([ind,dp[ind]])
    dp[ind] = a[v-1]
    ans[v] = bisect.bisect_left(dp,INF)
    
    for i in t[v]:
        if i in checked:
            continue
        checked.append(i)
        search(i)
    
    b,c = changes.pop()
    dp[b] = c
    
search(1)
print('\n'.join(map(str,ans[1:])))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
t = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v = map(int,input().split())
    t[u].append(v)
    t[v].append(u)
    
stack = [1]
dp = [[] for _ in range(n+1)]
dp[1] = [a[0]]
ans = [1]*(n+1)
checked = []
while stack:
    b = stack.pop()
    if b in checked:
        continue
    checked.append(b)
    e = dp[b]
    x = len(e)
    for i in t[b]:
        if i in checked:
            continue
        stack.append(i)
        c = a[i-1]
        d = dp[i]
        if e[-1] < c:
            d = e+[c]
            ans[i] = x+1
        else:
            d = e.copy()
            ans[i] = x
        for j in range(x-1,-1,-1):
            if j == 0 and d[j] > c:
                d[j] = c
            elif d[j] > c and d[j-1] < c:
                d[j] = c
                
for i in ans[1:]:
    print(i)