Buggy python submission (verdict: Time Limit Exceeded):
import sys


n,u,v = [int(_) for _ in input().split()]
dic = {}
for i in range(n-1):
    a,b = [int(_) for _ in input().split()]
    if a in dic:
        dic[a].append(b)
    else:
        dic[a] = [b]
    if b in dic:
        dic[b].append(a)
    else:
        dic[b] = [a]
# print(dic)
que = []
seen = {}
dic2 = {}
ans_l = []
for i in range(1,n+1):
    seen[i] = 0
    dic2[i] = 0
# print(seen)
que.append(v)
seen[v] = 1
while len(que) != 0:
    tem_v = que.pop()
    d = dic2[tem_v]
    if len(dic[tem_v]) == 1 and seen[dic[tem_v][0]] == 1:
        ans_l.append((tem_v, d))
        # print(tem_v, dic[tem_v])
    for node in dic[tem_v]:
        if seen[node] == 0:
            que.append(node)
            seen[node] = 1
            dic2[node] = d + 1
# print(dic2)
# print(seen)
ans_l = sorted(ans_l, reverse=True, key=lambda x: x[1])
# print(ans_l)
d_taka = dic2[u]
# print(d_taka)
que.append(u)
way = [u]
while len(que) != 0:
    tem_u = que.pop()
    d = dic2[tem_u]
    for node in dic[tem_u]:
        if dic2[node] < d:
            que.append(node)
            way.append(node)
# print(way)
for i in range(len(ans_l)):
    node, tem_d = ans_l[i]
    que.append(node)
    while len(que) != 0:
        tem_u = que.pop()
        d = dic2[tem_u]
        if tem_u in way:
            ans = tem_u
        else:
            for node2 in dic[tem_u]:
                if node2 in way:
                    ans = node2
                if dic2[node2] < d and node2 not in way:
                    que.append(node2)
    # print('ans', ans)
    if dic2[ans] * 2 > d_taka:
        print(tem_d - 1)
        sys.exit()



Fixed submission:
import sys


n,u,v = [int(_) for _ in input().split()]
dic = {}
for i in range(n-1):
    a,b = [int(_) for _ in input().split()]
    if a in dic:
        dic[a].append(b)
    else:
        dic[a] = [b]
    if b in dic:
        dic[b].append(a)
    else:
        dic[b] = [a]
# print(dic)
que = []
seen = {}
dic2 = {}
way = {}
ans_l = []
for i in range(1,n+1):
    seen[i] = 0
    dic2[i] = 0
    way[i] = 0
# print(seen)
que.append(v)
seen[v] = 1
while len(que) != 0:
    tem_v = que.pop()
    d = dic2[tem_v]
    if len(dic[tem_v]) == 1 and seen[dic[tem_v][0]] == 1:
        ans_l.append((tem_v, d))
        # print(tem_v, dic[tem_v])
    for node in dic[tem_v]:
        if seen[node] == 0:
            que.append(node)
            seen[node] = 1
            dic2[node] = d + 1
# print(dic2)
# print(seen)
ans_l = sorted(ans_l, reverse=True, key=lambda x: x[1])
# print(ans_l)
d_taka = dic2[u]
# print(d_taka)
que.append(u)
way[u] = 1
while len(que) != 0:
    tem_u = que.pop()
    d = dic2[tem_u]
    for node in dic[tem_u]:
        if dic2[node] < d:
            que.append(node)
            way[node] = 1
# print(way)
for i in range(len(ans_l)):
    node, tem_d = ans_l[i]
    que.append(node)
    while len(que) != 0:
        tem_u = que.pop()
        d = dic2[tem_u]
        if way[tem_u] == 1:
            ans = tem_u
        else:
            for node2 in dic[tem_u]:
                if way[node2] == 1:
                    ans = node2
                if dic2[node2] < d and way[node2] == 0:
                    que.append(node2)
    # print('ans', ans)
    if dic2[ans] * 2 > d_taka:
        print(tem_d - 1)
        sys.exit()