Problem p02834 (python)

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

n,u,v = map(int, input().split())
adjacent_list = [[] for i in range(n+1)]
aoki = [0 for i in range(n+1)]
takahashi = [0 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)

def dfs(node, count,li):
    global node_par
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            node_par[i] = node
            dfs(i, count + 1,li)
        
node_par = [0 for i in range(n+1)]

finished = set()
dfs(u,0,takahashi)

#辿って親保存しとく
finished = set()
dfs(v,0,aoki)

#さかのぼれるとこまで行く,startがさかのぼれる最大のノーど
par = u
start = u
while takahashi[par] < aoki[par]:
    start = par
    par = node_par[start]

ans_height = [0 for i in range(n+1)]
finished = set([node_par[start]])
dfs(start,0,ans_height)
print(max(ans_height) + aoki[start] - 1)

Buggy submission (verdict: Runtime Error):
n,u,v = map(int, input().split())
adjacent_list = [[]for i in range(n+1)]
aoki = [-1 for i in range(n+1)]
takahashi = [-1 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)

def dfs(node, count,li):
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            dfs(i, count + 1,li)

def search_path(node,t):
    if node == t:
        return
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            pre_node[i] = node
            search_path(i, t)

finished = set()
dfs(v,0,aoki)
finished = set()
path = []
pre_node = [0 for i in range(n+1)]
#最短炉を出す
search_path(u,v)
while v != u:
    path = [v] + path
    v = pre_node[v]
start = u
for i in range(len(path)-1):
    if aoki[path[i+1]] <= i+1:
        start = path[i]
        break
finished = set()
ans_path = [0 for i in range(n+1)]
dfs(start,0,ans_path)
ans = 0
for i in range(1,n+1):
    if aoki[i] > ans_path[i]:
        ans = max(ans, ans_path[i])
print(ans + aoki[start] - 1)

Buggy submission (verdict: Runtime Error):
n,u,v = map(int, input().split())
adjacent_list = [[]for i in range(n+1)]
aoki = [-1 for i in range(n+1)]
takahashi = [-1 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)

def dfs(node, count,li):
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            dfs(i, count + 1,li)

def search_path(node,t):
    if node == t:
        return
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            pre_node[i] = node
            search_path(i, t)

finished = set()
dfs(v,0,aoki)
finished = set()
path = []
pre_node = [0 for i in range(n+1)]
#最短炉を出す
search_path(u,v)
while v != u:
    path = [v] + path
    v = pre_node[v]
start = u
for i in range(len(path)-1):
    if aoki[path[i+1]] <= i+1:
        start = path[i]
        break
finished = set(path)
ans = [0 for i in range(n+1)]
dfs(start,0,ans)
print(max(ans) + aoki[start] - 1)

Buggy submission (verdict: Runtime Error):
n,u,v = map(int, input().split())
adjacent_list = [[]for i in range(n+1)]
aoki = [-1 for i in range(n+1)]
takahashi = [-1 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)
finished = set()

def dfs(node, count,li):
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            dfs(i, count + 1,li)

def search_path(node,t):
    if node == t:
        return
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            pre_node[i] = node
            search_path(i, t)
finished = set()
dfs(v,0,aoki)
finished = set()
path = []
node = v
pre_node = [0 for i in range(n+1)]
search_path(u,v)
while node != u:
    path = [node] + path
    node = pre_node[node]
start = 0
for i in range(len(path)-1):
    if aoki[path[i+1]] <= i+1:
        start = path[i]
        break
finished = set(path)
ans = [0 for i in range(n+1)]
dfs(start,0,ans)
print(max(0, max(ans) + aoki[start] - 1))

Buggy submission (verdict: Runtime Error):
n,u,v = map(int, input().split())
adjacent_list = [[]for i in range(n+1)]
aoki = [-1 for i in range(n+1)]
takahashi = [-1 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)
finished = set()

def dfs(node, count,li):
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            dfs(i, count + 1,li)

def search_path(node,t):
    if node == t:
        return
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            pre_node[i] = node
            search_path(i, t)
finished = set()
dfs(v,0,aoki)
finished = set()
path = []
node = v
pre_node = [0 for i in range(n+1)]
search_path(u,v)
while node != u:
    path = [node] + path
    node = pre_node[node]
start = 0
for i in range(len(path)-1):
    if aoki[path[i+1]] <= i+1:
        start = path[i]
        break
finished = set(path)
ans = [0 for i in range(n+1)]
dfs(start,0,ans)
print(ans)
print(min(0, max(ans) + aoki[start] - 1))