Buggy python submission (verdict: Time Limit Exceeded):
def dijkstra(s, n,  cost):
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j], d[v] + cost[v][j])
    return d

n,u,v = map(int,input().split())

cnt = [0 for i in range(n)]
cost = [[float(10 ** 10) for i in range(n)] for i in range(n)]

for i in range(n-1):
    x, y = map(int, input().split())
    cost[x-1][y-1] = 1
    cost[y-1][x-1] = 1
    cnt[x-1] += 1
    cnt[y-1] += 1

ans = 0
a = dijkstra(u-1,n,cost)
b = dijkstra(v-1,n,cost)
for i in range(n):
    if a[i] <= b[i]:
        if cnt[i] == 1:
            if b[i]-a[i]-1 > 0:
                ans = max(ans,b[i]-1)
        else:
            ans = max(ans,b[i])
print(ans)

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

graph = [[] for _ in range(n+1)]

for a,b in lis:
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    dist = [-1] * (n+1)
    stack = [v]
    dist[v] = 0
    while stack:
        v = stack.pop()
        dw = dist[v] + 1
        for w in graph[v]:
            if dist[w] >= 0:
                continue
            dist[w] = dw
            stack.append(w)
    return dist

takahashi = dfs(u)
aoki = dfs(v)

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def dijkstra(s, n,  cost):
    # 始点sから各頂点への最短距離
    # n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        # まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j], d[v] + cost[v][j])
    return d


################################
# n:頂点数　w:辺の数
n,u,v = map(int,input().split())

cnt = [0 for i in range(n)]
cost = [[float(10 ** 10) for i in range(n)] for i in range(n)]
# cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(n-1):
    x, y = map(int, input().split())
    cost[x-1][y-1] = 1
    cost[y-1][x-1] = 1
    cnt[x-1] += 1
    cnt[y-1] += 1

ans = 0
a = dijkstra(u-1,n,cost)
b = dijkstra(v-1,n,cost)
for i in range(n):
    if a[i] <= b[i]:
        if cnt[i] == 1:
            if b[i]-a[i]-1 > 0:
                ans = max(ans,b[i]-1)
        else:
            ans = max(ans,b[i])
print(ans)

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

graph = [[] for _ in range(n+1)]

for a,b in lis:
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    dist = [-1] * (n+1)
    stack = [v]
    dist[v] = 0
    while stack:
        v = stack.pop()
        dw = dist[v] + 1
        for w in graph[v]:
            if dist[w] >= 0:
                continue
            dist[w] = dw
            stack.append(w)
    return dist

takahashi = dfs(u)
aoki = dfs(v)

ans = 0

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

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def dijkstra(s, n,  cost):
    # 始点sから各頂点への最短距離
    # n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        # まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j], d[v] + cost[v][j])
    return d


################################
# n:頂点数　w:辺の数
n,u,v = map(int,input().split())

cnt = [0 for i in range(n)]
cost = [[float(10 ** 10) for i in range(n)] for i in range(n)]
# cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(n-1):
    x, y = map(int, input().split())
    cost[x-1][y-1] = 1
    cost[y-1][x-1] = 1
    cnt[x-1] += 1
    cnt[y-1] += 1

ans = 0
a = dijkstra(u-1,n,cost)
b = dijkstra(v-1,n,cost)
for i in range(n):
    if a[i] <= b[i]:
        if cnt[i] == 1:
            if b[i]-a[i]-1 > 0:
                ans = max(ans,b[i]-1)
        else:
            ans = max(ans,b[i])
print(ans)

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

graph = [[] for _ in range(n+1)]

for a,b in lis:
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    dist = [-1] * (n+1)
    stack = [v]
    dist[v] = 0
    while stack:
        v = stack.pop()
        dw = dist[v] + 1
        for w in graph[v]:
            if dist[w] >= 0:
                continue
            dist[w] = dw
            stack.append(w)
    return dist

takahashi = dfs(u)
aoki = dfs(v)

ans = 0

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

print(ans)