Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
a = [int(input())-1 for _ in [0]*N]
end = set()
end2 = set()
add = end.add
add2 = end2.add
result = 0
for i in range(N-1, -1, -1):
    if a[i] == -1:
        result += 1
    elif i in end:
        continue
    else:
        v = i
        l = 0
        n = 0
        while a[v] != -1:
            if v not in end2 and l < K:
                n += 1
                add2(v)
            l += 1
            add(v)
            v = a[v]
        result += n


print(result)

Fixed submission:
import sys
readline = sys.stdin.readline
N, K = map(int, readline().split())
a = [int(readline())-1 for _ in [0]*N]
result = a.count(-1)

nodes = set()
node_add = nodes.add
hoge = set()
hoge_add = hoge.add
for i in range(N):
    if a[i] in hoge:
        node_add(a[i])
    hoge_add(a[i])

visited = set()
add = visited.add
startnode = {v: K-1 for v in {i for i in range(N) if a[i] > -1} - set(a)}
while startnode:
    nextnode = dict()
    for v, l in startnode.items():
        #print("start:", v, "length:", l)
        result += (v not in visited)
        add(v)
        v = a[v]
        while a[v] > -1 and v not in nodes and l > 0:
            #print(v)
            if v not in visited:
                result += 1
                add(v)
            l -= 1
            v = a[v]
        if a[v] > -1 and v in nodes and l > 0:
            #print("->", v)
            nextnode[v] = max(nextnode.get(v, 0), l-1)
    startnode = nextnode

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
a = [int(input())-1 for _ in [0]*N]
end = set()
end2 = set()
add = end.add
add2 = end2.add
result = a.count(-1)

leaves = {i for i in range(N) if a[i] > -1} - set(a)
for i in leaves:
    v = i
    l = 0
    n = 0
    while a[v] != -1:
        if v not in end2 and l < K:
            n += 1
            add2(v)
        l += 1
        v = a[v]
    result += n

print(result)

Fixed submission:
import sys
readline = sys.stdin.readline
N, K = map(int, readline().split())
a = [int(readline())-1 for _ in [0]*N]
result = a.count(-1)

nodes = set()
node_add = nodes.add
hoge = set()
hoge_add = hoge.add
for i in range(N):
    if a[i] in hoge:
        node_add(a[i])
    hoge_add(a[i])

visited = set()
add = visited.add
startnode = {v: K-1 for v in {i for i in range(N) if a[i] > -1} - set(a)}
while startnode:
    nextnode = dict()
    for v, l in startnode.items():
        #print("start:", v, "length:", l)
        result += (v not in visited)
        add(v)
        v = a[v]
        while a[v] > -1 and v not in nodes and l > 0:
            #print(v)
            if v not in visited:
                result += 1
                add(v)
            l -= 1
            v = a[v]
        if a[v] > -1 and v in nodes and l > 0:
            #print("->", v)
            nextnode[v] = max(nextnode.get(v, 0), l-1)
    startnode = nextnode

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, K = map(int, input().split())
a = [int(input())-1 for _ in [0]*N]
result = a.count(-1)

leaves = {i for i in range(N) if a[i] > -1} - set(a)
dq = deque()
append = dq.append
for i in leaves:
    v = i
    l = 0
    while a[v] != -1 and l < K:
        append(v)
        l += 1
        v = a[v]

print(result+len(set(dq)))

Fixed submission:
import sys
readline = sys.stdin.readline
N, K = map(int, readline().split())
a = [int(readline())-1 for _ in [0]*N]
result = a.count(-1)

nodes = set()
node_add = nodes.add
hoge = set()
hoge_add = hoge.add
for i in range(N):
    if a[i] in hoge:
        node_add(a[i])
    hoge_add(a[i])

visited = set()
add = visited.add
startnode = {v: K-1 for v in {i for i in range(N) if a[i] > -1} - set(a)}
while startnode:
    nextnode = dict()
    for v, l in startnode.items():
        #print("start:", v, "length:", l)
        result += (v not in visited)
        add(v)
        v = a[v]
        while a[v] > -1 and v not in nodes and l > 0:
            #print(v)
            if v not in visited:
                result += 1
                add(v)
            l -= 1
            v = a[v]
        if a[v] > -1 and v in nodes and l > 0:
            #print("->", v)
            nextnode[v] = max(nextnode.get(v, 0), l-1)
    startnode = nextnode

print(result)