Buggy python submission (verdict: Runtime Error):
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

if taka == aoki:
    return 0

q = [(0, aoki)]
aoki_dist = [-1] * n
aoki_dist[aoki] = 0

index = 0
while index < len(q):
    c0, i = q[index]
    c = c0 + 1
    for l in vert[i]:
        if aoki_dist[l] >= 0:
            continue
        aoki_dist[i] = c
        q.append((c, l))
    index += 1

q = [(0, taka)]
max_dist = aoki_dist[taka]

index = 0
while index < len(q):
    c0, i = q[index]
    c = c0 + 1
    for l in vert[i]:
        if aoki_dist[l] < 0:
            continue
        if c >= aoki_dist[l]:
            continue
        if max_dist < aoki_dist[l]:
            max_dist = aoki_dist[l]
        aoki_dist[l] = -1
        q.append((c, l))
    index += 1

print(max_dist - 1)

Fixed submission:
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

if taka == aoki:
    print(0)
    exit()

def bfs(begin):
    q = [(0, begin)]
    dist = [-1] * n
    dist[begin] = 0

    index = 0
    while index < len(q):
        c0, i = q[index]
        c = c0 + 1
        for l in vert[i]:
            if dist[l] >= 0:
                continue
            dist[l] = c
            q.append((c, l))
        index += 1
    return dist

aoki_dist = bfs(aoki)
taka_dist = bfs(taka)

ans = 0
for i in range(n):
    if taka_dist[i] < aoki_dist[i]:
        ans = max(ans, aoki_dist[i] - 1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

q = [(0, aoki)]
aoki_dist = [-1] * n
closed = 0

index = 0
while index < len(q):
    c0, i = q[index]
    aoki_dist[i] = c0
    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        closed |= (1 << i)
        q.append((c, l))
    index += 1

q = [(0, taka)]
closed = (1 << taka)
max_dist = aoki_dist[taka]

index = 0
while index < len(q):
    c0, i = q[index]
    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        closed |= (1 << l)
        if c >= aoki_dist[l]:
            continue
        if max_dist < aoki_dist[l]:
            max_dist = aoki_dist[l]
        q.append((c, l))

    index += 1

print(max_dist - 1)


Fixed submission:
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

if taka == aoki:
    print(0)
    exit()

def bfs(begin):
    q = [(0, begin)]
    dist = [-1] * n
    dist[begin] = 0

    index = 0
    while index < len(q):
        c0, i = q[index]
        c = c0 + 1
        for l in vert[i]:
            if dist[l] >= 0:
                continue
            dist[l] = c
            q.append((c, l))
        index += 1
    return dist

aoki_dist = bfs(aoki)
taka_dist = bfs(taka)

ans = 0
for i in range(n):
    if taka_dist[i] < aoki_dist[i]:
        ans = max(ans, aoki_dist[i] - 1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

q = []
heapq.heappush(q, (0, aoki))
aoki_dist = [-1] * n
closed = 0

while len(q) > 0:
    c0, i = heapq.heappop(q)
    aoki_dist[i] = c0
    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        closed |= (1 << i)
        heapq.heappush(q, (c, l))

heapq.heappush(q, (0, taka))
closed = (1 << taka)
max_dist = aoki_dist[taka]

while len(q) > 0:
    c0, i = heapq.heappop(q)
    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        closed |= (1 << l)
        if c >= aoki_dist[l]:
            continue
        if max_dist < aoki_dist[l]:
            max_dist = aoki_dist[l]
        heapq.heappush(q, (c, l))

print(max_dist - 1)

Fixed submission:
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

if taka == aoki:
    print(0)
    exit()

def bfs(begin):
    q = [(0, begin)]
    dist = [-1] * n
    dist[begin] = 0

    index = 0
    while index < len(q):
        c0, i = q[index]
        c = c0 + 1
        for l in vert[i]:
            if dist[l] >= 0:
                continue
            dist[l] = c
            q.append((c, l))
        index += 1
    return dist

aoki_dist = bfs(aoki)
taka_dist = bfs(taka)

ans = 0
for i in range(n):
    if taka_dist[i] < aoki_dist[i]:
        ans = max(ans, aoki_dist[i] - 1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

q = []
heapq.heappush(q, (0, aoki))
aoki_dist = [-1] * n
closed = 0

while len(q) > 0:
    c0, i = heapq.heappop(q)
    aoki_dist[i] = c0
    closed |= (1 << i)

    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        heapq.heappush(q, (c, l))

closed = 0
heapq.heappush(q, (0, taka))
max_dist = aoki_dist[taka] - 1

while len(q) > 0:
    c0, i = heapq.heappop(q)
    closed |= (1 << i)

    c = c0 + 1
    for l in vert[i]:
        if closed & (1 << l) != 0:
            continue
        closed |= (1 << l)
        if c >= aoki_dist[l]:
            continue
        if max_dist < aoki_dist[l] - 1:
            max_dist = aoki_dist[l] - 1
        heapq.heappush(q, (c, l))

print(max_dist)

Fixed submission:
import heapq;

n, u, v = [int(v) for v in input().split()]

vert = []

for i in range(n):
    vert.append([])

for i in range(n - 1):
    a, b = [int(v) - 1 for v in input().split()]
    vert[a].append(b)
    vert[b].append(a)

taka = u-1
aoki = v-1

if taka == aoki:
    print(0)
    exit()

def bfs(begin):
    q = [(0, begin)]
    dist = [-1] * n
    dist[begin] = 0

    index = 0
    while index < len(q):
        c0, i = q[index]
        c = c0 + 1
        for l in vert[i]:
            if dist[l] >= 0:
                continue
            dist[l] = c
            q.append((c, l))
        index += 1
    return dist

aoki_dist = bfs(aoki)
taka_dist = bfs(taka)

ans = 0
for i in range(n):
    if taka_dist[i] < aoki_dist[i]:
        ans = max(ans, aoki_dist[i] - 1)

print(ans)