Buggy python submission (verdict: Time Limit Exceeded):
N,u,v = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    

dicT = {u-1:0}
dicA = {v-1:0}
ans = 0
while len(dicT) > 0 and len(dicA) > 0:
    tempT = {}
    for t in dicT:
        for e in edge[t]:
            if e not in dicA:
                tempT[e] = 0
                
    dicT = tempT
    if not dicT:
        break
    tempA = {}
    for a in dicA:
        for e in edge[a]:
            if e in dicT:
                dicT[e] = 1
            else:
                tempA[e] = 0
    
    dicA = tempA
    
    tempT = {}
    for t in dicT:
        if dicT[t] == 0:
            tempT[t] = 0
            
    dicT = tempT
    ans += 1
    
print(ans)

Fixed submission:
N,u,v = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int, input().split())
    E[a-1].append(b-1)
    E[b-1].append(a-1)
    

import heapq

def dijkstra(s):
    d = [10**6]*N
    used = [True]*N
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in E[s]:
        heapq.heappush(edgelist, [1, e])
    while edgelist:
        t = heapq.heappop(edgelist)
        if not used[t[1]]:
            continue
        used[t[1]] = False
        d[t[1]] = t[0]
        for e in E[t[1]]:
            if used[e]:
                heapq.heappush(edgelist, [t[0]+1, e])
                
    return d

T = dijkstra(u-1)
A = dijkstra(v-1)

ans = 0
for i in range(N):
    if T[i] < A[i]:
        ans = max(ans, A[i])
        idx = i
        
print(ans-1)

Buggy python submission (verdict: Time Limit Exceeded):
N,u,v = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
    

dicT = {u-1:0}
dicA = {v-1:0}
ans = 0
while len(dicT) > 0 and len(dicA) > 0:
    tempT = {}
    for t in dicT:
        for e in edge[t]:
            if e not in dicA:
                tempT[e] = 0
                
    dicT = tempT
    if not dicT:
        break
    tempA = {}
    for a in dicA:
        for e in edge[a]:
            if e in dicT:
                dicT[e] = 1
            else:
                tempA[e] = 0
    
    dicA = tempA
    
    tempT = {}
    for t in dicT:
        if dicT[t] == 0:
            tempT[t] = 0
            
    dicT = tempT
    ans += 1
    
print(ans)

Fixed submission:
N,u,v = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int, input().split())
    E[a-1].append(b-1)
    E[b-1].append(a-1)
    

import heapq

def dijkstra(s):
    d = [10**6]*N
    used = [True]*N
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in E[s]:
        heapq.heappush(edgelist, [1, e])
    while edgelist:
        t = heapq.heappop(edgelist)
        if not used[t[1]]:
            continue
        used[t[1]] = False
        d[t[1]] = t[0]
        for e in E[t[1]]:
            if used[e]:
                heapq.heappush(edgelist, [t[0]+1, e])
                
    return d

T = dijkstra(u-1)
A = dijkstra(v-1)

ans = 0
for i in range(N):
    if T[i] < A[i]:
        ans = max(ans, A[i])
        idx = i
        
print(ans-1)