Answer the question
In order to leave comments, you need to log in
Why does RunTime error occur when calling recursion?
Greetings, I'm tormenting the task for the second day and I don't know what's wrong with it.
1 - 3 screen condition of the problem
4 - incorrect answers
Gives an error RunTime error I will assume that it is a matter of recursion, but at the same time, according to the condition of the problem, a closed loop should not occur (I solve it using graphs)
all_classes = {}
for _ in range(int(input())):
request = input().split()
if len(request) == 1:
if request[0] not in all_classes:
all_classes[request[0]] = []
else:
all_classes[request[0]] += request[0]
else:
del request[1]
if request[0] not in all_classes:
all_classes[request[0]] = []
for parent in request[1:]:
all_classes[request[0]] += parent
else:
for parent in request[1:]:
all_classes[request[0]] += parent
def parent_width_search(child):
global count
if request[0] in all_classes[child]:
count += 1
return
else:
for parent in all_classes[child]:
parent_width_search(parent)
if count >= 1:
return
for _ in range(int(input())):
count = 0
request = input().split()
if request[0] == request[1]:
print('Yes')
elif request[1] not in all_classes:
print('No')
else:
parent_width_search(request[1])
print('Yes' if count >= 1 else 'No')
Answer the question
In order to leave comments, you need to log in
The question is closed. It turns out that when reading the request, I did not take into account that when adding a string to the list using +=, the string is added letter by letter, and not completely
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question