Problem p03805 (python)

Fixed submission:
# from log import logger

N, M = map(int, input().split())
key_value = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    key_value[a].append(b)
    key_value[b].append(a)

# logger.info('key_value: {}\n'.format(key_value))

closed_list = [False] * N
closed_list[0] = True
cnt = 0


def dfs(target):
    global cnt
    # logger.info('key_value[target]: {}'.format(key_value[target]))
    if all(closed_list):
        cnt += 1
        return
    for node in key_value[target]:
        # logger.info('node: {}'.format(node))
        # 探索済みでない場合
        if closed_list[node] is False:
            closed_list[node] = True
            dfs(node)
            closed_list[node] = False
        # logger.info('closed_list: {}\n'.format(closed_list))
    return


dfs(0)
print(cnt)

Buggy submission (verdict: Runtime Error):
from log import logger

N, M = map(int, input().split())
key_value = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    key_value[a].append(b)
    key_value[b].append(a)

logger.info('key_value: {}\n'.format(key_value))

closed_list = [False] * N
closed_list[0] = True
cnt = 0


def dfs(target):
    global cnt
    logger.info('key_value[target]: {}'.format(key_value[target]))
    if all(closed_list):
        cnt += 1
        return
    for node in key_value[target]:
        logger.info('node: {}'.format(node))
        # 探索済みでない場合
        if closed_list[node] is False:
            closed_list[node] = True
            dfs(node)
            closed_list[node] = False
        logger.info('closed_list: {}\n'.format(closed_list))
    return


dfs(0)
print(cnt)

Buggy submission (verdict: Runtime Error):
from log import logger

N, M = list(map(int, input().split()))

key_value = {}
for i in range(M):
    a, b = input().split()
    try:
        key_value[a].append(b)
    except KeyError:
        key_value[a] = [b]
    try:
        key_value[b].append(a)
    except KeyError:
        key_value[b] = [a]

logger.info("key_value: {}\n".format(key_value))

"""深さ優先探索: depthFirst
# 初期化
# nodeを展開し，nodeをclosed_listに入れる
# 2つの条件を満たしたら，childrenをopen_listに入れる
# # 1. open_listに同じchildrenがない
# # 2. closedに同じchildrenがない
# 条件を満たせばcntをインクリメントする
"""
# 初期化
node = "1"
# 探索対象
open_list = [node]
# 探索済み
closed_list = []

cnt = 0
while True:
    # for i in range(10):
    logger.info(
        "[before] open_list: {}, closed_list: {}".format(open_list, closed_list)
    )
    if len(open_list) == 0:
        break
    else:
        # 先頭を削除し，探索済みに追加する
        node = open_list[0]
        open_list = open_list[1:]
        closed_list.append(node)
        # 結果の照合
        if sorted(closed_list) == list(map(str, range(1, N + 1))):
            cnt += 1
        else:
            # nodeの展開
            try:
                children = key_value[node]
                for i in range(len(children)):
                    if children[i] not in open_list and children[i] not in closed_list:
                        open_list.append(children[i])
            except KeyError:
                pass
    logger.info(
        "[after] open_list: {}, closed_list: {}\n".format(open_list, closed_list)
    )
print(cnt)