I was doing this [problem[problem:1006E]](https://mirror.codeforces.com/contest/1006/problem/E) in python. I am receiving Runtime error on test 3 with exit code -1073741571.↵
I am not able to understand the fault in my code.↵
Any help is greatly appreciated.↵
↵
Link to my [submission](https://mirror.codeforces.com/contest/1006/submission/118525767) .↵
↵
The main part of the code is below: ↵
↵
~~~~~↵
↵
cnt = 1↵
d = defaultdict(lambda:0)↵
travesal = []↵
subtreesize = defaultdict(lambda:0)↵
↵
def dfs(node,parent):↵
global adj,cnt↵
travesal.append(node)↵
ans = 1↵
for child in adj[node]:↵
if child==parent:↵
continue↵
d[child]=cnt↵
cnt+=1↵
ans+=dfs(child,node)↵
subtreesize[node]=ans↵
return ans↵
↵
n,q = li()↵
p = li()↵
adj = [[] for i in range(200002)]↵
for i in range(len(p)):↵
adj[i+1].append(p[i]-1)↵
# print(p[i]-1)↵
adj[p[i]-1].append(i+1)↵
dfs(0,-1)↵
for i in range(len(travesal)):↵
travesal[i]+=1↵
for i in range(q):↵
u,k = li()↵
u-=1↵
dis = d[u]↵
x = dis+k-1↵
# print("x",x)↵
if x>=len(travesal) or subtreesize[u]<k:↵
print(-1)↵
else:↵
print(travesal[x])↵
~~~~~↵
↵
I am not able to understand the fault in my code.↵
Any help is greatly appreciated.↵
↵
Link to my [submission](https://mirror.codeforces.com/contest/1006/submission/118525767) .↵
↵
The main part of the code is below: ↵
↵
~~~~~↵
↵
cnt = 1↵
d = defaultdict(lambda:0)↵
travesal = []↵
subtreesize = defaultdict(lambda:0)↵
↵
def dfs(node,parent):↵
global adj,cnt↵
travesal.append(node)↵
ans = 1↵
for child in adj[node]:↵
if child==parent:↵
continue↵
d[child]=cnt↵
cnt+=1↵
ans+=dfs(child,node)↵
subtreesize[node]=ans↵
return ans↵
↵
n,q = li()↵
p = li()↵
adj = [[] for i in range(200002)]↵
for i in range(len(p)):↵
adj[i+1].append(p[i]-1)↵
# print(p[i]-1)↵
adj[p[i]-1].append(i+1)↵
dfs(0,-1)↵
for i in range(len(travesal)):↵
travesal[i]+=1↵
for i in range(q):↵
u,k = li()↵
u-=1↵
dis = d[u]↵
x = dis+k-1↵
# print("x",x)↵
if x>=len(travesal) or subtreesize[u]<k:↵
print(-1)↵
else:↵
print(travesal[x])↵
~~~~~↵
↵