Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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)
Aokidis = [0]*n
Takahashidis = [0]*n
def dfs(node,dis,TorA,pre):
  if TorA == "Takahashi":Takahashidis[node] = dis
  else:Aokidis[node] = dis
  for vi in G[node]:
    if vi == pre:continue
    dfs(vi,dis+1,TorA,node)
dfs(U,0,"Takahashi",-1)
dfs(V,0,"",-1)
for i in range(n):
  if Takahashidis[i] <= Aokidis[i]:
    ans = max(ans,Aokidis[i]-1)
print(ans)

Fixed submission:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
minans = inf ;ans = 0 ;count = 0 ;pro = 1
sys.setrecursionlimit(10**7)

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)
D=[-1]*n
maxD=[-1]*n

def make_tree_dfs(n,d):
    D[n]=d
    tmp=0
    f=True
    for gi in G[n]:
        if D[gi]==-1:
            f=False
            tmp=max(tmp,make_tree_dfs(gi,d+1))
    if f: tmp=d
    maxD[n]=tmp
    return tmp

make_tree_dfs(v,0)
sdis=D[u]

def good_dfs(v,d,sdis):
    f=True
    for gi in G[v]:
        if D[gi]==d-1 and d-1>sdis-(d-1):
            return good_dfs(gi,d-1,sdis)
    return (maxD[v],d)

gd,orid=good_dfs(u,D[u],sdis)
k=orid-(sdis-orid)
print(gd-orid+(sdis-orid)+k-1)
# print(D)
# print(maxD)
# print(sdis,orid,gd,k)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
minans = inf ;ans = 0 ;count = 0 ;pro = 1
sys.setrecursionlimit(10**7)

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)
D=[-1]*n
maxD=[-1]*n

def make_tree_dfs(n,d):
    D[n]=d
    tmp=0
    f=True
    for gi in G[n]:
        if D[gi]==-1:
            f=False
            tmp=max(tmp,make_tree_dfs(gi,d+1))
    if f: tmp=d
    maxD[n]=tmp
    return tmp

make_tree_dfs(v,0)
sdis=D[u]

def good_dfs(v,d,sdis):
    f=True
    for gi in G[v]:
        if D[gi]==d-1 and d-1>sdis-(d-1):
            return good_dfs(gi,d-1,sdis)
    return (maxD[v],d)

gd,orid=good_dfs(u,D[u],sdis)
k=orid-(sdis-orid)
print(gd-orid+(sdis-orid)+k-1)
# print(D)
# print(maxD)
# print(sdis,orid,gd,k)
        







Fixed submission:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
minans = inf ;ans = 0 ;count = 0 ;pro = 1
sys.setrecursionlimit(10**7)

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)
D=[-1]*n
maxD=[-1]*n

def make_tree_dfs(n,d):
    D[n]=d
    tmp=0
    f=True
    for gi in G[n]:
        if D[gi]==-1:
            f=False
            tmp=max(tmp,make_tree_dfs(gi,d+1))
    if f: tmp=d
    maxD[n]=tmp
    return tmp

make_tree_dfs(v,0)
sdis=D[u]

def good_dfs(v,d,sdis):
    f=True
    for gi in G[v]:
        if D[gi]==d-1 and d-1>sdis-(d-1):
            return good_dfs(gi,d-1,sdis)
    return (maxD[v],d)

gd,orid=good_dfs(u,D[u],sdis)
k=orid-(sdis-orid)
print(gd-orid+(sdis-orid)+k-1)
# print(D)
# print(maxD)
# print(sdis,orid,gd,k)