Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque
infinity=10**6
#uを中心とする深さ優先探索(の改良版)
def bfs(u,d):
    Q=deque([u])
    d[u]=0

    while(Q):
        x = Q.pop()
        for y in G[x]:
            if d[y] == infinity:
                d[y] = d[x]+1
                Q.appendleft(y)
    dlist=[set() for i in range(N)]
    #dlist=[set()]*(N-1)
    for i in range(N):
        dlist[d[i]].add(i) 

    return dlist


N,u,v = map(int,input().split())
u -= 1
v -= 1
#グラフの作成
G=[[] for i in range(N)]
# G=[[]]*N この初期化方法でappendすると全リストに要素が追加されてしまう　
# print(id(G[0]) == id(G[1]))がTrueになってしまうことからもわかる(同じオブジェクトを指してしまっている)
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

du=[infinity]*N 
dv=[infinity]*N
 
dlist_u = bfs(u,du) #逃げる側
dlist_v = bfs(v,dv) #鬼側

set_u_odd=set()
set_u_even={u} #set_uは常にi+1番目を更新したいため
set_v_odd=set()
set_v_even=set()


for i in range(N-1):
    if i%2==0:
        set_u_odd |= set(dlist_u[i+1])
        set_v_even |= set(dlist_v[i])
         
        if len(set_u_even-set_v_even)==0 or len(set_u_odd-set_v_even)==0: #前者は鬼が動いて確保, 後者は逃げる側が自爆
            print(i)
            break
    else:
        set_u_even |= set(dlist_u[i+1])
        set_v_odd |= set(dlist_v[i])
        if len(set_u_odd-set_v_odd)==0 or len(set_u_even-set_v_odd)==0:
            print(i)
            break


Fixed submission:
from collections import deque
infinity=10**6

import sys
input = sys.stdin.readline
N,u,v = map(int,input().split())
u -= 1
v -= 1

G=[[] for i in range(N)]
for i in range(N-1):
    A,B = map(int,input().split())
    A -= 1
    B -= 1
    G[A].append(B)
    G[B].append(A)

ends = []
for i in range(N):
    if len(G[i]) == 1:
        ends.append(i)

#幅優先探索
def BFS (s):
    queue = deque()
    d = [infinity]*N
    
    queue.append(s)
    d[s]= 0
    
    while len(queue)!=0:
      u = queue.popleft()
      for v in G[u]:
      	if d[v] == infinity:
        	d[v] = d[u]+1
        	queue.append(v) 	  


  
    return d

d_nigeru = BFS(u)
d_oni = BFS(v)

ans=0
for u in ends:
    if d_nigeru[u] < d_oni[u]:
        ans = max(ans,d_oni[u]-1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
infinity=10**6
#uを中心とする深さ優先探索(の改良版)
def bfs(u,d):
    Q=deque([u])
    d[u]=0

    while(Q):
        x = Q.pop()
        for y in G[x]:
            if d[y] == infinity:
                d[y] = d[x]+1
                Q.appendleft(y)
    dlist=[set() for i in range(N)]
    #dlist=[set()]*(N-1)
    for i in range(N):
        dlist[d[i]].add(i) 

    return dlist


N,u,v = map(int,input().split())
u -= 1
v -= 1
#グラフの作成
G=[[] for i in range(N)]
# G=[[]]*N この初期化方法でappendすると全リストに要素が追加されてしまう　
# print(id(G[0]) == id(G[1]))がTrueになってしまうことからもわかる(同じオブジェクトを指してしまっている)
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

du=[infinity]*N 
dv=[infinity]*N
 
dlist_u = bfs(u,du) #逃げる側
dlist_v = bfs(v,dv) #鬼側

set_u_odd=set()
set_u_even={u} #set_uは常にi+1番目を更新したいため
set_v_odd=set()
set_v_even=set()


for i in range(N-1):
    if i%2==0:
        set_u_odd |= set(dlist_u[i+1])
        set_v_even |= set(dlist_v[i])
         
        if len(set_u_even-set_v_even)==0 or len(set_u_odd-set_v_even)==0: #前者は鬼が動いて確保, 後者は逃げる側が自爆
            print(i)
            break
    else:
        set_u_even |= set(dlist_u[i+1])
        set_v_odd |= set(dlist_v[i])
        if len(set_u_odd-set_v_odd)==0 or len(set_u_even-set_v_odd)==0:
            print(i)
            break


Fixed submission:
from collections import deque
infinity=10**6

import sys
input = sys.stdin.readline
N,u,v = map(int,input().split())
u -= 1
v -= 1

G=[[] for i in range(N)]
for i in range(N-1):
    A,B = map(int,input().split())
    A -= 1
    B -= 1
    G[A].append(B)
    G[B].append(A)

ends = []
for i in range(N):
    if len(G[i]) == 1:
        ends.append(i)

#幅優先探索
def BFS (s):
    queue = deque()
    d = [infinity]*N
    
    queue.append(s)
    d[s]= 0
    
    while len(queue)!=0:
      u = queue.popleft()
      for v in G[u]:
      	if d[v] == infinity:
        	d[v] = d[u]+1
        	queue.append(v) 	  


  
    return d

d_nigeru = BFS(u)
d_oni = BFS(v)

ans=0
for u in ends:
    if d_nigeru[u] < d_oni[u]:
        ans = max(ans,d_oni[u]-1)

print(ans)