Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

tmp = 0
MAX = 100000
prime = primes(MAX)
a = [0 for _ in range(MAX+1)]
for i in range(MAX+1):
    if i%2 == 0:
        continue
    elif i in prime and int((i+1)/2) in prime:
        a[i] = 1
#累積和
s = [0 for _ in range(MAX+2)]
for i in range(MAX+1):
    s[i+1] = s[i] + a[i]


Q = int(input())
for i in range(Q):
    l,r = map(int, input().split())
    print(s[r+1]-s[l])

Fixed submission:
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

tmp = 0
MAX = 100000
prime = primes(MAX)
a = [0 for _ in range(MAX+1)]
for i in range(MAX+1):
    if i%2 == 0:
        continue
    elif i in prime and int((i+1)/2) in prime:
        a[i] = 1
#累積和
s = [0 for _ in range(MAX+2)]
for i in range(MAX+1):
    s[i+1] = s[i] + a[i]


Q = int(input())
for i in range(Q):
    l,r = map(int, input().split())
    print(s[r+1]-s[l])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())

res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        for k in range(j+1, N):
            if j - i == k - j:
                continue
            if S[j] != S[k] and S[i] != S[k]:
                res += 1

print(res)


Fixed submission:
N = int(input())
S = list(input())

r = len([s for s in S if s == "R"])
g = len([s for s in S if s == "G"])
b = len([s for s in S if s == "B"])

res = r * g * b

for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j - i)
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            res -= 1

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())

res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            if j - i == k - j:
                continue
            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                res += 1

print(res)


Fixed submission:
N = int(input())
S = list(input())

r = len([s for s in S if s == "R"])
g = len([s for s in S if s == "G"])
b = len([s for s in S if s == "B"])

res = r * g * b

for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j - i)
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            res -= 1

print(res)
Buggy python submission (verdict: Time Limit Exceeded):
from math import floor

A, B = map(int, input().split())

res = -1
for price in range(1, 10**7):
    if floor(price*0.08) == A and floor(price*0.1) == B:
        res = price
        break

print(res)


Fixed submission:
from math import floor

A, B = map(int, input().split())

res = -1
for price in range(1, 10**6):
    if floor(price*0.08) == A and floor(price*0.1) == B:
        res = price
        break

print(res)
Buggy python submission (verdict: Runtime Error):
import math

S = list(input())

prev = S[0]
series = 1
l = []

for i in range(1, len(S)):
    if prev == S[i]:
        series += 1
    else:
        l.append((prev, series))
        series = 1


prev_max = l[0][1]-1
prev = math.factorial(l[0][1]-1)
res = prev

for s, n in range(len(l)):
    if s == '<':
        prev = math.factorial(n-1)
        prev_max = n-1
        res += prev
    else:
        if n-1 > prev_max:
            prev = math.factorial(n-2)
            res += prev
        else:
            res -= prev_max
            prev = math.factorial(n-1)
            res += prev

print(res)


Fixed submission:
def calc(n):
    return n * (n+1) / 2

S = list(input())

prev = S[0]
series = 1
l = []

for i in range(1, len(S)):
    if prev == S[i]:
        series += 1
    else:
        l.append((prev, series))
        prev = S[i]
        series = 1

l.append((prev, series))

prev_max = l[0][1]
prev = calc(prev_max)
res = prev

for s, n in l[1::]:
    if s == '<':
        prev = calc(n)
        prev_max = n
        res += prev
    else:
        if n >= prev_max:
            res -= prev_max
            prev = calc(n)
            res += prev
        else:
            prev = calc(n-1)
            res += prev

print(int(res))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = sorted(list(map(int, input().split())))

res = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if L[k] < L[i] + L[j]:
                res += 1
            else:
                break

print(res)


Fixed submission:
import bisect

N = int(input())
L = sorted(list(map(int, input().split())))

res = 0
for i in range(N):
    for j in range(i+1, N):
        idx = bisect.bisect_left(L, L[i]+L[j])
        res += max(0, idx-j-1)

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

N, M, L = map(int, input().split())
INF = 10**9 + 1
dist = [[0 if i == j else INF for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  a, b, c = map(int, input().split())
  dist[a][b] = c
  dist[b][a] = c

Q = int(input())
queries = [list(map(int, input().split())) for i in range(Q)]

for k in range(1, N+1):
  for i in range(1, N+1):
    if dist[i][k] == INF:
      continue
    for j in range(1, N+1):
      if dist[k][j] == INF:
        continue
      dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

dist_2 = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(1, N+1):
  for j in range(i+1, N+1):
    if dist[i][j] <= L:
      dist_2[i][j] = 1
      dist_2[j][i] = 1

for k in range(1, N+1):
  for i in range(1, N+1):
    if dist_2[i][k] == INF:
      continue
    for j in range(1, N+1):
      if dist_2[j][k] == INF:
        continue
      dist_2[i][j] = min(dist_2[i][j], dist_2[i][k] + dist_2[k][j])

for i in range(Q):
  s, t = queries[i]
  print(-1 if dist_2[s][t] == INF else dist_2[s][t]-1)

Fixed submission:
from scipy.sparse.csgraph import floyd_warshall

N, M, L = map(int, input().split())
INF = 10**9 + 1
dist = [[0 if i == j else INF for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  a, b, c = map(int, input().split())
  dist[a][b] = c
  dist[b][a] = c

dist = floyd_warshall(dist)

dist_2 = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(1, N+1):
  for j in range(i+1, N+1):
    if dist[i][j] <= L:
      dist_2[i][j] = 1
      dist_2[j][i] = 1

dist_2 = floyd_warshall(dist_2)

Q = int(input())
for i in range(Q):
  s, t = map(int, input().split())
  print(-1 if dist_2[s][t] == INF else int(dist_2[s][t]-1))
Buggy python submission (verdict: Time Limit Exceeded):
def make_divisors(n):
  divisors = [n]
  for i in range(1, n//2+1):
    if n % i == 0:
      divisors.append(i)
  divisors.sort()
  return divisors


A, B = map(int, input().split())
divided_1 = min(A, B)
divided_2 = max(A, B)

divisors = make_divisors(divided_1)
cd = []
for d in divisors:
  if divided_2 % d == 0:
    cd.append(d)

res = []
for e in cd:
  flg = True
  for i in range(1, len(res)):
    r = res[i]
    if e % r == 0:
      flg = False
      break
  if flg:
    res.append(e)

print(len(res))

Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


A, B = map(int, input().split())
divided_1 = min(A, B)
divided_2 = max(A, B)

divisors = make_divisors(divided_1)
cd = []
for d in divisors:
  if divided_2 % d == 0:
    cd.append(d)

res = []
for e in cd:
  flg = True
  for i in range(1, len(res)):
    r = res[i]
    if e % r == 0:
      flg = False
      break
  if flg:
    res.append(e)

print(len(res))
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
M = [input() for i in range(H)]
dp = [[0 for i in range(W)] for j in range(H)]
dp[0][0] = 1

for i in range(H):
  for j in range(W):
    if i+1 < H and M[i+1][j] == ".":
      dp[i+1][j] = dp[i][j]
    if j+1 < W and M[i][j+1] == ".":
      dp[i][j+1] += dp[i][j]

print(dp[H][W] % (10**9+7))


Fixed submission:
H, W = map(int, input().split())
M = [input() for i in range(H)]
dp = [[0 for i in range(W+1)] for j in range(H+1)]
dp[1][1] = 1

for i in range(1, H+1):
  for j in range(1, W+1):
    if i == 1 and j == 1:
      continue
    if M[i-1][j-1] == "#":
      continue
    dp[i][j] = dp[i-1][j] + dp[i][j-1]
print(dp[H][W] % (10**9+7))
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
print(sum(a[::2]-sum(a[1::2])))

Fixed submission:
N = int(input())
cards = sorted(list(map(int, input().split())), reverse=True)

alice = 0
bob = 0
for i in range(N):
  if i % 2 == 0:
    alice += cards[i]
  else:
    bob += cards[i]

print(alice-bob)

Buggy python submission (verdict: Runtime Error):
N = int(input())
cards = list(map(int, input().split()))

alice = 0
bob = 0
for i in range(N+1):
  if i % 2 == 0:
    alice += cards[i]
  else:
    bob += cards[i]

print(alice-bob)

Fixed submission:
N = int(input())
cards = sorted(list(map(int, input().split())), reverse=True)

alice = 0
bob = 0
for i in range(N):
  if i % 2 == 0:
    alice += cards[i]
  else:
    bob += cards[i]

print(alice-bob)
Buggy python submission (verdict: Runtime Error):
from collections import deque

N = int(input())
A = list(map(int, input().split()))

q = deque()
q.append(A[0])
q.append(A[1])
for i in range(2, N):
  a = A[i]
  if i % 2 == 1:
    q.appendleft(a)
  else:
    q.append(a)

print(" ".join(map(str, q)))


Fixed submission:
from collections import deque

N = int(input())
A = list(map(int, input().split()))

q = deque()
for i in range(N):
  a = A[i]
  if i > 1 and i % 2 == 0:
    q.appendleft(a)
  else:
    q.append(a)

if N % 2 == 0:
  q.reverse()
print(" ".join(map(str, q)))
Buggy python submission (verdict: Runtime Error):
def bfs(u):
  if used.count(True) == N:
    res[0] += 1
    return
  for v in adj[u]:
    if used[v]:
      continue
    used[v] = True
    bfs(v)
    used[v] = False

N, M = map(int, input().split())
adj = [[] for i in range(N)]
for i in range(N):
  u, v = map(lambda x: int(x)-1, input().split())
  adj[u].append(v)
  adj[v].append(u)

used = [False for i in range(N)]
used[0] = True
res = [0]
bfs(0)

print(res[0])


Fixed submission:
def bfs(u):
  if used.count(True) == N:
    res[0] += 1
    return
  for v in adj[u]:
    if used[v]:
      continue
    used[v] = True
    bfs(v)
    used[v] = False

N, M = map(int, input().split())
adj = [[] for i in range(N)]
for i in range(M):
  u, v = map(lambda x: int(x)-1, input().split())
  adj[u].append(v)
  adj[v].append(u)

used = [False for i in range(N)]
used[0] = True
res = [0]
bfs(0)

print(res[0])
Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())

res = 0
for x in range(K+1):
  for y in range(K+1):
    for z in range(K+1):
      if x+y+z == S:
        res += 1

print(res)


Fixed submission:
K, S = map(int, input().split())

res = 0
for x in range(K+1):
  for y in range(K+1):
    z = S - (x + y)
    if 0 <= z <= K:
      res += 1

print(res)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
D = [s[::-1] for s in ["dream", "dreamer", "erase", "eraser"]]

i = 0
flg = True
while i < len(S):
  for d in D:
    if S[i:i+len(d)] == d:
      i += len(d)
      break
    flg = False
    break

if flg:
  print('YES')
else:
  print('NO')


Fixed submission:
import re
S = input()

S = re.sub(r'eraser', '', S)
S = re.sub(r'erase', '', S)
S = re.sub(r'dreamer', '', S)
S = re.sub(r'dream', '', S)

if len(S) == 0:
  print('YES')
else:
  print('NO')
Buggy python submission (verdict: Runtime Error):
in=input()
ok=0
for i in range(len(in)):
  if(in[i]=='7'):
    print("Yes")
    ok=1
    break
if(ok==0):
  print("No")

Fixed submission:
inp=input()
ok=0
for i in range(len(inp)):
  if(inp[i]=='7'):
    print("Yes")
    ok=1
    break
if(ok==0):
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    inf = 10**17
    #mod = 10**9 + 7

    n,m,s = map(int, input().split())
    s = min(s, 2500)
    # edge[i]:頂点iからの[行先, 必要な銀貨, 時間]
    edge = [[] for _ in range(n)] #頂点数, 場合によって変える
    for _ in range(m):
        a,b,c,d = map(int, input().split())
        edge[a-1].append((b-1, c, d))
        edge[b-1].append((a-1, c, d))

    # 銀貨との交換を自己ループとする
    for i in range(n):
        c, d = map(int, input().split())
        edge[i].append((i, -c, d))

    dp = [[inf]*2501 for _ in range(n)]
    dp[0][s] = 0
    # [時間, 所持金, 頂点]
    hq = [(0, s, 0)]
    heapify(hq)

    while hq:
        t, ginka, v = heappop(hq)
        if dp[v][ginka] < t:
            continue
        for nv, a, b in edge[v]:
            if ginka < a:
                continue
            ng = ginka - a
            if ng > 2500:
                ng = 2500
            for i in range(ng, -1, -1):
                if dp[nv][i] > t+b:
                    dp[nv][i] = t+b
                else:
                    break
            heappush(hq, (t+b, ng, nv))
    
    for i in range(1, n):
        print(min(dp[i]))

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    inf = 10**17
    #mod = 10**9 + 7

    n,m,s = map(int, input().split())
    s = min(s, 2500)
    # edge[i]:頂点iからの[行先, 必要な銀貨, 時間]
    edge = [[] for _ in range(n)] #頂点数, 場合によって変える
    for _ in range(m):
        a,b,c,d = map(int, input().split())
        edge[a-1].append((b-1, c, d))
        edge[b-1].append((a-1, c, d))

    # 銀貨との交換を自己ループとする
    for i in range(n):
        c, d = map(int, input().split())
        edge[i].append((i, -c, d))

    dp = [[inf]*2501 for _ in range(n)]
    dp[0][s] = 0
    # [時間, 所持金, 頂点]
    hq = [(0, s, 0)]
    heapify(hq)

    while hq:
        t, ginka, v = heappop(hq)
        if dp[v][ginka] < t:
            continue
        for nv, a, b in edge[v]:
            if ginka < a:
                continue
            ng = ginka - a
            if ng > 2500:
                ng = 2500
            if dp[nv][ng] > t+b:
                dp[nv][ng] = t+b
                heappush(hq, (t+b, ng, nv))
    
    for i in range(1, n):
        print(min(dp[i]))

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    X,Y,a,b,c = map(int, input().split())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    r = list(map(int, input().split()))
    i,j,k = 0,0,0
    p.sort(reverse=1)
    q.sort(reverse=1)
    r.sort(reverse=1)
    res = 0
    while k<c and i<X and j<Y:
        s = p[i]
        t = q[j]
        u = r[k]
        if s<t and s<u:
            res += t+u
            j += 1
            k += 1
        elif t<s and t<u:
            res += s+u
            i += 1
            k += 1
        else:
            res += s+t
            i += 1
            j += 1
    if i==X:
        l = q[j:] + r[k:]
        l.sort(reverse=1)
        res += sum(l[:X+Y-i-j-k])
    elif j==Y:
        l = p[i:] + r[k:]
        l.sort(reverse=1)
        res += sum(l[:X+Y-i-j-k])
    else:
        res = 0
        for i in range(X+Y-c):
            if i < X and X+Y-c-i<Y:
                res = max(res, p[:i]+q[:X+Y-c-i])
            else:
                continue
        res += sum(r)
    print(res)


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    X,Y,a,b,c = map(int, input().split())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))
    r = list(map(int, input().split()))
    i,j,k = 0,0,0
    p.sort(reverse=1)
    q.sort(reverse=1)
    r.sort(reverse=1)
    res = 0
    #[値, 種類, インデックス]
    hq = [[-p[0], 0, 0], [-q[0], 1, 0], [-r[0], 2, 0]]
    heapify(hq)
    n = 0
    while X+Y>n:
        x,y,z = heappop(hq)
        res += -x
        n += 1
        if y==0 and z+1<X:
            heappush(hq, [-p[z+1], 0, z+1])
        if y==1 and z+1<Y:
            heappush(hq, [-q[z+1], 1, z+1])
        if y==2 and z+1<c:
            heappush(hq, [-r[z+1], 2, z+1])
    print(res)
        
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int, input().split())
if c-a-b<0:
    print('No')
else:
    4*a*b<(c-a-b)**2:
    print('Yes')

Fixed submission:

a,b,c = map(int, input().split())
if c-a-b<0:
    print('No')
else:
    if 4*a*b<(c-a-b)**2:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = [input() for _ in range(n)]

import collections
c0 = collections.Counter(s)
c1 = c0.most_common()
max = c1[0][1]
l = []
for x, i in c0.items():
    if i == max:
        l.append(x)
ans = sorted(l)
for i in ans:
    print(i)

Fixed submission:
a, b, c = list(map(int, input().split()))
if a==b and b!=c:
    print('Yes')
elif a==c and a!=b:
    print('Yes')
elif c==b and b!=a:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [input() for _ in range(n)]

v = sorted(set(s))
num = []

for i in v:
    num.append(s.count(i))
d = dict(zip(v, num))
#d_sort = sorted(d.items(), key=lambda x: x[1], reverse=True)

ma = max(d.values())
for i in v:
    if d.get(i)==ma:
        print(i)

Fixed submission:
n = int(input())
s = [input() for _ in range(n)]

import collections
c0 = collections.Counter(s)
c1 = c0.most_common()
max = c1[0][1]
l = []
for x, i in c0.items():
    if i == max:
        l.append(x)
ans = sorted(l)
for i in ans:
    print(i)
Buggy python submission (verdict: Runtime Error):
n,m = int(input())
if n == m:
    print('Yes')
else:
    print('No')

Fixed submission:
n,m = map(int, input().split())
if n == m:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    c = list(map(int, input().split()))
    c.sort()
    res = 0
    b = int(pow(2, n, mod))
    for i in range(n):
        l = int(pow(2, i, mod))
        r = int(pow(2, n-i-1, mod) + (n-i-1)*pow(2, n-i-2))
        res += b*c[i]*l*r % mod
    print(res%mod)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    c = list(map(int, input().split()))
    c.sort()
    res = 0
    b = pow(2, n, mod)
    for i in range(n):
        l = pow(2, i, mod)
        if n-i-2 >= 0:
            r = pow(2, n-i-1, mod) + (n-i-1)*pow(2, n-i-2, mod)
        else:
            r = pow(2, n-i-1, mod)
        res += b*c[i]*l*r % mod
        res %= mod
    print(res)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7
    
    # u高橋, s青木
    n,u,s = map(int, input().split())
    adj = [[] for _ in range(n)] #頂点数, 場合によって変える
    for _ in range(n-1):
        a,b = map(int, input().split())
        adj[a-1].append(b-1)
        adj[b-1].append(a-1)
    ta = [-1]*n
    ao = [-1]*n

    d = deque([s-1])
    ao[s-1] = 0
    while d:
        v = d.popleft()
        for nv in adj[v]:
            if ao[nv] == -1:
                ao[nv] = ao[v] + 1
                d.append(nv)

    d = deque([u-1])
    ta[u-1] = 0
    while d:
        v = d.popleft()
        for nv in adj[v]:
            if ta[v]+1 <= ao[nv] and ta[v] < ao[v]:
                ta[nv] = ta[v] + 1
                d.append(nv)
    print(max(ta))

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7
    
    # u高橋, s青木
    n,u,s = map(int, input().split())
    adj = [[] for _ in range(n)] #頂点数, 場合によって変える
    for _ in range(n-1):
        a,b = map(int, input().split())
        adj[a-1].append(b-1)
        adj[b-1].append(a-1)
    ta = [-1]*n
    ao = [-1]*n

    d = deque([s-1])
    ao[s-1] = 0
    while d:
        v = d.popleft()
        for nv in adj[v]:
            if ao[nv] == -1:
                ao[nv] = ao[v] + 1
                d.append(nv)

    d = deque([u-1])
    ta[u-1] = 0
    while d:
        v = d.popleft()
        for nv in adj[v]:
            if ta[nv] == -1:
                ta[nv] = ta[v] + 1
                d.append(nv)

    res = 0
    for i in range(n):
        if ta[i] < ao[i]:
            res = max(res, ao[i]-1)
    print(res)

if __name__ == '__main__':
    main()