alexjercan/bugnet huggingface dataset:
Problem p02678 (Python) — buggy submission, verdict: Runtime Error

from collections import deque

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

dist = [1000000] * n
ans = [None] * n
not_visited = set(range(n))
not_visited.remove(0)
vs = deque([0])
dist[0] = 0
while vs:
    v = vs.popleft()
    for other_side in e[v]:
        if other_side in not_visited:
            vs.append(other_side)
            not_visited.remove(other_side)
            if dist[other_side] > dist[v] + 1:
                dist[other_side] = dist[v] + 1
                ans[other_side] = v

print("Yes")
for i in ans[1:]:
    print(i + 1)


Error: ModuleNotFoundError: No module named 'scipy'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02678/Python/s593149455.py", line 1, in <module>
    from scipy.sparse.csgraph import dijkstra
ModuleNotFoundError: No module named 'scipy'


Fixed submission:

from scipy.sparse.csgraph import dijkstra
import numpy as np

n, m = map(int, input().split())
e = np.zeros((n, n))
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    e[a][b] = 1
    e[b][a] = 1
dist, predecessor = dijkstra(e, directed=False, return_predecessors=True)

if all(type(v) == np.int32 for v in predecessor[0][1:]):
    print("Yes")
    for v in predecessor[0][1:]:
        print(v + 1)
else:
    print("No")
Problem p02679 (Python) — buggy submission, verdict: Runtime Error

import sys
import math
from collections import defaultdict

input = sys.stdin.buffer.readline
MOD = 1000000007
N = int(input())
dd = defaultdict(int)
count = 1
iwashi = [list(map(int, input().split())) for _ in range(N)]


checked = defaultdict(bool)
zeros = 0

for i in range(N):
    if iwashi[i][0] == iwashi[i][1] == 0:
        zeros += 1
        continue
    iw = iwashi[i]
    gcd_iw = math.gcd(iw[0], iw[1])
    if gcd_iw != 0:
        iw = list(map(lambda x: x // gcd_iw, iw))
    if iw[0] < 0:
        iw = list(map(lambda x: -x, iw))
    if iw[0] == 0 and iw[1] < 0:
        iw = list(map(lambda x: -x, iw))
    dd[tuple(iw)] += 1

for key, val in dd.items():
    if checked[key]:
        continue
    vert_key = (key[1], -key[0]) if key[1] > 0 else (-key[1], key[0])
    vert_val = dd[vert_key] if vert_key in dd else 0
    count *= (pow(2, val, MOD) - 1) + (pow(2, vert_val, MOD) - 1) + 1
    count %= MOD
    checked[vert_key] = True

# 0匹入れる場合を除外
count -= 1

count += zeros
print(count % MOD)


Error: 0

Fixed submission:

import sys
import math
from collections import defaultdict

input = sys.stdin.buffer.readline
MOD = 1000000007
N = int(input())
dd = defaultdict(int)
count = 1
iwashi = [list(map(int, input().split())) for _ in range(N)]


checked = defaultdict(bool)
zeros = 0

for i in range(N):
    if iwashi[i][0] == iwashi[i][1] == 0:
        zeros += 1
        continue
    iw = iwashi[i]
    gcd_iw = math.gcd(iw[0], iw[1])
    if gcd_iw != 0:
        iw = [iw[0] / gcd_iw, iw[1] / gcd_iw]
    if iw[0] < -1:
        iw *= -1
    dd[tuple(iw)] += 1
for key, val in dd.items():
    if checked[key]:
        continue
    vert_key = (key[1], -key[0]) if key[1] >= 0 else (-key[1], key[0])
    vert_val = dd[vert_key] if vert_key in dd else 0
    count *= (pow(2, val, MOD) - 1) + (pow(2, vert_val, MOD) - 1) + 1
    count %= MOD
    checked[vert_key] = True

# 0匹入れる場合を除外
count -= 1

count += zeros
print(count % MOD)
Problem p02681 (Python) — buggy submission, verdict: Runtime Error

s = input()
t = input()
print("Yes" if s == t[:-1] else "No")


Error: ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s074309875.py", line 1, in <module>
    s, t = input().split()
ValueError: not enough values to unpack (expected 2, got 1)


Fixed submission:

s, t = input().split()
print("Yes" if s == t[:-1] else "No")
Problem p02681 (Python) — buggy submission, verdict: Runtime Error

# A - Registration
def main():
    S, T = open(0).read().split()
    print("Yes" if T[:-1] == S else "No")


if __name__ == "__main__":
    main()


Error: ValueError: invalid literal for int() with base 10: 'chokudai'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s408863353.py", line 8, in <module>
    main()
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s408863353.py", line 3, in main
    S, T = map(int, open(0).read().split())
ValueError: invalid literal for int() with base 10: 'chokudai'


Fixed submission:

# A - Registration
def main():
    S, T = map(int, open(0).read().split())
    print("Yes" if T[:-1] == S else "No")


if __name__ == "__main__":
    main()
Problem p02681 (Python) — buggy submission, verdict: Runtime Error

import re

a = input()
b = input()

if re.match(f"^{a}.$", b):
    print("Yes")
else:
    print("No")


Error: ValueError: invalid literal for int() with base 10: 'chokudai'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s616201447.py", line 1, in <module>
    [a, b, h] = [int(input()) for i in range(3)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s616201447.py", line 1, in <listcomp>
    [a, b, h] = [int(input()) for i in range(3)]
ValueError: invalid literal for int() with base 10: 'chokudai'


Fixed submission:

[a, b, h] = [int(input()) for i in range(3)]


result = (a + b) * h // 2
print(result)
Problem p02681 (Python) — buggy submission, verdict: Runtime Error

S = list(map(str, input().split()))[0]
T = list(map(str, input().split()))[0]

if (abs(len(T) - len(S)) == 1) and (T[:-1] == S):
    print("Yes")
else:
    print("No")


Error: ValueError: not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s417304047.py", line 1, in <module>
    S, T = list(map(str, input().split()))
ValueError: not enough values to unpack (expected 2, got 1)


Fixed submission:

S, T = list(map(str, input().split()))

if (abs(len(T) - len(S)) == 1) and (T[:-1] == S):
    print("Yes")
else:
    print("No")
Problem p02681 (Python) — buggy submission, verdict: Runtime Error

S = input()
T = input()

if S == T[0:-1]:
    print("Yes")
else:
    print("No")


Error: ValueError: invalid literal for int() with base 10: 'chokudai'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02681/Python/s135786455.py", line 3, in <module>
    N, K = map(int, input().split())
ValueError: invalid literal for int() with base 10: 'chokudai'


Fixed submission:

import sys

N, K = map(int, input().split())
town_list = list(map(int, input().split()))

roop = 0
queue = [1]

start = -1

for now_town in queue:
    if town_list[now_town - 1] in queue:
        start = town_list[now_town - 1]
        break
    queue.append(town_list[now_town - 1])

if start == -1:
    print(queue[K])
    sys.exit()

before_len = queue.index(start)
roop = len(queue) - before_len

print(queue[before_len + (K - before_len) % roop])
Problem p02682 (Python) — buggy submission, verdict: Time Limit Exceeded

a, b, c, k = map(int, input().split())

a_times = 0
b_times = 0
c_times = 0

if a <= k:
    a_times = a
    # print('a_times = ', a_times)
    if a_times + b <= k:
        b_times = b
        # print('if - b_times = ', b_times)
    else:
        b_times = k - a_times
        # print('else - b_times = ', b_times)
    if a_times + b_times + c <= k:
        c_times = c
        # print('if - c_times = ', c_times)
    else:
        c_times = k - a_times - b_times
        # print('else - c_times = ', c_times)
else:
    a_times = k


print(a_times * 1 + b_times * 0 + c_times * -1)


Error: TLE

Fixed submission:

a, b, c, k = map(int, input().split())

res = 0
i = 0

a_times = 0
b_times = 0
c_times = 0

if a == k:
    a_times = k
else:
    for i in range(k):
        # print('i = ', i)
        if i < a:
            a_times += 1
            # print('res + 1= ', res)
        elif i + a < a + b:
            b_times += 1
            # print('res + 0 = ', res)
        elif i + a + b < a + b + c:
            c_times += 1
            # print('res - 1 = ', res)

print(a_times * 1 + b_times * 0 + c_times * -1)
Problem p02682 (Python) — buggy submission, verdict: Runtime Error

import sys

a, b, c, k = map(int, input().split())
if a >= k:
    print(k)
    sys.exit()

total = a
k = k - a
if k <= 0:
    print(total)
    sys.exit()

k = k - b
if k <= 0:
    print(total)
    sys.exit()

print(total - k)


Error: 0

Fixed submission:

import sys

a, b, c, k = map(int, input().split())
if a >= k:
    print(k)
    sys.exit()
elif a + b >= k:
    print(a)
    sys.exit()

ones = [1] * a
zeros = [0] * b
negas = [-1] * c

totals = []
totals.extend(ones)
totals.extend(zeros)
totals.extend(negas)

totals = totals[0:k]
print(sum(totals))
Problem p02682 (Python) — buggy submission, verdict: Runtime Error

A, B, C, K = map(int, input().split())
if K <= A:
    print(K)
elif K <= A + B:
    print(A)
else:
    print(2 * A + B - K)


Error: 0

Fixed submission:

A, B, C, K = (int(x) for x in input().split())

a = [1] * A
b = [0] * B
c = [-1] * C

X = a + b + c
print(sum(X[:K]))
Problem p02682 (Python) — buggy submission, verdict: Runtime Error

a, b, c, k = map(int, input().split())
ans = 0
if k <= a:
    ans = k
elif k <= a + b:
    ans = a
else:
    ans = a + (-1) * (k - a - b)
print(ans)


Error: 0

Fixed submission:

a, b, c, k = map(int, input().split())
list1 = [1] * a
list0 = [0] * b
list_m1 = [-1] * c

list1.extend(list0)
list1.extend(list_m1)

list1.sort(reverse=True)

ans = 0
for i in range(k):
    ans += list1[i]

print(ans)
Problem p02682 (Python) — buggy submission, verdict: Time Limit Exceeded

a, b, c, k = map(int, input().split())

import sys

ans = 0
if a != 0 and a >= k:
    # a = 1
    print(k)
    sys.exit()
elif a != 0:
    ans += a
    k -= a

if b != 0 and b >= k:
    print(ans)
    sys.exit()
elif b != 0:
    k -= b

ans += k * -1
print(ans)


Error: TLE

Fixed submission:

a, b, c, k = map(int, input().split())

cards = []
for _ in range(a):
    cards.append(1)
for _ in range(b):
    cards.append(0)
for _ in range(c):
    cards.append(-1)

ans = 0
for i in range(k):
    ans += cards[i]

print(ans)
Problem p02682 (Python) — buggy submission, verdict: Time Limit Exceeded

a, b, c, k = map(int, input().split())

total = 0

if a > 0:
    if a < k:
        total += a
        k -= a
    else:
        total += k
        print(total)
        k = 0

if b > 0 and k > 0:
    if b < k:
        k -= b
    else:
        print(total)
        k = 0

if c > 0 and k > 0:
    if c < k:
        total -= c
        print(total)
    else:
        total -= k
        print(total)


Error: TLE

Fixed submission:

a, b, c, k = map(int, input().split())
total = 0

while k > 0:
    if a > 0:
        total += 1
        a -= 1
        k -= 1
        continue
    if b > 0:
        b -= 1
        k -= 1
        continue
    if c > 0:
        total -= 1
        k -= 1
        continue
print(total)
Problem p02682 (Python) — buggy submission, verdict: Time Limit Exceeded

a, b, c, k = map(int, input().split())

if k <= a:
    sum = k * 1
elif a < k and k <= a + b:
    sum = a
elif a + b < k <= a + b + c:
    sum = a + (-1) * (k - a - b)

print(sum)


Error: TLE

Fixed submission:

import numpy as np

a, b, c, k = map(int, input().split())

ones = [1 for i in range(a)]
zeros = [0 for i in range(b)]
minusOnes = [-1 for i in range(c)]

combination = np.array(ones + zeros + minusOnes)
sum = 0
for i in range(k):
    sum += combination[i]
print(sum)
Problem p02682 (Python) — buggy submission, verdict: Runtime Error

A, B, C, K = map(int, input().split(" "))
if K <= A:
    print(K)
else:
    K = K - A
    if K <= B:
        print(A)
    else:
        K = K - B
        print(A - K)


Error: AttributeError: 'builtin_function_or_method' object has no attribute 'split'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02682/Python/s825415458.py", line 1, in <module>
    A, B, C, K = map(int, input.split(" "))
AttributeError: 'builtin_function_or_method' object has no attribute 'split'


Fixed submission:

A, B, C, K = map(int, input.split(" "))
if K <= A:
    print(K)
else:
    K = K - A
    if K <= B:
        print(A)
    else:
        K = K - B
        print(A - K)
Problem p02683 (Python) — buggy submission, verdict: Runtime Error

N, M, X = map(int, input().split())
C, A = [], []
for _ in range(N):
    tmp = [int(a) for a in input().split()]
    C.append(tmp[0])
    A.append(tmp[1:])

mi = 1 << 100
for binary in range(1 << N):
    B = [0] * M
    c = 0
    for i in range(N):
        if binary >> i & 1:
            c += C[i]
            for j in range(M):
                B[j] += A[i][j]
    if min(B) >= X:
        mi = min(c, mi)
print(mi if mi < 1 << 99 else -1)


Error: TypeError: 'str' object cannot be interpreted as an integer
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02683/Python/s161118337.py", line 3, in <module>
    for _ in range(N):
TypeError: 'str' object cannot be interpreted as an integer


Fixed submission:

N, M, X = input().split()
C, A = [], []
for _ in range(N):
    tmp = [int(a) for a in input().split()]
    C.append(tmp[0])
    A.append(tmp[1:])

mi = 1 << 100
for binary in range(1 << N):
    B = [0] * M
    c = 0
    for i in range(N):
        if binary >> N & 1:
            c += C[i]
            for j in range(M):
                B[j] += A[i][j]
    if min(B) >= X:
        mi = min(c, mi)
print(mi if mi < 1 << 99 else -1)
Problem p02684 (Python) — buggy submission, verdict: Time Limit Exceeded

[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
count = 0
visited = dict()
tmp = 1
while count < k:
    visited[tmp] = count
    count += 1
    tmp = a[tmp - 1]
    if tmp in visited:
        break
if count < k:
    k = (k - count) % (count - visited[tmp])
    for _ in range(k):
        tmp = a[tmp - 1]
print(tmp)


Error: TLE

Fixed submission:

[n, k], [*a] = [[*map(int, t.split())] for t in open(0)]
ans = 1
for _ in range(k):
    ans = a[ans - 1]
print(ans)
Problem p02684 (Python) — buggy submission, verdict: Runtime Error

class Telp:
    def __init__(self, to, idx):
        self.to = to
        self.idx = idx
        self.visited = False
        self.step = -1

    def visit(self, step):
        self.visited = True
        self.step = step

    def is_visited(self):
        return self.visited


n, k = map(int, input().split())

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

net = {}

for i in range(n):
    net[i] = Telp(a[i], i)

current = net[0]
count = 0
path = []

while not current.visited:
    # print(current.to)
    path.append(current)
    current.visit(count)
    current = net[current.to - 1]
    count += 1
    if count == k:
        print(path[-1].to)
        exit(0)
    # print("count", count)

offset = current.step
loop = count - offset
fact_idx = (k - offset) % loop

# print("count, offset, loop, fact_idx", count, offset, loop, fact_idx)
# print("ans", path[offset + fact_idx - 1].to)

print(path[offset + fact_idx - 1].to)


Error: 0

Fixed submission:

class Telp:
    def __init__(self, to, idx):
        self.to = to
        self.idx = idx
        self.visited = False
        self.step = -1

    def visit(self, step):
        self.visited = True
        self.step = step

    def is_visited(self):
        return self.visited


n, k = map(int, input().split())

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

net = {}

for i in range(n):
    net[i] = Telp(a[i], i)

current = net[0]
count = 0
path = [0]

while not current.visited:
    # print(current.to)
    current.visit(count)
    current = net[current.to - 1]
    path.append(current)
    count += 1
    # print("count", count)

offset = current.step
loop = count - offset
fact_idx = (k - offset) % loop

# print("count, offset, loop, fact_idx", count, offset, loop, fact_idx)
# print("ans", path[offset + fact_idx - 1].to)

print(path[offset + fact_idx - 1].to)
Problem p02684 (Python) — buggy submission, verdict: Runtime Error

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

NN = [-1] * N
NN[0] = 0

i = 0
num = 0

while True:
    num += 1
    i = A[i] - 1
    if NN[i] != -1:
        n1 = NN[i]
        break
    NN[i] = num

n_ = num - n1

if K > n1:
    K -= n1
    t = NN.index(n1 + K % n_) + 1
    print(t)
else:
    t = NN.index(K) + 1
    print(t)


Error: 0

Fixed submission:

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

NN = [-1] * N
NN[0] = 0

i = 0
num = 0

while True:
    num += 1
    i = A[i] - 1
    if NN[i] != -1:
        n1 = NN[i]
        break
    NN[i] = num

n_ = num - n1

if K > N:
    K -= n1
    t = NN.index(n1 + K % n_) + 1
    print(t)
else:
    t = NN.index(K)
    print(t) + 1
Problem p02684 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
from io import StringIO
import unittest


def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    loop_list = []
    loop_set = set()
    now = 0

    if k > n:
        for _ in range(k):
            now = a[now] - 1

            if now + 1 in loop_set:
                s_index = loop_list.index(now + 1)
                before_loop = s_index
                loop_list = loop_list[s_index:]
                break
            else:
                loop_list.append(now + 1)
                loop_set.add(now + 1)

        l = len(loop_list)
        k -= before_loop

        # print(loop_list, l, before_loop)
        print(loop_list[k % l - 1])
    else:
        for _ in range(k):
            now = a[now] - 1

        print(now + 1)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 5
3 2 4 1"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 727202214173249351
6 5 2 5 3 2"""
        output = """2"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()


Error: TLE

Fixed submission:

import sys
from io import StringIO
import unittest


def resolve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    loop_list = []
    now = 0

    if k > n:
        while 1:
            now = a[now] - 1

            if now + 1 in loop_list:
                s_index = loop_list.index(now + 1)
                before_loop = s_index
                loop_list = loop_list[s_index:]
                break
            else:
                loop_list.append(now + 1)

        l = len(loop_list)
        k -= before_loop

        # print(loop_list, l, before_loop)
        print(loop_list[k % l - 1])
    else:
        for _ in range(k):
            now = a[now] - 1

        print(now + 1)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4 5
3 2 4 1"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 727202214173249351
6 5 2 5 3 2"""
        output = """2"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()
Problem p02684 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
X = list(map(int, input().split()))
town = []
dic = {}
i = 0
j = 0
while i not in dic and j < K:
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1

if j == K:
    print(i + 1)

else:
    num = dic[i]
    loop = j - num
    print(town[((K - num) % loop) + num] + 1)


Error: 0

Fixed submission:

N, K = map(int, input().split())
X = list(map(int, input().split()))
town = []
dic = {}
i = 0
j = 0
while i not in dic and j < K:
    dic[i] = j
    town.append(i)
    i = X[i] - 1
    j += 1

if j == K:
    print(town[K] + 1)

else:
    num = dic[i]
    loop = j - num
    print(town[((K - num) % loop) + num] + 1)
Problem p02684 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
portals = [0] + list(map(int, input().split()))

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    # teleport
    curTown = portals[curTown]

    curTime += 1

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])


Error: WA

Fixed submission:

N, K = map(int, input().split())
portals = [0] + list(map(int, input().split()))

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    # teleport
    curTown = portals[curTown]

    curTime += 1
    print(visitTowns, visitTimes[1:])

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])
Problem p02684 (Python) — buggy submission, verdict: Runtime Error

n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))

visited_city = {1: 0}
visited_city_inv = {0: 1}

now = 1
num = 0
while True:
    next_city = arr[now - 1]
    num += 1
    if next_city not in visited_city:
        visited_city[next_city] = num
        visited_city_inv[num] = next_city
        now = next_city
    else:
        break

if k < num:
    ans = visited_city_inv[k]
else:
    x = visited_city[next_city]
    period = num - x
    ans = visited_city_inv[x + (k - x) % period]

print(ans)
# print(visited_city)


Error: 0

Fixed submission:

n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))

visited_city = {1: 0}
visited_city_inv = {0: 1}

now = 1
num = 0
while True:
    next_city = arr[now - 1]
    num += 1
    if next_city not in visited_city:
        visited_city[next_city] = num
        visited_city_inv[num] = next_city
        now = next_city
    else:
        break

if k < num:
    ans = visited_city[k]
else:
    x = visited_city[next_city]
    period = num - x
    ans = visited_city_inv[x + (k - x) % period]

print(ans)
# print(visited_city)