Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=[[] for i in range(n+1)]
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
itta=[0]*(n+1)
ta=deque([u])
ao=deque([v])
itta[v]=1
cnt=0
while 1:
    ittata=[0]*(n+1)
    for i in range(len(ta)):
        tmp=ta.popleft()
        if itta[tmp]==1:
            continue
        for i in dic[tmp]:
            if itta[i]==0 and ittata[i]==0:
                ittata[i]=1
                ta.append(i)
    if len(ta)==0:
        print(cnt)
        break
    cnt+=1
    for i in range(len(ao)):
        tmp=ao.popleft()
        for i in dic[tmp]:
            if itta[i]==0:
                itta[i]=1
                ao.append(i)
    # print(ta)
    # print(ao)

Fixed submission:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=defaultdict(list)
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
# print(dic)

taka=[-1]*(n+1)
aoki=[-1]*(n+1)

d=deque([[u,0]])
if len(dic[u])==1:
    taka[u]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if taka[dic[now][i]]==-1:
            taka[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        taka[now]=cnt
# print(taka)

d=deque([[v,0]])
if len(dic[v])==1:
    aoki[v]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if aoki[dic[now][i]]==-1:
            aoki[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        aoki[now]=cnt
# print(aoki)

ans=0
for i in range(n+1):
    if taka[i]<aoki[i]:
        ans=max(ans, aoki[i]-1)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=[[] for i in range(n+1)]
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
itta=[0]*(n+1)
ta=deque([u])
ao=deque([v])
itta[v]=1
cnt=0
while 1:
    for i in range(len(ta)):
        tmp=ta.popleft()
        if itta[tmp]==1:
            continue
        for i in dic[tmp]:
            if itta[i]==0:
                ta.append(i)
    if len(ta)==0:
        print(cnt)
        break
    cnt+=1
    for i in range(len(ao)):
        tmp=ao.popleft()
        for i in dic[tmp]:
            if itta[i]==0:
                itta[i]=1
                ao.append(i)
    # print(ta)
    # print(ao)

Fixed submission:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=defaultdict(list)
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
# print(dic)

taka=[-1]*(n+1)
aoki=[-1]*(n+1)

d=deque([[u,0]])
if len(dic[u])==1:
    taka[u]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if taka[dic[now][i]]==-1:
            taka[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        taka[now]=cnt
# print(taka)

d=deque([[v,0]])
if len(dic[v])==1:
    aoki[v]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if aoki[dic[now][i]]==-1:
            aoki[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        aoki[now]=cnt
# print(aoki)

ans=0
for i in range(n+1):
    if taka[i]<aoki[i]:
        ans=max(ans, aoki[i]-1)
print(ans)