Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


INF = 10**9 + 1

if __name__ == "__main__":
    N, M, L = map(int, input().split())
    D = [[INF] * N for _ in range(N)]
    for i in range(N):
        D[i][i] = 0
    for i in range(M):
        tmp = list(map(int, input().split()))
        D[tmp[0]-1][tmp[1]-1] = tmp[2]
        D[tmp[1]-1][tmp[0]-1] = tmp[2]
    Q = int(input())
    st = list()
    for i in range(Q):
        st.append(list(map(int, input().split())))

    D = warshall_floyd(D)

    for i in range(N):
        for j in range(N):
            if i == j:
                D[i][j] = 0
            elif D[i][j] <= L:
                D[i][j] = 1
            else:
                D[i][j] = INF

    D = warshall_floyd(D)

    for i in range(Q):
        ans = D[st[i][0]-1][st[i][1]-1] - 1
        if ans == INF - 1:
            print(-1)
        else:
            print(ans)


Fixed submission:
def warshall_floyd(d):
    for k in range(len(d)):
        for i in range(len(d)):
            for j in range(len(d)):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])
    return d


INF = 10**9 + 1

if __name__ == "__main__":
    N, M, L = map(int, input().split())
    D = [[INF] * N for _ in range(N)]
    for i in range(N):
        D[i][i] = 0
    for i in range(M):
        tmp = list(map(int, input().split()))
        D[tmp[0]-1][tmp[1]-1] = tmp[2]
        D[tmp[1]-1][tmp[0]-1] = tmp[2]
    Q = int(input())
    st = list()
    for i in range(Q):
        st.append(list(map(int, input().split())))

    D = warshall_floyd(D)

    for i in range(N):
        for j in range(N):
            if i == j:
                D[i][j] = 0
            elif D[i][j] <= L:
                D[i][j] = 1
            else:
                D[i][j] = INF

    D = warshall_floyd(D)

    for i in range(Q):
        ans = D[st[i][0]-1][st[i][1]-1] - 1
        if ans == INF - 1:
            print(-1)
        else:
            print(ans)
Buggy python submission (verdict: Runtime Error):
from math import gcd, sqrt


def IsPrime(num):
    if num < 2:
        return True
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    sqrtNum = sqrt(num)
    for i in range(3, int(sqrtNum) + 1, 2):
        if num % i == 0:
            return False

    return True


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

    # divisors.sort()
    return divisors


if __name__ == "__main__":
    A, B = map(int, input().split())

    gcd = gcd(A, B)

    div = make_divisors(gcd)

    ans = list()
    for i in div:
        if IsPrime(i):
            ans.append(i)

    print(len(ans))


Fixed submission:
from math import sqrt


def IsPrime(num):
    if num < 2:
        return True
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    sqrtNum = sqrt(num)
    for i in range(3, int(sqrtNum) + 1, 2):
        if num % i == 0:
            return False

    return True


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

    # divisors.sort()
    return divisors


if __name__ == "__main__":
    A, B = map(int, input().split())

    A_list = make_divisors(A)
    B_list = make_divisors(B)

    AB_list = set(A_list) & set(B_list)

    ans = list()
    for i in AB_list:
        if IsPrime(i):
            ans.append(i)

    print(len(ans))
Buggy python submission (verdict: Runtime Error):
if __name__ == "__main__":
    N = int(input())
    S = input()

    dp = [[0] * 5005 for i in range(5005)]

    for i in range(N, 0, -1):
        for j in range(N, 0, -1):
            if S[i-1] != S[j-1]:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i+1][j+1] + 1

    result = 0
    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            now = min(dp[i][j], j-i)
            result = max([now, result])

    print(result)


Fixed submission:
if __name__ == "__main__":
    S = input()
    if S == 'Sunny':
        print('Cloudy')
    elif S == 'Cloudy':
        print('Rainy')
    else:
        print('Sunny')
Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

    for i in range(M):
        A[A.index(max(A))] = A[A.index(max(A))] // 2
    
    print(sum(A))
    

Fixed submission:
import heapq

if __name__ == "__main__":
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A = [(-x, x) for x in A]
    heapq.heapify(A)

    for i in range(M):
        tmp = heapq.heappop(A)
        v = tmp[1] // 2
        heapq.heappush(A, (-v, v))
    
    res = 0
    for i in range(len(A)):
        res += A[i][1]
    
    print(res)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

res = 0
for i in range(N):
    res += i

print(res)


Fixed submission:
N = int(input())

res = (N - 1) * N // 2

print(res)
Buggy python submission (verdict: Time Limit Exceeded):
import math
 
def comb(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
 
N = int(input())
s = ["".join(sorted(input())) for _ in range(N)]
 
 
unique_s = set(s)
 
output = 0
for k in unique_s:
     if s.count(k) != 1:
        output += int(comb(s.count(k), 2))
 
print(output)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
s = [input() for i in range(N)]

output = 0
dic = dict()
for i in range(N):
    tmp = ''.join(sorted(s[i]))
    if tmp not in dic:
        dic[tmp] = 1
    else:
        output += dic[tmp]
        dic[tmp] += 1

print(output)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import math

def comb(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
s = [input() for i in range(N)]

for i in range(N):
    s[i] = ''.join(sorted(s[i]))

unique_s = set(s)

output = 0
for k in unique_s:
     if s.count(k) != 1:
        output += int(comb(s.count(k), 2))

print(output)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
s = [input() for i in range(N)]

output = 0
dic = dict()
for i in range(N):
    tmp = ''.join(sorted(s[i]))
    if tmp not in dic:
        dic[tmp] = 1
    else:
        output += dic[tmp]
        dic[tmp] += 1

print(output)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import math

def comb(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
s = [input() for i in range(N)]

for i in range(N):
    s[i] = ''.join(sorted(s[i]))

unique_s = set(s)

output = 0
for k in unique_s:
     if s.count(k) != 1:
        output += int(comb(s.count(k), 2))

print(output)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
s = [input() for i in range(N)]

output = 0
dic = dict()
for i in range(N):
    tmp = ''.join(sorted(s[i]))
    if tmp not in dic:
        dic[tmp] = 1
    else:
        output += dic[tmp]
        dic[tmp] += 1

print(output)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from scipy.special import comb

N = int(input())
s = [input() for i in range(N)]

for i in range(N):
    s[i] = ''.join(sorted(s[i]))

unique_s = set(s)

output = 0
for k in unique_s:
     if s.count(k) != 1:
        output += int(comb(s.count(k), 2))

print(output)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
s = [input() for i in range(N)]

output = 0
dic = dict()
for i in range(N):
    tmp = ''.join(sorted(s[i]))
    if tmp not in dic:
        dic[tmp] = 1
    else:
        output += dic[tmp]
        dic[tmp] += 1

print(output)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
L, R = list(map(int, str(input()).split()))

output = 2 * pow(10,9)
for i in range(L,R+1):
    for j in range(i+1, R+1):
        if (i*j) % 2019 < output:
            output =  (i*j) % 2019
            if output == 0:
                break
print(output)

Fixed submission:
L, R = list(map(int, str(input()).split()))

# L と Rの差が十分に大きければ、答えは0になる
# それ以外の状況であれば愚直に計算しても大した計算量ではない。

MOD = 2019
ouput = 2020 * 2020
for i in range(L, R + 1):
    for j in range(L, R + 1):
        if j > i and ouput > (i * j) % MOD:
            ouput = (i * j) % MOD
            if ouput == 0:
                print(ouput)
                exit()

print(ouput)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    R, G, B, N = map(int, input().split())

    ans = 0
    for i in range(N+1):
        for j in range(N+1):
            tmp = N - i * R - j * G
            if tmp % B == 0 and tmp >= 0:
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    R, G, B, N = map(int, input().split())

    ans = 0
    for i in range(N+1):
        for j in range(N+1):
            tmp = N - i * R - j * G
            if tmp % B == 0 and tmp >= 0:
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import fractions
import copy
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

gcds = list()
for i in range(0,N):
    B = copy.deepcopy(A)
    B.pop(i)
    tmp = gcd_list(B)
    gcds.append(tmp)

print(max(gcds))


Fixed submission:
# -*- coding: utf-8 -*-
A = list(map(int,input().split()))
print(A[2] // A[0] * A[1])
Buggy python submission (verdict: Runtime Error):
from numba import njit


@njit
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10**6] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    print(dp[N-1])


if __name__ == "__main__":
    main()


Fixed submission:
from numba import njit


@njit
def main(N, K, h):
    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    return dp[N-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(main(N, K, h))

Buggy python submission (verdict: Runtime Error):
from numba import njit


@njit
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    print(dp[N-1])


if __name__ == "__main__":
    main()


Fixed submission:
from numba import njit


@njit
def main(N, K, h):
    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    return dp[N-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(main(N, K, h))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    print(dp[N-1])


if __name__ == "__main__":
    main()


Fixed submission:
from numba import njit


@njit
def main(N, K, h):
    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    return dp[N-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(main(N, K, h))
Buggy python submission (verdict: Time Limit Exceeded):
def solve(items, W):
    dp = [[0]*(W+1) for _ in range(len(items)+1)]

    for i, g in enumerate(items, 1):
        for w in range(W+1):
            if w >= g[0]:
                dp[i][w] = max(dp[i-1][w], dp[i-1][w-g[0]]+g[1])
            else:
                dp[i][w] = dp[i-1][w]

    return dp[len(items)][W]


if __name__ == "__main__":
    N, W = map(int, input().split())
    items = [list(map(int, input().split())) for _ in range(N)]
    print(solve(items, W))


Fixed submission:
def solve(items, W):
    dp = [[0]*(W+1) for _ in range(len(items)+1)]

    for i, g in enumerate(items, 1):
        for w in range(W+1):
            if w >= g[0]:
                dp[i][w] = max(dp[i-1][w], dp[i-1][w-g[0]]+g[1])
            else:
                dp[i][w] = dp[i-1][w]

    return dp[len(items)][W]


if __name__ == "__main__":
    N, W = map(int, input().split())
    items = [list(map(int, input().split())) for _ in range(N)]
    print(solve(items, W))
Buggy python submission (verdict: Runtime Error):
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K + 1):
        if res > h[i] - h[i+K]:
            res = h[i] - h[i+K]
    
    print(res)


Fixed submission:
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K+1):
        if res > h[i+K-1] - h[i]:
            res = h[i+K-1] - h[i]
    
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K + 1):
        if res > max(h[i:i+K]) - min(h[i:i+K]):
            res = max(h[i:i+K]) - min(h[i:i+K])
    
    print(res)


Fixed submission:
if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list()
    for i in range(N):
        h.append(int(input()))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K+1):
        if res > h[i+K-1] - h[i]:
            res = h[i+K-1] - h[i]
    
    print(res)
Buggy python submission (verdict: Time Limit Exceeded):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    for i in range(n):
        tmp = factorization(a[i])
        if tmp[0][0] == 2:
            ans += tmp[0][1]

    print(ans)


Fixed submission:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    for i in range(n):
        ans += format(a[i], 'b')[::-1].find('1')

    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    for i in range(n):
        tmp = factorization(a[i])
        if tmp[0][0] == 2:
            ans += tmp[0][1]

    print(ans)


Fixed submission:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0
    for i in range(n):
        ans += format(a[i], 'b')[::-1].find('1')

    print(ans)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_right

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)

    ans = 0

    # まずあるbを選んだ時のcの組み合わせの数のリストを作る。
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        ans += clist[b_pos]

    print(ans)


Fixed submission:
from bisect import bisect_right

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)

    ans = 0

    # まずあるbを選んだ時のcの組み合わせの数のリストを作る。
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        if b_pos != n:
            ans += clist[b_pos]

    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 10**10
    for i in range(1, n):
        g1 = a[0:i]
        g2 = a[i:n]
        tmp = abs(sum(g1) - sum(g2))
        if tmp < ans:
            ans = tmp

    print(ans)


Fixed submission:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    g1 = a[0]
    g2 = sum(a[1:n])
    ans = abs(g1 - g2)
    if n > 2:
        for i in range(1, n-1):
            g1 += a[i]
            g2 -= a[i]
            tmp = abs(g1 - g2)
            if tmp < ans:
                ans = tmp

    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def solve(a):
    ans = deque()

    for i, aa in enumerate(a):
        ans.append(str(aa))
        ans.reverse()

    return list(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    print(' '.join(solve(a)))


Fixed submission:
from collections import deque


def solve(a):
    ans = deque()

    for i, aa in enumerate(a):
        if i % 2 == 0:
            ans.append(str(aa))
        else:
            ans.appendleft(str(aa))

    if len(a) % 2 == 1:
        ans.reverse()

    return list(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    print(' '.join(solve(a)))
Buggy python submission (verdict: Runtime Error):
3 3 33

Fixed submission:
def main():
    c = list(map(int, input().split()))

    c_set = set(c)
    print(len(c_set))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from collections import deque
N = int(input())
A = sorted([int(x) for x in input().split()])
D = deque(A)
ans = 0
if A[0] == A[1]: 
    ans -= 1
while len(D):
    a = D.popleft()
    ans += 1
    D = deque(filter(lambda x: x%a != 0, D))
print(ans)


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
m = max(A)+2
cnt = [0] * m
for a in A:
    cnt[a] += 1
    if cnt[a] == 1:
        for i in range(a+a, m, a):
            cnt[i] += 1
ans = 0
for a in A:
    ans += (cnt[a] == 1)
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = sorted([int(x) for x in input().split()], reverse=True)
ans = 0
for i, ai in enumerate(A):
    f = 1
    for aj in A[i+1:]:
        if ai % aj == 0:
            f = 0
            break
    ans += f
ans -= (A[-1] == A[-2])
print(ans)


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
m = max(A)+2
cnt = [0] * m
for a in A:
    cnt[a] += 1
    if cnt[a] == 1:
        for i in range(a+a, m, a):
            cnt[i] += 1
ans = 0
for a in A:
    ans += (cnt[a] == 1)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = [int(x) for x in input().split()]
INF = 10**9
dist = [INF] * N
ans = [-1] * N
to = [set() for _ in range(N)]
for _ in range(M):
    A, B = [int(x)-1 for x in input().split()]
    to[A].add(B)
    to[B].add(A)
bfs = deque([(0, 0, -1)])
while len(bfs):
    cur, d, p = bfs.popleft()
    if d < dist[cur]:
        dist[cur] = d
        ans[cur] = p
    for v in to[cur]:
        if d+1 < dist[v]:
            bfs.append((v, d+1, cur))
    d += 1
print('Yes')
for a in ans[1:]:
    print(a+1)


Fixed submission:
from collections import deque
N, M = [int(x) for x in input().split()]
INF = 10**6
dist = [INF] * N
ans = [-1] * N
to = [set() for _ in range(N)]
for _ in range(M):
    A, B = [int(x)-1 for x in input().split()]
    to[A].add(B)
    to[B].add(A)
bfs = deque([(0, 0, -1)])
while len(bfs):
    cur, d, p = bfs.popleft()
    if d >= dist[cur]: continue
    dist[cur] = d
    ans[cur] = p
    for v in to[cur]:
        if d+1 < dist[v]:
            bfs.append((v, d+1, cur))
print('Yes')
for a in ans[1:]:
    print(a+1)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = [int(x) for x in input().split()]
INF = 10**9
dist = [INF] * N
ans = [-1] * N
to = [set() for _ in range(N)]
for _ in range(M):
    A, B = [int(x)-1 for x in input().split()]
    to[A].add(B)
    to[B].add(A)
bfs = [(0, 0, -1)]
while len(bfs):
    cur, d, p = bfs.pop()
    if d < dist[cur]:
        dist[cur] = d
        ans[cur] = p
    for v in to[cur]:
        if d+1 < dist[v]:
            bfs.append((v, d+1, cur))
    d += 1
print('Yes')
for a in ans[1:]:
    print(a+1)


Fixed submission:
from collections import deque
N, M = [int(x) for x in input().split()]
INF = 10**6
dist = [INF] * N
ans = [-1] * N
to = [set() for _ in range(N)]
for _ in range(M):
    A, B = [int(x)-1 for x in input().split()]
    to[A].add(B)
    to[B].add(A)
bfs = deque([(0, 0, -1)])
while len(bfs):
    cur, d, p = bfs.popleft()
    if d >= dist[cur]: continue
    dist[cur] = d
    ans[cur] = p
    for v in to[cur]:
        if d+1 < dist[v]:
            bfs.append((v, d+1, cur))
print('Yes')
for a in ans[1:]:
    print(a+1)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
    na = A[past]
    if na in history: 
        break
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)


Fixed submission:
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
s = set()
while True:
    na = A[past]
    if na in s: 
        break
    s.add(na)
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)

Buggy python submission (verdict: Runtime Error):
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
    na = A[past]
    if na in history: 
        break
    history.append(na)
    past = na
s = history.index(na)
e = len(history) - s
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)


Fixed submission:
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
s = set()
while True:
    na = A[past]
    if na in s: 
        break
    s.add(na)
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)