Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02794 (python)

Fixed submission:
n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]
m = int(input())
path = [list(map(int, input().split())) for i in range(m)]

tree = [[] for i in range(n)]
memo = {}
for i in range(n - 1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
    memo[(min(a, b), max(a, b))] = i


def dfs(par, pos, goal):
    if pos == goal:
        return True
    for child in tree[pos]:
        if child == par:
            continue
        if dfs(pos, child, goal):
            res.append((min(pos, child), max(pos, child)))
            return True
    return False
  

li_path = [0] * m
for i in range(m):
    res = []
    s, g = path[i]
    s -= 1
    g -= 1
    dfs(-1, s, g)
    tmp = 0
    for j in res:
        tmp += (1 << memo[j])
    li_path[i] = tmp
  
ans = 0
for bit_state in range(1 << m):
    mask = 0
    cnt = 0
    for i in range(m):
        if bit_state & (1 << i):
            cnt += 1
            mask |= li_path[i]
    num_n = bin(mask).count("1")
    if cnt % 2 == 0:
        ans += 2 ** (n - 1 - num_n)
    else:
        ans -= 2 ** (n - 1 - num_n)
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque


def dfs(start, goal, tree):
    """startからgoalまでの木上のpathを出力する"""
    q = deque([start])
    visited = {}
    visited[start] = -1
    while True:
        pos = q.pop()
        if pos == goal:
            break
        for child in tree[pos]:
            if child in visited:
                continue
            visited[child] = pos
            q.append(child)
    res = []
    pos = goal
    while True:
        par = visited[pos]
        if par == -1:
            break
        res.append((min(par, pos), max(par, pos)))
        pos = par
    return res


n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]
m = int(input())
path = [list(map(int, input().split())) for i in range(m)]

tree = [[] for i in range(n)]
memo = {}
for i in range(n - 1):
    a, b = info[i]
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[b].append(a)
    memo[(min(a, b), max(a, b))] = i
  

li_path = [0] * m
for i in range(m):
    s, g = path[i]
    s -= 1
    g -= 1
    res = dfs(s, g)
    tmp = 0
    for j in res:
        tmp += (1 << memo[j])
    li_path[i] = tmp
  
ans = 0
for bit_state in range(1 << m):
    mask = 0
    cnt = 0
    for i in range(m):
        if bit_state & (1 << i):
            cnt += 1
            mask |= li_path[i]
    num_n = bin(mask).count("1")
    if cnt % 2 == 0:
        ans += 2 ** (n - 1 - num_n)
    else:
        ans -= 2 ** (n - 1 - num_n)
print(ans)
Problem p02851 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

ruiseki = [0] * (n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

for i in range(n + 1):
    ruiseki[i] -= i
    ruiseki [i] %= k

memo = {}
cnt = 0
ans = 0
for i in range(n+1):
    if ruiseki[i] in memo:
        ans += memo[ruiseki[i]]

    if ruiseki[i] in memo:
        memo[ruiseki[i]] += 1
    else:
        memo[ruiseki[i]] = 1

    if cnt < k - 1:
        cnt += 1
    else:
        memo[ruiseki[i-cnt]] -= 1 

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

ruiseki = [0] * (n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

for i in range(n+1):
    ruiseki[i] -= i
    ruiseki[i] %= k
memo = {}
for i in range(min(k, n)):
    if ruiseki[i] not in memo:
        memo[ruiseki[i]] = 1
    else:
        memo[ruiseki[i]] += 1

ans = 0
for i in range(n+1):
    tmp = memo[ruiseki[i]]
    ans += tmp - 1
    memo[ruiseki[i]] -= 1
    if i+k < n+1:
        if ruiseki[i+k] not in memo:
            memo[ruiseki[i+k]] = 1
        else:
            memo[ruiseki[i+k]] += 1
print(ans)
Problem p02998 (python)

Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(200010)
n = int(input())
info = [list(map(int, input().split())) for i in range(n)]

v = 100000
graph = [[] for i in range(v*2)]
for i in range(n):
    x = info[i][0] - 1
    y = (info[i][1] - 1) + v
    graph[x].append(y)
    graph[y].append(x)

def dfs(pos):
    if visited[pos]:
        return
    visited[pos] = True
    cnt[pos//v] += 1
    for i in graph[pos]:
        dfs(i)
    return

visited = [False] * (v*2)
cnt = [0]*2
ans = 0
for i in range(v*2):
    cnt[0] = 0
    cnt[1] = 0
    dfs(i)
    ans += cnt[0]*cnt[1]
print(ans - n)

Buggy submission (verdict: Runtime Error):
import sys


sys.setrecursionlimit(2000)
n = int(input())
info = [list(map(int, input().split())) for i in range(n)]

v = 100000
graph = [[] for i in range(v*2)]
for i in range(n):
    x = info[i][0] - 1
    y = (info[i][1] - 1) + v
    graph[x].append(y)
    graph[y].append(x)

def dfs(pos):
    if visited[pos]:
        return
    visited[pos] = True
    if 0 <= pos < v:
        cnt[0] += 1
    else:
        cnt[1] += 1
    for i in graph[pos]:
        dfs(i)
    return

visited = [False] * (v*2)
cnt = [0]*2
ans = 0
for i in range(v*2):
    cnt[0] = 0
    cnt[1] = 0
    dfs(i)
    ans += cnt[0]*cnt[1]
print(ans - n)
Problem p03017 (python)

Fixed submission:
n, a, b, c, d = map(int, input().split())
s = input()

def is_possible(start, goal):
    for i in range(start, goal - 1):
        if s[i] == "#" and s[i + 1] == "#":
            return False
    return True
  
def is_possible2(start, goal):
    for i in range(start, goal):
        if s[i - 1] == "." and s[i] == "." and s[i + 1] == ".":
            return True
    return False

if c < d:
    if is_possible(a - 1, c) and is_possible(b - 1, d):
        print("Yes")
    else:
        print("No")
else:
    if is_possible(a - 1, c) and is_possible(b - 1, d) and is_possible2(b - 1, d):
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
n, a, b, c, d = map(int, input().split())
a -= 1
b -= 1
c -= 1
d -= 1
s = input()

if c < d:
    # ふぬけ君を動かす
    for i in range(b, d):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    # すぬけ君を動かす
    for i in range(a, c):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    print("Yes")
    exit()

if d < c:
    for i in range(b, d-1):
        if s[i] == s[i+1] and s[i+1] == s[i+2] and s[i] == ".":
            break
    else:
        a = []
        print(a[3])
        print("No")
        exit()
    for i in range(b, d):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    print("Yes")
    exit()

Buggy submission (verdict: Runtime Error):
n, a, b, c, d = map(int, input().split())
a -= 1
b -= 1
c -= 1
d -= 1
s = input()

if c < d:
    # ふぬけ君を動かす
    for i in range(b, d):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    # すぬけ君を動かす
    for i in range(a, c):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    print("Yes")
    exit()

if d < c:
    a = []
    print(a[2])
    for i in range(b, d-1):
        if s[i] == s[i+1] and s[i+1] == s[i+2] and s[i] == ".":
            break
    else:
        print("No")
        exit()
    for i in range(b, d):
        if s[i] == s[i+1] and s[i] == "#":
            print("No")
            exit()
    print("Yes")
    exit()
Problem p03108 (python)

Fixed submission:
#UnionFindクラス
class UnionFind(object):

  def __init__(self, n=1):
    self.par =  [i for i in range(n)]
    self.rank = [1 for _ in range(n)]

  def find(self, x):
    if self.par[x] == x:
      return x
    else:
      self.par[x] = self.find(self.par[x])
      return self.par[x]
  
  # xとyの根を結合させる
  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      if self.rank[x] < self.rank[y]:
        x, y = y, x
      self.rank[x] += self.rank[y]
      self.par[y] = x

  
  # x,yの根が同じかどうかを返す（根が同じ:True、根が異なる:False）
  def is_same(self, x, y):
    return self.find(x) == self.find(y)
    
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]
uf = UnionFind(n)
okisa = [1]*(n)
ans = [0]*(m)
ans_=0
for i in range(n):
  ans_ = ans_+i
  
ans[m-1]= ans_
for i in range(1,m)[::-1]:
  if uf.is_same(info[i][0]-1,info[i][1]-1):
    ans[i-1] = ans[i] 
  else:
    ans[i-1] = ans[i]-uf.rank[uf.find(info[i][0]-1)]*uf.rank[uf.find(info[i][1]-1)]
    uf.union(info[i][0]-1,info[i][1]-1)
for i in range(m):
  print(ans[i])

Buggy submission (verdict: Runtime Error):
#UnionFindクラス
class UnionFind:

  def __init__(self,n):
    self.par =  [i for i in range(n)]
    self.rank = [1 for _ in range(n)]

  # xの根を返す
  def find(self, x):
    if self.par[x] == x:
      return x
    else:
      self.par[x] = self.find(self.par[x])
      return self.par[x]
  
  # xとyの根を結合させる
  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x != y:
      if self.rank[x] > self.rank[y]:
        x, y = y, x
      self.rank[x] += self.rank[y]
      self.par[y] = x
  
  # x,yの根が同じかどうかを返す（根が同じ:True、根が異なる:False）
  def is_same(self, x, y):
    return self.find(x) == self.find(y)

  # xが含まれる木の要素数を返す
  def size(self, x):
    return self.rank[self.find(x)]
    
n,m = map(int,input().split())
info = [list(map(int,input().split())) for i in range(m)]
uf = UnionFind(n)
okisa = [1]*(n)
ans = [0]*(m)
ans_=0
for i in range(n):
  ans_ = ans_+i
  
ans[m-1]= ans_
for i in range(1,m)[::-1]:
  if uf.is_same(info[i][0]-1,info[i][1]-1):
    ans[i-1] = ans[i] 
  else:
    ans[i-1] = ans[i]-uf.size(info[i][0]-1)*uf.size(info[i][1]-1)
    uf.union(info[i][0]-1,info[i][1]-1)
for i in range(m):
  print(ans[i])
Problem p03222 (python)

Fixed submission:
h,w,k = map(int,input().split())

a=[[0]*w for i in range(h+1)]

fib_memo = {}
def fib(n):
    if n < 3: return 1
    if n not in fib_memo:
        fib_memo[n] = fib(n-1) + fib(n-2)
    return fib_memo[n]
  

for j in range(w):
  a[0][j]=0
a[0][0]=1

for i in range(1,h+1):
  for j in range(w):
    if w==1:
      a[i][j]=a[i-1][j]
    elif j==0:
      a[i][j]=a[i-1][j]*fib(w)+a[i-1][j+1]*fib(w-1)
    elif 1<=j<w-1:
      a[i][j]=a[i-1][j-1]*fib(w-j)*fib(j)+a[i-1][j]*fib(w-j)*fib(j+1)+a[i-1][j+1]*fib(w-j-1)*fib(j+1)
    elif j==w-1:
      a[i][j]=a[i-1][j]*fib(w)+a[i-1][j-1]*fib(w-1)
print(a[h][k-1]%1000000007)

Buggy submission (verdict: Runtime Error):
h,w,k = map(int,input().split())

a=[[0]*w for i in range(h+1)]

fib_memo = {}
def fib(n):
    if n < 3: return 1
    if n not in fib_memo:
        fib_memo[n] = fib(n-1) + fib(n-2)
    return fib_memo[n]
  

for j in range(w):
  a[0][j]=0
a[0][0]=1

for i in range(1,h+1):
  for j in range(w):
    if j==0:
      a[i][j]=a[i-1][j]*fib(w)+a[i-1][j+1]*fib(w-1)
    elif 1<=j<w-1:
      a[i][j]=a[i-1][j-1]*fib(w-j)*fib(j)+a[i-1][j]*fib(w-j)*fib(j+1)+a[i-1][j+1]*fib(w-j-1)*fib(j+1)
    elif j==w-1:
      a[i][j]=a[i-1][j]*fib(w)+a[i-1][j-1]*fib(w-1)
print(a[h][k-1]%1000000007)
Problem p03272 (python)

Fixed submission:
n,i = map(int,input().split())
print(n-i+1)

Buggy submission (verdict: Runtime Error):
n,i = map(int,input().spllit())
print(n-i+1)
Problem p03286 (python)

Fixed submission:
n = int(input())
base = 2
ans =""

while n != 0:
  ans = str(n%base)+ans
  n = -(n//base)
if ans:
  print(ans)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())

ans = ""
cnt = 0

while n != 0:
    new = n - (-2) ** cnt
    if new % (2 ** (cnt + 1)) == 0:
        ans = "1" + ans
        n = n - (-2) ** cnt
    else:
        ans = "0" + ans
    cnt += 1
    aa += 1
print(ans)
Problem p03315 (python)

Fixed submission:
s=list(input())
count =0
for i in range(len(s)):
  if s[i]=="+":
    count +=1
  else:
    count -=1
print(count)

Buggy submission (verdict: Runtime Error):
s=list(input())
count =0
for i in range(len(s)):
  if s[i]="+":
    count +=1
  else:
    count -=1
print(count)
Problem p03332 (python)

Fixed submission:
class Combination:
    '''
    計算量：階乗・逆元テーブルの作成O(N)
            nCkを求めるO(1)
    '''

    def __init__(self, n, MOD):
        self.fact = [1]
        for i in range(1, n + 1):
            self.fact.append(self.fact[-1] * i % MOD)
        self.inv_fact = [pow(self.fact[i] ,MOD - 2 ,MOD) for i in range(n + 1)]
        self.MOD = MOD

    def factorial(self, k):
        '''k!を求める'''
        return self.fact[k]

    def inverse_factorial(self, k):
        '''k!の逆元を求める'''
        return self.inv_fact[k]

    def combination(self, k, r):
        '''kCrを求める'''
        return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.MOD


n, a, b, k = map(int, input().split())
MOD = 998244353
comb = Combination(n, MOD)
ans = 0
for i in range(n+1):
    if not(k - a*i >= 0 and (k - a*i) % b == 0 and (k - a*i) // b <= n):
        continue
    else:
        ai = i
        bi = (k - a*i) // b
        ans += comb.combination(n, ai) * comb.combination(n, bi)
        ans %= MOD
print(ans)

Buggy submission (verdict: Runtime Error):
class Combination:
    '''
    計算量：階乗・逆元テーブルの作成O(N)
            nCkを求めるO(1)
    '''

    def __init__(self, n, MOD):
        self.fact = [1]
        for i in range(1, n + 1):
            self.fact.append(self.fact[-1] * i % MOD)
        self.inv_fact = [pow(self.fact[i] ,MOD - 2 ,MOD) for i in range(n + 1)]
        self.MOD = MOD

    def factorial(self, k):
        '''k!を求める'''
        return self.fact[k]

    def inverse_factorial(self, k):
        '''k!の逆元を求める'''
        return self.inv_fact[k]

    def combination(self, k, r):
        '''kCrを求める'''
        return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.MOD


n, a, b, k = map(int, input().split())
MOD = 998244353
comb = Combination(n, MOD)
ans = 0
for i in range(n+1):
    if not(k - a*i >= 0 and (k - a*i) % b == 0 and (k - a*i) % b <= n):
        continue
    else:
        ai = i
        bi = (k - a*i) // b
        ans += comb.combination(n, ai) * comb.combination(n, bi)
        ans %= MOD
print(ans)
Problem p03822 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
    j = int(input())
    j -= 1
    i += 1
    tree[i].append(j)
    tree[j].append(i)

def dfs(pos):
    if pos != 0 and len(tree[pos]) == 1:
        return 1
    li = []
    for next_pos in tree[pos]:
        if visited[next_pos]:
            continue
        visited[next_pos] = True
        li.append(dfs(next_pos))
    li = sorted(li)
    res = 0
    for i in range(len(li)):
        if li[i] > res:
            res = li[i]
        elif li[i] <= res:
            res += 1
    return res + 1

visited = [False] * n
visited[0] = True
print(dfs(0) - 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
    j = int(input())
    j -= 1
    i += 1
    tree[i].append(j)
    tree[j].append(i)

def dfs(pos):
    if pos != 0 and len(tree[pos]) == 1:
        return 1
    li = []
    for next_pos in tree[pos]:
        if visited[next_pos]:
            continue
        visited[next_pos] = True
        li.append(dfs(next_pos))
    max_li = max(li)
    len_li = len(li)
    res = max(len_li + 1, max_li + 1)
    return res

visited = [False] * n
visited[0] = True
print(dfs(0) - 1)
Problem p02598 (python)

Fixed submission:
n, k = map(int, input().split())
alst = list(map(int, input().split()))

if k == 0:
    print(max(alst))
    exit()

l = 1
r = max(alst)

while r != l:
    pos = (r + l) // 2
    cnt = 0
    for a in alst:
        cnt += (a - 1) // pos
    if cnt <= k:
        r = pos
    else:
        l = pos + 1
print(r)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
alst = list(map(int, input().split()))

if k == 0:
    print(max(alst))
    exit()

l = 0
r = max(alst)

while r != l:
    pos = (r + l) // 2
    cnt = 0
    for a in alst:
        cnt += (a - 1) // pos
    if cnt <= k:
        r = pos
    else:
        l = pos + 1
print(r)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
alst = list(map(int, input().split()))

l = 0
r = max(alst)

while r != l:
    pos = (r + l) // 2
    cnt = 0
    for a in alst:
        cnt += (a - 1) // pos
    if cnt <= k:
        r = pos
    else:
        l = pos + 1
print(r)
Problem p02625 (python)

Fixed submission:
n, m = map(int, input().split())

N = m + 100
MOD = 10 ** 9 + 7
fact = [0 for _ in range(N)]
invfact = [0 for _ in range(N)]
fact[0] = 1
for i in range(1, N):
    fact[i] = i * fact[i - 1] % MOD

invfact[N - 1] = pow(fact[N - 1], MOD - 2, MOD)

for i in range(N - 2, -1, -1):
    invfact[i] = invfact[i + 1] * (i + 1) % MOD
    
def nCk(n, k):
    if k < 0 or n < k:
        return 0
    else:
        return fact[n] * invfact[k] * invfact[n - k] % MOD
        
def nPk(n, k):
    return fact[n] * invfact[n - k] % MOD
    
ans = 0
sign = 1

for i in range(n + 1):
    ans = (ans + nPk(m, i) * nCk(n, i) * nPk(m - i, n - i) ** 2 * sign) % MOD
    sign *= -1
    
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
alst = list(map(int, input().split()))
if n == 2:
    if sum(alst) % 2 != 0:
        print(-1)
    elif alst[0] < alst[1]:
        print(-1)
    else:
        print((alst[0] - alst[1]) // 2)
    exit()

if alst.count(1) == n:
    if n % 2 == 0:
        print(0)
    else:
        print(-1)
    exit()
    
if alst.count(1) == n - 1:
    if n % 2 == 1:
        print(0)
    else:
        print(-1)
    exit()

xor = 0
for num in alst[2:]:
    xor ^= num
total = alst[0] + alst[1]
if xor > total:
    print(-1)
    exit()

diff = total - xor
if diff % 2 == 1:
    print(-1)
    exit()

if alst[0] == 1:
    if xor == alst[0] ^ alst[1]:
        print(0)
    else:
        print(-1)
    exit()
    
diff //= 2
total_bin = format(total, "b")
diff_bin = format(diff,"b")
xor_bin = format(xor,"b")
i = len(total_bin)
ans = 0
for j in range(i, 0, -1):
    if j <= len(diff_bin):
        if diff_bin[-j] == "1":
            ans += 2 ** (j - 1)
            continue
for j in range(i, 0, -1):
    if j <= len(diff_bin):
        if diff_bin[-j] == "1":
            continue
    if j <= len(xor_bin):
        if xor_bin[-j] == "0":
            continue    
    if ans + 2 ** (j - 1) < alst[0]:
        ans += 2 ** (j - 1)
if ans == 0 or ans >= alst[0]:
    print(-1)
else:
    print(alst[0] - ans)
Problem p02695 (python)

Fixed submission:
import itertools

n, m, q = map(int, input().split())
lst = []
for _ in range(q):
    item = list(map(int, input().split()))
    lst.append(item)

max_num = 0
for num_lst in itertools.combinations_with_replacement(range(1, m + 1), n):
    count = 0
    for a, b, c, d in lst:
        if num_lst[b - 1] - num_lst[a - 1] == c:
            count += d
    max_num = max(max_num, count)
print(max_num)

Buggy submission (verdict: Runtime Error):
import bisect

def lis(lst):
    lis_lst = [lst[0]]
    for i in lst:
        if i > lis_lst[-1]:
            lis_lst.append(i)
        else:
            lis_lst[bisect.bisect_left(lis_lst, i)] = i
    return len(lis_lst)

n = int(input())

a = list(map(int, input().split()))

uv = {}
for _ in range(n-1):
    u, v = map(int, input().split())
    uv[v] = u
    
for i in range(1, n + 1):
    lst = []    
    while i != 1:
        lst.append(a[i - 1])
        i = uv[i]
    lst.append(a[0])
    lst.reverse()
    print(lis(lst))
Problem p02834 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 9)

n, u, v = map(int, input().split())
u -= 1
v -= 1
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    ab[a - 1].append(b - 1)
    ab[b - 1].append(a - 1)

def dfs1(pos, before_pos):
    for next_pos in ab[pos]:
        if next_pos == before_pos:
            continue
        if next_pos == u:
            return True, [pos]
        tf, root = dfs1(next_pos, pos)
        if tf:
            root.append(pos)
            return True, root
    return False, []
    
root = dfs1(v, -1)[1]
root = root[::-1]
root.append(u)
n = (len(root) + 1) // 2
pos = root[n]
before_pos = root[n - 1]

max_len = n

def dfs2(pos, before_pos, cnt):
    global max_len
    cnt += 1
    max_len = max(max_len, cnt)
    if cnt < n - 1:
        dfs2(root[cnt + 1], pos, cnt)
    else:
        for next_pos in ab[pos]:
            if next_pos == before_pos:
                continue
            dfs2(next_pos, pos, cnt)


dfs2(pos, before_pos, n - 1)
ans = max_len - 1
ans = max(ans, 0)
print(ans)

Buggy submission (verdict: Runtime Error):
n, u, v = map(int, input().split())
u -= 1
v -= 1
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    ab[a - 1].append(b - 1)
    ab[b - 1].append(a - 1)

def bfs1(pos, before_pos):
    for next_pos in ab[pos]:
        if next_pos == before_pos:
            continue
        if next_pos == u:
            return True, [pos]
        tf, root = bfs1(next_pos, pos)
        if tf:
            root.append(pos)
            return True, root
    return False, []
    
root = bfs1(v, -1)[1]
root = root[::-1]
root.append(v)
n = (len(root)) // 2

max_len = 0

def bfs2(pos, before_pos, cnt):
    global max_len
    cnt += 1
    max_len = max(max_len, cnt)
    if cnt < n - 1:
        bfs2(root[cnt + 1], pos, cnt)
    else:
        for next_pos in ab[pos]:
            if next_pos == before_pos:
                continue
            bfs2(next_pos, pos, cnt)


bfs2(v, -1, -1)
ans = max_len - 1
ans = max(ans, 0)
print(ans)

Buggy submission (verdict: Runtime Error):
n, u, v = map(int, input().split())
u -= 1
v -= 1
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    ab[a - 1].append(b - 1)
    ab[b - 1].append(a - 1)

def bfs1(pos, before_pos):
    for next_pos in ab[pos]:
        if next_pos == before_pos:
            continue
        if next_pos == u:
            return True, [pos]
        tf, root = bfs1(next_pos, pos)
        if tf:
            root.append(pos)
            return True, root
    return False, []
    
root = bfs1(v, -1)[1]
root = root[::-1]
n = (len(root) + 1) // 2

ans = (len(root)) // 2 * 2 - 1
ans = max(ans, 0)

max_len = 0

def bfs2(pos, before_pos, cnt):
    global max_len
    cnt += 1
    max_len = max(max_len, cnt)
    if cnt < n - 1:
        bfs2(root[cnt + 1], pos, cnt)
    else:
        for next_pos in ab[pos]:
            if next_pos == before_pos:
                continue
            bfs2(next_pos, pos, cnt)


bfs2(v, -1, -1)
ans = max_len - 1
ans = max(ans, 0)
print(ans)
Problem p02836 (python)

Fixed submission:
s = input()
cnt = 0
for s1, s2 in zip(s, s[::-1]):
    if s1 != s2:
        cnt += 1
print(cnt // 2)

Buggy submission (verdict: Runtime Error):
s = int(input())
cnt = 0
for s1, s2 in zip(s, s[::-1]):
    if s1 != s2:
        cnt += 1
print(cnt // 2)