Problem p02678 (python)

Fixed submission:
import collections
def solve(a, b, ab_list):

    route = {}

    for a, b in ab_list:
        if a not in route:
            route[a] = []
        if b not in route:
            route[b] = []

        route[a].append(b)
        route[b].append(a)

    deque = collections.deque()
    deque.extend(route[1])
    route_memo = {i:1 for i in route[1]}
    while len(deque) > 0:
        room = deque.popleft()
        for i in route[room]:
            if i != 1 and i not in route_memo:
                route_memo[i] = room
                deque.append(i)

    if len(route_memo.keys()) == n-1:
        print("Yes")
        tmp = sorted(route_memo.items())
        for i in tmp:
            print(i[1])
    else:
        print("No")

if __name__ == "__main__":
    n, m = [int(i) for i in input().split()]
    ab_list = [[int(i) for i in input().split()] for j in range(m)]
    solve(n, m, ab_list)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(5000)
def solve(a, b, ab_list):

    route = {}

    for a, b in ab_list:
        if a not in route:
            route[a] = []
        if b not in route:
            route[b] = []

        route[a].append(b)
        route[b].append(a)

    route_memo = {}
    def func(x, memo):
        if x in route_memo:
            memo = route_memo[x]
            return True

        if  1 in route[x]:
            route_memo[x] = memo
            return True
        else:
            tmp = False
            for y in route[x]:
                if y not in memo:
                    memo.append(y)
                    tmp = func(y, memo)
                if tmp:
                    return True
            return False
    ans = []
    flg = True
    for i in range(2, n+1):
        memo = []
        if func(i, memo):
            if len(memo) == 0:
                memo.append(1)
            ans.append(memo[-1])
        else:
            flg = False
            break

    if flg:
        print("Yes")
        for i in ans:
            print(i)
    else:
        print("No")

if __name__ == "__main__":
    n, m = [int(i) for i in input().split()]
    ab_list = [[int(i) for i in input().split()] for j in range(m)]
    solve(n, m, ab_list)

Buggy submission (verdict: Runtime Error):

def solve(a, b, ab_list):

    route = {}

    for a, b in ab_list:
        if a not in route:
            route[a] = []
        if b not in route:
            route[b] = []

        route[a].append(b)
        route[b].append(a)

    def func(x, memo):
        if  1 in route[x]:
            return True
        else:
            tmp = False
            for y in route[x]:
                if y not in memo:
                    memo.append(y)
                    tmp = func(y, memo)
                if tmp:
                    return True
            return False
    ans = []
    flg = True
    for i in range(2, n+1):
        memo = []
        if func(i, memo):
            if len(memo) == 0:
                memo.append(1)
            ans.append(memo[-1])
        else:
            flg = False
            break

    if flg:
        print("Yes")
        for i in ans:
            print(i)
    else:
        print("No")

if __name__ == "__main__":
    n, m = [int(i) for i in input().split()]
    ab_list = [[int(i) for i in input().split()] for j in range(m)]
    solve(n, m, ab_list)