Buggy python submission (verdict: Runtime Error):
from collections import deque
N,ppl,oni = map(int,input().split())
ppl -= 1
oni -= 1
AB = [ []for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    AB[a-1].append(b-1)
    AB[b-1].append(a-1)

def dfs(start):
    dist = [-1 for i in range(N)]
    dist[start] = 0
    que = deque()
    que.append(start)
    while que:
        q = que.popleft()
        for nextr in AB[q]:
            if dist[nextr] == -1:
                dist[nextr] = dist[q] +1
                que.append(nextr)
    return dist
    
if len(AB[ppl]) == 1 and oni in AB[ppl]:
    print(0)
else:
    ppldist = dfs(ppl)
    onidist = dfs(oni)
    maxa = max(onidist)
    for i in range(max(onidist),-1,-1):
        for j in [k for k in range(len(onidist)) if onidist[k] == maxa]:
            if maxa >= ppldist[j]:
                if maxa&ppldist[j] == 1:
                    ans = maxa
                else:
                    ans = maxa-1
    print(ans)

Fixed submission:
from collections import deque
N,ppl,oni = map(int,input().split())
ppl -= 1
oni -= 1
AB = [ []for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    AB[a-1].append(b-1)
    AB[b-1].append(a-1)

def dfs(start):
    dist = [-1 for i in range(N)]
    dist[start] = 0
    que = deque()
    que.append(start)
    while que:
        q = que.popleft()
        for nextr in AB[q]:
            if dist[nextr] == -1:
                dist[nextr] = dist[q] +1
                que.append(nextr)
    return dist
    
if len(AB[ppl]) == 1 and oni in AB[ppl]:
    print(0)
else:
    ppldist = dfs(ppl)
    onidist = dfs(oni)
    maxa = max(onidist)
    ans = 0
    for i in range(N):
        if ppldist[i] < onidist[i]:
            ans = max(ans,onidist[i]-1)
    print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import deque
N,ppl,oni = map(int,input().split())
ppl -= 1
oni -= 1
AB = [ []for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    AB[a-1].append(b-1)
    AB[b-1].append(a-1)

def dfs(start):
    dist = [-1 for i in range(N)]
    dist[start] = 0
    que = deque()
    que.append(start)
    while que:
        q = que.popleft()
        for nextr in AB[q]:
            if dist[nextr] == -1:
                dist[nextr] = dist[q] +1
                que.append(nextr)
    return dist
    
if len(AB[ppl]) == 1 and oni in AB[ppl]:
    print(0)
else:
    ppldist = dfs(ppl)
    onidist = dfs(oni)
    maxa = max(onidist)
    for i in range(max(onidist),-1,-1):
        for j in [k for k in range(len(onidist)) if onidist[k] == maxa]:
            if maxa >= ppldist[j]:
                if maxa&ppldist[j] == 1:
                    ans = maxa
                else:
                    ans = maxa-1
    print(ans)

Fixed submission:
from collections import deque
N,ppl,oni = map(int,input().split())
ppl -= 1
oni -= 1
AB = [ []for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    AB[a-1].append(b-1)
    AB[b-1].append(a-1)

def dfs(start):
    dist = [-1 for i in range(N)]
    dist[start] = 0
    que = deque()
    que.append(start)
    while que:
        q = que.popleft()
        for nextr in AB[q]:
            if dist[nextr] == -1:
                dist[nextr] = dist[q] +1
                que.append(nextr)
    return dist
    
if len(AB[ppl]) == 1 and oni in AB[ppl]:
    print(0)
else:
    ppldist = dfs(ppl)
    onidist = dfs(oni)
    maxa = max(onidist)
    ans = 0
    for i in range(N):
        if ppldist[i] < onidist[i]:
            ans = max(ans,onidist[i]-1)
    print(ans)